Question

I am using the dispatch reboot library version 0.9.5 (http://dispatch.databinder.net/Dispatch.html) in my project. Via sbt I have the following line:

libraryDependencies += "net.databinder.dispatch" %% "dispatch-core" % "0.9.5"

In the scala (2.9.2) repl (started using sbt console to get the appropriate dependency) and independent of my code I run the following session:

import dispatch._
import java.util.concurrent.TimeUnit._
val spoo = Http.threads(1).waiting( Duration(10, SECONDS ) )

(I believe that the third line sets up my own threadpool with one thread and a timeout of 10 seconds).

I then run this code repeatedly (in paste mode), to submit a future to fetch a particular url and then print the status code (asynchronously):

spoo(url("http://www.evapcool.com/products/commercial/")).either
    .map {
        case Right(r) => println( "S: " + r.getStatusCode())
        case Left(e)  => println( "E: " + e.toString ) }

Each time I run this line I wait for the status code to get printed before running the line again. For the first twenty to forty calls it works as expected. Then it reliably fails to report either a successful page reply or an exception. My assumption was that if this is caused by a timeout, I should expect the callback to fire after 10 seconds with the Left clause of the Either containing some form of timeout exception. But my experience is that this never comes.

Can anyone help tell me what I'm doing wrong?

Update

By the way, I am aware that there is a similar question (with answer) here but I am looking for the official (i.e. intended by the library author) way to handle timeouts - and it appears to me that this is what the waiting method is designed for

Was it helpful?

Solution

So an answer, although one I'm not enormously satisfied with - because it involves ignoring the rather nice looking waiting method on Http and playing directly with the async-http-client apis is this (inspired by the SO post linked in the question update):

val spoo = Http.threads( 1 ).configure
    { builder =>
        builder.setRequestTimeoutInMs( 10000 )
        builder.setConnectionTimeoutInMs( 10000 )

        builder
    }

My code now runs as expected. Ho hum...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top