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
Why we started using JSON Schema in our internal Helm charts
Helm 3 supports validating the provided chart values against a JSON Schema. While it may be quicker to get started in your chart development without a schema, we found it valuable for a number of […]
Rewriting files in Google Cloud Storage
Rewriting Files in GCP Note: even though this code is in Python, this should be the same idea in JavaScript, Go, etc. I wrote the following to copy a file from one Google Cloud Storage […]
Interpreting Spatial Data in the Age of COVID-19
As 2020 has come to an end, many are eager to leave the mess of COVID-19 behind with the new year and gain a fresh start. Unfortunately, new cases are still soaring across the United […]
Building a Better Mousetrap
Recently, my daughter (age 6) was into building “mousetraps” out of shoe boxes. These were more or less comfortable cardboard mouse houses complete with beds, rooms, everything a mouse could want or need and not […]