Pooling Web Service Connections in Grails

Apache Commons Pool is a great tool to easily configure an object pool on the JVM. Having a pool of created objects helps when you need to reuse connection objects that are expensive to create (LDAP, Web Service, etc).

While improving the performance of a Grails application, I noticed that 2 to 4 seconds were being consumed simply creating a connection to a specific JAX-WS Java Web Service that we used to retrieve Rules for building data grid columns. The perfect opportunity to install an object pool. Along the way I ran into some issues pooling the JAX-WS Port Proxy stub object, and I’ll show you the simple work around here.

First, install the commons-pool2 jar dependency in BuildConfig.groovy

Next create a BasePooledObjectFactory that Apache Commons Pool will use to create an object to be placed in the pool. You’ll need a create() method to build your object, and a wrap() method that wraps your object with a PooledObject implementation for stats and pool maintenance purposes. This code uses a legacy Java RuleServiceClientFactory to build the ruleService JAX-WS Port Proxy stub object, yours should probably inject a factory or service.

The factory is then wired into a GenericObjectPool in resources.groovy so we can easily inject the ConnectionPool into our Service objects. Here is where you can configure the pool properties.

See how the ruleConnectionPool is used in this sample GridService. The getGridRules() uses an available pooled WS Connection.

The PoolHelper.withCommonsPool is a convenience closure that I use to wrap the handling of the pool. This method handles invalidating and returning objects to the pool, much like how Groovy Sql methods will wrap the closing and transaction handling of a JDBC Connection. It frees up the GridService from knowing the API of Apache Commons Pool.

Finally, you may have noticed that the RuleServiceFactory is using a JaxwsPortProxy object to wrap the connection. Apache Pool2 requires each item in the pool to be unique via the equals method, but the JAX-WS port proxy object does not play well with this. So using a simple Groovy Proxy wrapper with UUID’s to id the objects can get around the problem.

Hopefully this shows how easy it is to pool web service connections in Groovy. It should also be noted that all of this is pure Groovy besides the simple helpers that the Grails BuildConfig and resources.groovy files provide. So you can use this pattern in any Groovy/Java project even if Grails is not your current framework of choice.

One thought on “Pooling Web Service Connections in Grails

  1. Manuel Vio says:

    Although I have never used JAX-WS services I’ve found your post very interesting and I will surely borrow some of you hints in my next Grails application. Thanks!

  2. Jeff Sheets says:

    Manuel, I’m glad that you found it useful, and of course the pooling concepts apply to any objects not just the JAX-WS that I was using in this example.

    Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*