Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala - dispatch example not working

I'm following the very first example in the dispatchdocs -

    val svc = url("http://api.hostip.info/country.php")
    val country = Http(svc OK as.String)
    for (c <- country)
      println(c)

I do not get any output printed. When I change it to below to make blocking call then I get the output.

val res = country()
println(res)

Need help with this.

Full program-

import dispatch._
object DispatchTest {

  def main(args: Array[String]) {
    val svc = url("http://api.hostip.info/country.php")
    val country = Http(svc OK as.String)
    for (c <- country)
      println(c)
  }
}
like image 755
Shwetanka Avatar asked Nov 22 '25 19:11

Shwetanka


1 Answers

Hmm, not sure here, but maybe the problem is that your main thread is finished so fast, that background thread (in which Dispatch works asynchronously) has no time for taking action?

To check this you may try to insert a delay:

for (c <- country) // Here we spawn a background thread!
  println(c)

Thread.sleep(500) // So let's wait half a second for it to work

Of course, in real program you should never need to do this.

Another option for delay is simply a readLine() in the end of main.

like image 181
NIA Avatar answered Nov 24 '25 14:11

NIA