Created
September 7, 2011 23:13
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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