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.

About the Author

Jeff Sheets profile.

Jeff Sheets

VP - Technology

Jeff has developed Java, Groovy, Grails, and Javascript web apps for industries as varied as Defense, Energy, Weather, Insurance, and Telecom. He is a co-organizer of the Omaha Java Users Group. Jeff has worked on Grails projects since the Grails 1.3.x days, and has experience with production Groovy code as well as Spock tests and Gradle builds. His latest focus has been on AngularJS and Spring Boot applications using JHipster. Jeff also enjoys volunteering at local CoderDojo events to teach programming to our next generation.

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 *

Related Blog Posts
Up to Spec: JavaScript Numeric Separators
Let's take a look at the proposal to add Numeric Separators to the JavaScript specification.
Using Conftest to Validate Configuration Files
Conftest is a utility within the Open Policy Agent ecosystem that helps simplify writing validation tests against configuration files. In a previous blog post, I wrote about using the Open Policy Agent utility directly to […]
SwiftGen with Image & Color Asset Catalogs
You might remember back in 2015 when iOS 9 was introduced, and we were finally given a way to manage all of our assets in one place with Asset Catalogs. A few years later, support […]
Tracking Original URL Through Authentication
If you read my other post about refreshing AWS tokens, then you probably have a use case for keeping track of the original requested resource while the user goes through authentication so you can route […]