« The Fight Continues!! - JBoss vs. SpringSource | Main | Spring 3.0.0 Has Been Released »


Adding Timeouts in HTTPBuilder - Use Java

This is just a short post that's intended to let anyone discovering this site via search engine referral that in Groovy-module HTTPBuilder, at present, http timeouts need to be set in Java.  There is no groovy-fied way to set timeouts yet.

The following block illustrates how to set a connection timeout and a socket timeout on a basic httpbuilder GET request:

// create a new builder
def http = new HTTPBuilder( args[0] )
  http.request(GET,HTML) { req ->
    headers.'User-Agent' = 'GroovyHTTPBuilderTest/1.1'
    headers.'Referer' = 'http://blog.techstacks.com/'

    // Begin: java code to set HTTP Parameters/Properties
    // Groovy HTTPBuilder doesn't provide convenience methods
    // for many of these yet.
    req.getParams().setParameter("http.connection.timeout", new Integer(5000));
    req.getParams().setParameter("http.socket.timeout", new Integer(5000));
    // End java code to set HTTP Parameters/Properties

    response.success = { resp, html ->
      println "Server Response: ${resp.statusLine}"
      println "Server Type: ${resp.getFirstHeader('Server')}"
      println "Title: ${html.HEAD.TITLE.text()}"
    response.failure = { resp ->
      println resp.statusLine

In the example above, I'm setting an http connection timeout to 5 seconds and a socket timeout to 5 seconds as well. (Not very practical but this is only for illustrative purposes any way) The script starts out in groovy, then switches over to a java syntax when it comes time to set the timeout parameters, then switches back to a groovy syntax for the response. I hope this helps!


TrackBack URL for this entry:

Listed below are links to weblogs that reference Adding Timeouts in HTTPBuilder - Use Java: