Spring MDPs (Message-Driven-Pojos) provide excellent alternatives to MDBs (EJB specification), as we can enable asynchronous request handling capabilities to any tested business logic without defining container constraints or dependency hooks. There are three ways we could implement a Spring MDP:
(1) implementing javax.jms.MessageListener
(2) implementing org.springframework.jms.listener.SessionAwareMessageListener (which is a wrapper around javax.jms.MessageListener with javax.jms.Session) or
(3) any interface with implementation logic which accepts a string argument containing message data
This particular post tries to explore the third option of converting a tested business entity to handle asynchronous requests using Spring JMS (ServiceExporter bean) with ActiveMQ embedded broke, using a JMS Queuer.
1) Let us assume that our business entity interface and implementation are defined as below:
public interface MyBusinessEntity
{ public void doBusinessLogic(String logicParameters) throws MyBusinessException;
}
public class MyBusinessEntityImpl implements MyBusinessEntity
{ public void doBusinessLogic(String logicParameters) throws MyBusinessException
{ //using logicParameters, perform the business logic
}
}
2) The Spring application context should be defined to wire the embedded ActiveMQ broker with Spring JMS Service Exporter bean and the business entities as below:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:amq="http://activemq.apache.org/schema/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.2.0.xsd">
<!-- Defines the ActiveMQ embedded broker -->
<amq:broker useJmx="false" persistent="false">
<amq:transportConnectors>
<amq:transportConnector uri="tcp://localhost:0" />
</amq:transportConnectors>
</amq:broker>
<amq:queue id="qDestination" physicalName="embedded" />
<amq:connectionFactory id="connectionFactory" brokerURL="vm://localhost" />
<bean id="mybusinessService" class="org.springframework.jms.remoting.JmsInvokerServiceExporter">
<property name="serviceInterface" value="mypackage.MyBusinessEntity"/>
<property name="service">
<bean class="mypackage.MyBusinessEntityImpl"/>
</property>
</bean>
<bean class="org.springframework.jms.listener.SimpleMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destination" ref="qDestination"/>
<property name="messageListener" ref="placeBidService"/>
</bean>
</beans>
3) If the application is a web-archive (war), add the Spring ContextLoader Listener to load the spring Context to your WEB-INF/web.xml.
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:jms-context.xml</param-value>
</context-param>
4) In order to asynchronously place a request, from the client, you need to define another client Spring context configuration mapping containing the reference to MyBusinessEntrity interface as given below, which could be used directly to place a request.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:amq="http://activemq.apache.org/schema/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.2.0.xsd">
<bean id="mybusinessService" class="org.springframework.jms.remoting.JmsInvokerProxyFactoryBean">
<property name="serviceInterface" value="mypackage.MyBusinessEntity"/>
<property name="connectionFactory" ref="connectionFactory"/>
<property name="queue" ref="qDestination"/>
</bean>
<amq:queue id="qDestination" physicalName="embedded" />
<amq:connectionFactory id="connectionFactory" brokerURL="vm://localhost" />
</beans>
Having setup the server and client logics, the client can asynchronously lodge a request on server for the specific business logic.