1

I have a Grails/Groovy application which is making use of the HTTPBuilder library to make calls to our application server. As resource utilization is going up, we are looking for a way to pool our HTTP connections. The only version of HTTPBuilder which supports pooling that I have found is AsyncHTTPBuilder, but our calls must be made synchronously. Does anyone have experience pooling connections with HTTPBuilder, or is there an alternate library we should be using to make our requests using pooled connections?

Here is an example of our usage of the HTTPBuilder library:

def get(event, request) {    
    def http = new HTTPBuilder(appServerURL)
    def result = ""
    http.client.cookieStore.addCookie
    sessionHolderService.getVPMClientUser().apiSessionCookie
    http.request(GET, TEXT) {
        uri.path = "/path/on/appserver"
        uri.query = [event: event, request: request, responseFormat: 'text/xml']

        response.success = { resp, text ->
            result = text.text                
        }

        response.failure = { resp, text ->
            result = text.text
            throw new VPMClientException(resp.status, resp.message, text.text)
        }
    }
    result
}
McMagic
  • 11
  • 3

1 Answers1

0

We had a similar problem with our application (but limited to one instance of client). The following solution is based on Gpars. The actions are put in a send queue and then consumed by a task.

final def http = ...

final DataflowQueue sendQueue = new DataflowQueue()
final DataflowVariable finished = new DataflowVariable<Boolean>()

def sendQueueTask = task {
    while(!finished.isBound()){
            def sendAction = sendQueue.getVal(1, TimeUnit.SECONDS)
            if(sendAction)
                  sendAction()
        }
}

Then the actions are wrapped in a closure added in the sendQueue :

def get(event, request) { 
  sendQueue << {
       // your get implementation
    }
}
Julien
  • 1,278
  • 12
  • 26
  • Thanks, this seems like it could potentially work for our purposes. One question -- is there a reason you chose to move the http variable declaration outside of the function call? Is it just using the same connection persistently? Appreciate your input. – McMagic Jan 08 '13 at 16:15
  • You are right, the `http` variable is defined only once and should be declared `final`. Everything is in a class marked with the `@Singleton` annotation. – Julien Jan 10 '13 at 08:43
  • Unfortunately, this still isn't working to send requests. Do I need to start the sendQueueTask from the controller? I'm doing exactly what you posted above, and am not seeing any requests on the other server. – McMagic Jan 10 '13 at 21:27
  • I could never get this solution to work. I am now considering abandoning the HTTPBuilder and just using a plain old apache HTTPClient, which supports connection persistence. – McMagic Feb 13 '13 at 21:45