<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Technotrance, Illusions and Perspectives &#187; spring-jms</title>
	<atom:link href="http://myblog.shriharisc.com/tag/spring-jms/feed/" rel="self" type="application/rss+xml" />
	<link>http://myblog.shriharisc.com</link>
	<description>A dose of everyday bruises with Java/JEE</description>
	<lastBuildDate>Mon, 02 Aug 2010 17:45:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creating asynchronous service using Spring JMS (ServiceExporter) and ActiveMQ</title>
		<link>http://myblog.shriharisc.com/2009/10/02/creating-asynchronous-service-using-spring-jms-serviceexporter-and-activemq/</link>
		<comments>http://myblog.shriharisc.com/2009/10/02/creating-asynchronous-service-using-spring-jms-serviceexporter-and-activemq/#comments</comments>
		<pubDate>Thu, 01 Oct 2009 18:57:07 +0000</pubDate>
		<dc:creator>Shrihari</dc:creator>
				<category><![CDATA[jee-light]]></category>
		<category><![CDATA[activemq]]></category>
		<category><![CDATA[spring-jms]]></category>

		<guid isPermaLink="false">http://schakrap.wordpress.com/?p=123</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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 <a href="http://www.infoq.com/articles/richards-jms2">three ways</a> we could implement a Spring MDP:</p>
<p>(1) implementing javax.jms.MessageListener<br />
(2) implementing org.springframework.jms.listener.SessionAwareMessageListener (which is a wrapper around javax.jms.MessageListener with javax.jms.Session) or<br />
(3) any interface with implementation logic which accepts a string argument containing message data</p>
<p>This particular post tries to explore the third option of converting a tested business entity to handle asynchronous requests using <a href="http://static.springsource.org/spring/docs/2.5.x/reference/remoting.html">Spring JMS (ServiceExporter bean)</a> with ActiveMQ embedded broke, using a JMS Queuer.</p>
<p>1) Let us assume that our business entity interface and implementation are defined as below:</p>
<pre class="brush: java">
public interface MyBusinessEntity
{      public void doBusinessLogic(String logicParameters) throws MyBusinessException;
}
</pre>
<pre class="brush: java">
public class MyBusinessEntityImpl implements MyBusinessEntity
{      public void doBusinessLogic(String logicParameters) throws MyBusinessException
       {       //using logicParameters, perform the business logic
       }
}
</pre>
<p>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:</p>
<pre class="brush: xml">
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xmlns:amq=&quot;http://activemq.apache.org/schema/core&quot;
        xsi:schemaLocation=&quot;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&quot;&gt;

     &lt;!-- Defines the ActiveMQ embedded  broker --&gt;
    &lt;amq:broker useJmx=&quot;false&quot; persistent=&quot;false&quot;&gt;
        &lt;amq:transportConnectors&gt;
                 &lt;amq:transportConnector uri=&quot;tcp://localhost:0&quot; /&gt;
        &lt;/amq:transportConnectors&gt;
      &lt;/amq:broker&gt;

     &lt;amq:queue id=&quot;qDestination&quot; physicalName=&quot;embedded&quot; /&gt;

    &lt;amq:connectionFactory id=&quot;connectionFactory&quot; brokerURL=&quot;vm://localhost&quot; /&gt;

   &lt;bean id=&quot;mybusinessService&quot; class=&quot;org.springframework.jms.remoting.JmsInvokerServiceExporter&quot;&gt;
     &lt;property name=&quot;serviceInterface&quot; value=&quot;mypackage.MyBusinessEntity&quot;/&gt;
     &lt;property name=&quot;service&quot;&gt;
            &lt;bean class=&quot;mypackage.MyBusinessEntityImpl&quot;/&gt;
     &lt;/property&gt;
   &lt;/bean&gt;

   &lt;bean class=&quot;org.springframework.jms.listener.SimpleMessageListenerContainer&quot;&gt;
       &lt;property name=&quot;connectionFactory&quot; ref=&quot;connectionFactory&quot;/&gt;
       &lt;property name=&quot;destination&quot; ref=&quot;qDestination&quot;/&gt;
       &lt;property name=&quot;messageListener&quot; ref=&quot;placeBidService&quot;/&gt;
   &lt;/bean&gt;

&lt;/beans&gt;
</pre>
<p>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.</p>
<pre class="brush: xml">
&lt;listener&gt;
     &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
   &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
         &lt;param-value&gt;classpath:jms-context.xml&lt;/param-value&gt;
   &lt;/context-param&gt;
</pre>
<p>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.</p>
<pre class="brush: xml">
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
      xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
      xmlns:amq=&quot;http://activemq.apache.org/schema/core&quot;
      xsi:schemaLocation=&quot;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&quot;&gt;

 &lt;bean id=&quot;mybusinessService&quot; class=&quot;org.springframework.jms.remoting.JmsInvokerProxyFactoryBean&quot;&gt;
      &lt;property name=&quot;serviceInterface&quot; value=&quot;mypackage.MyBusinessEntity&quot;/&gt;
      &lt;property name=&quot;connectionFactory&quot; ref=&quot;connectionFactory&quot;/&gt;
      &lt;property name=&quot;queue&quot; ref=&quot;qDestination&quot;/&gt;
 &lt;/bean&gt;

 &lt;amq:queue id=&quot;qDestination&quot; physicalName=&quot;embedded&quot; /&gt;
 &lt;amq:connectionFactory id=&quot;connectionFactory&quot; brokerURL=&quot;vm://localhost&quot; /&gt;
&lt;/beans&gt;
</pre>
<p>Having setup the server and client logics, the client can asynchronously lodge a request on server for the specific business logic.</p>
]]></content:encoded>
			<wfw:commentRss>http://myblog.shriharisc.com/2009/10/02/creating-asynchronous-service-using-spring-jms-serviceexporter-and-activemq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
