Using Spring Integration for creating scalable distributed services backed by JMS (part 2)
4 minute read
In part 1 of this series, we explored how Spring Integration allows us to easily leverage some of the Enterprise Integration Patterns to deploy salable services. In this part we’ll take a look at one more pattern that is very helpful, the claim check pattern. This patterns allows to store parts of message, to be retrieved at a later stage. This is illustrated on the following diagram:
In practice, this pattern allows us to avoid having to send very large messages across the JMS broker. Message brokers are designed to handle many small messages, and performance tends to degrade with larger messages. While there are ways of tweaking the different JMS brokers to work better in these kinds of workloads, the “Claim Check” pattern allows us to sidestep the issue entirely, by using a separate storage mechanism to hold parts of our messages. So how big is too big for a message? That’s a very subjective matter, but good common sense will tell us that for example most large binary data should not be transferred through a JMS broker.
Luckily Spring Integration provides out of the box support for this pattern. In fact, we can use the pattern without making any changes to our code. We do require some extra libraries, which we have added to the pom.xml file:
We have added:
The Spring Integration JDBC jar. This provides, among other functionality, a JDBC backed message store.
H2, a Java embedded database. In this database we will store our messages.
Apache Commons DBCP, a connection pool to manage our JDBC connections.
Now let’s look at the spring-int-server.xml file:
Now let’s look at the changes:
We have an in memory instance of an H2 database. H2 is a very useful embedded database, which means that we can run it inside of our JVM process. No need to have a separate database server for this example. Both the server and the client will connect to this instance. Obviously in production you will choose something more robust. But for this example, this setup works well. It’s worth noting that the H2 is started with the configuration options needed to accept outside connections over TCP.
A datasource is created. This datasource will point to the database server created in point #1.
A data base initialization element. Spring Integration requires some tables to be created in the database that will be used as a message store. Since our database will reside in memory, this will be run every time the server is started up.
The message store we’re using is a JDBC store, using the data source defined in #2.
We now introduce the chain element. The chain element allows multiple filter or transformations to be chained together. In this case we’re still delegating to the orderServiceHandler bean for processing as the first step in the chain.
In the second step of the chain, we check in the message. This transformed will take the message (which must be serializable ), store in the message store, and return a UUID (Universally Unique Identifier). This UUID is sent to the client.
Now let’s look at the client side. The client side is very similar to server (actually a bit simpler).
Let’s look at the changes in this file:
A data source is created, using the same URL as in the server side. There’s no need to start up the H2 server here, since we’re using the H2 running inside the server process.
A message store is created using the data source created in #1.
A chain is created to handle messages in the requestChannel. This chain will receive a UUID, and retrieve the corresponding object from the message store.
You will see the exact same output as in part 1. While that might seem anti-climatic, the important aspect is what is happening behind the scenes. The message is being stored in a database, and a UUID is sent across the wire. What we have implemented is a very simple Enterprise Service Bus. There’s plenty of opportunities to use patterns like this.