Skip to content

Instantly share code, notes, and snippets.

@aviflax
Created September 7, 2011 23:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aviflax/1202094 to your computer and use it in GitHub Desktop.
Save aviflax/1202094 to your computer and use it in GitHub Desktop.
How to reliably implement a request timeout for Restlet's Client and ClientResource classes
#!/usr/bin/env groovy
@GrabResolver(name="restlet", root="http://maven.restlet.org/")
@Grab(group="org.restlet.jse", module="org.restlet", version="2.0.9")
// request timeouts reliably only with Apache HttpClient
@Grab(group="org.restlet.jse", module="org.restlet.ext.httpclient", version="2.0.9")
import org.restlet.*
import org.restlet.data.*
import org.restlet.representation.*
import org.restlet.resource.*
resource = new ClientResource("http://localhost:3000/")
/* Create a Client with the socketTimout parameter for HttpClient and "attach"
it to the ClientResource. Of course we could just use the Client
on its own. */
context = new Context()
context.parameters.add "socketTimeout", "1000"
resource.next = new Client(context, [Protocol.HTTP])
// Set the client to not retry on error. Default is true with 2 attempts.
resource.retryOnError = false
// Test. Should time out after 1 second.
println "Sending GET request. This should throw an exception after 1 second."
println resource.get()
print resource.response.status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment