Spring Integration: Whats going on in there? Part 1 Esper wire tap
February 24th, 2010
Earlier this week Russ Miles announced the release of the OpenCredo Esper extension project (read it here). This project is something we created primarily to make using Esper, the open source Complex Event Processing framework, in conjunction with Spring Integration really easy. At OpenCredo we have been using Esper to help solve the problem of comprehending and monitoring the state of messaging applications for a while now. Understanding the state of an application is one of the issues we repeatedly see with clients looking to adopt an Event Driven Architecture based on Spring Integration. Esper allows us to create views of the data as it passes through our asynchronous event based application which helps to eliminate some of the complexity created by breaking our application into a series of loosely coupled components.
In the first post on this topic I will be walking you through a simple example that takes an existing Spring Integration application and shows how to use the Esper wire tap to send messages passing over a Spring Integration channel into Esper.
In the next post in the series I will show how you can hook Spring Integration into Esper to have events trigger the creation of Spring Integration messages.
Sample Application
Our sample application consists of a Spring Integration message pipeline that is connected to a HR system. Whenever the HR process successfully hires a new employee at our fictitious company the HR system creates a notification message that allows other systems to take appropriate actions to deal with the new hire.
To hide the fact that we are using Spring Integration from the HR system we use a Spring Integration gateway which is injected into the HR system. The SI gateway creates a proxy implementation of the configured interface, shown below, which is then injected into HR System.
public void employementConfirmed(EmployeeConfirmation confirmation);
}
For testing purposes we expose the HR system as a JMX bean with the two methods we use to instruct HR to hire new developers or lawyers exposed as managed operations.
public class HrSystem {
private final AtomicLong employeeNumber = new AtomicLong(0);
private final EmployeeConfirmationNotifier confirmationNotifier;
public HrSystem(EmployeeConfirmationNotifier confirmationNotifier) {
this.confirmationNotifier = confirmationNotifier;
}
@ManagedOperation
public void addMoreLawyers(int count){
for(int i = 0 ; i < count ; i++){
//do some hiring
this.confirmationNotifier
.employementConfirmed(
new EmployeeConfirmation(
this.employeeNumber.getAndIncrement(), "Lawyer"));
}
}
@ManagedOperation
public void addMoreDevelopers(int count){
for(int i = 0 ; i < count ; i++){
//do some hiring
this.confirmationNotifier
.employementConfirmed(
new EmployeeConfirmation(
this.employeeNumber.getAndIncrement(), "Developer"));
}
}
}
Then simply add the MBean export to the configuration file.
Adding in the wire tap
The OpenCredo Esper Extension project provides a namespace for configuring the wire tap. All that is required is to use the wire-tap element from the si-esper namespace. An example of the esper wire tap is shown below:
template-ref="defaultEsperTemplate" sourceId="hrSystem"
post-send="true" send-context="false"/>
<si-esper:wire-tap-channels default-wire-tap="defaultWireTap">
<si-esper:channel pattern="confirmedEmployeesChannel" />
</si-esper:wire-tap-channels>
<esper:template id="defaultEsperTemplate">
<esper:statements />
</esper:template>
Here we have configured a wire tap to publish an event to Esper each time a message is sent to the confirmedEmployeesChannel channel. By setting send-context to false the published event will simply be the payload of the Spring Integration message, in this case an instance of EmployeeConfirmation. If send-context is set to true, the default, then additional context such as the channel and the sourceId will be included in the event published to Esper.
That’s all that is required to make the data passing available through Spring Integration channels available to Esper.
Next time
In the next post I will show how to then use Esper to query that flow of data allowing us to better understand what is happening at runtime and to alert us to interesting conditions in our application. The next post will also include the runnable source code for the example.
Filed under: Code Sample, Spring Integration