blob: ff8607ba338de313211e284b4148895186224521 [file] [log] [blame]
<div class="wiki-content maincontent"><h1 id="SJSASwithGenericJMSRA-UsingActiveMQ,GenericJMSRAandSJSAS(Glassfish)">Using ActiveMQ, Generic JMS RA and SJSAS (Glassfish)</h1>
<p>This document is my notes on making ActiveMQ and SJSAS work together using GenericJMSRA. The objectives is to make ActiveMQ as the JMS provider and MDB can be deployed in SJSAS, listening messages from ActiveMQ. Please note that, the SJSAS version I use is 9.0 Update 1. I don't have time to test it with SJSAS 9.1 or Glassfish v2.</p>
<h2 id="SJSASwithGenericJMSRA-Theprocedures">The procedures</h2>
<p>First, download all binaries:</p>
<ul><li>genericra 1.7 (rar)</li><li>activemq 4.1.1 (zip or tar.gz)</li></ul>
<p>The activemq have some dependencies, however, you can easily found all of them in the distribution (the zip or tar.gz). The following is list of minimum dependencies:</p>
<ul><li>activemq-core</li><li>activeio</li><li>commons-logging</li><li>backport-util-concurrent</li></ul>
<p>In order to use genericra you need to first create a resource adapter config using asadmin (Command line tools of SJSAS).</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
asadmin create-resource-adapter-config
--property
SupportsXA=false
:RMPolicy=OnePerPhysicalConnection
:ProviderIntegrationMode=javabean
:ConnectionFactoryClassName=org.apache.activemq.ActiveMQConnectionFactory
:QueueConnectionFactoryClassName=org.apache.activemq.ActiveMQConnectionFactory
:TopicConnectionFactoryClassName=org.apache.activemq.ActiveMQConnectionFactory
:XAConnectionFactoryClassName=org.apache.activemq.ActiveMQXAConnectionFactory
:XAQueueConnectionFactoryClassName=org.apache.activemq.ActiveMQXAConnectionFactory
:XATopicConnectionFactoryClassName=org.apache.activemq.ActiveMQXAConnectionFactory
:UnifiedDestinationClassName=org.apache.activemq.command.ActiveMQDestination
:QueueClassName=org.apache.activemq.command.ActiveMQQueue
:TopicClassName=org.apache.activemq.command.ActiveMQTopic
:ConnectionFactoryProperties=brokerURL\\=tcp\\://127.0.0.1\\:61616
:LogLevel=FINE
myapp#genericra
</pre>
</div></div>
<p>You should note that, the above command should executed as one single line and no space around ':'. Just like:</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
asadmin create-resource-adapter-config --property SupportsXA=false:ConnectionFactoryProperties=brokerURL\\=tcp\\://127.0.0.1\\:61616 myapp#genericra
</pre>
</div></div>
<p>Under DOS prompt, you should use ONLY ONE '/' to do espcape, like this.</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
asadmin create-resource-adapter-config --property SupportsXA=false:ConnectionFactoryProperties=brokerURL\=tcp\://127.0.0.1\:61616 myapp#genericra
</pre>
</div></div>
<p>Package your application, genericra.rar and activemq dependencies in one single EAR. The structure of the EAR should like this:</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
lib/activemq-core-4.1.1.jar
lib/log4j-1.2.13.jar
lib/commons-logging-1.1.jar
lib/backport-util-concurrent-2.1.jar
lib/activeio-core-3.0.0-incubator.jar
META-INF/application.xml
genericra.rar
mymodules.jar
</pre>
</div></div>
<p>Inside the mymodules.jar (where I put my MDB), the sun-ejb-jar.xml should look like this:</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE sun-ejb-jar
PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 8.1 EJB 2.1//EN"
"http://www.sun.com/software/appserver/dtds/sun-ejb-jar_2_1-1.dtd"&gt;
&lt;sun-ejb-jar&gt;
&lt;enterprise-beans&gt;
&lt;ejb&gt;
&lt;ejb-name&gt;TestingMessageDrivenBean&lt;/ejb-name&gt;
&lt;mdb-connection-factory&gt;
&lt;jndi-name&gt;jms/SimpleQueueConnectionFactory&lt;/jndi-name&gt;
&lt;/mdb-connection-factory&gt;
&lt;mdb-resource-adapter&gt;
&lt;resource-adapter-mid&gt;myapp#genericra&lt;/resource-adapter-mid&gt;
&lt;activation-config&gt;
&lt;activation-config-property&gt;
&lt;activation-config-property-name&gt;DestinationType&lt;/activation-config-property-name&gt;
&lt;activation-config-property-value&gt;javax.jms.Queue&lt;/activation-config-property-value&gt;
&lt;/activation-config-property&gt;
&lt;activation-config-property&gt;
&lt;activation-config-property-name&gt;DestinationProperties&lt;/activation-config-property-name&gt;
&lt;activation-config-property-value&gt;PhysicalName=Foo.Bar&lt;/activation-config-property-value&gt;
&lt;/activation-config-property&gt;
&lt;/activation-config&gt;
&lt;/mdb-resource-adapter&gt;
&lt;/ejb&gt;
&lt;/enterprise-beans&gt;
&lt;/sun-ejb-jar&gt;
</pre>
</div></div>
<p>And the application.xml should look like this:</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;application
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd"
version="5"&gt;
&lt;description&gt;Example DD&lt;/description&gt;
&lt;display-name&gt;sample app with amq and genericra&lt;/display-name&gt;
&lt;module&gt;
&lt;ejb&gt;mymodules.jar&lt;/ejb&gt;
&lt;/module&gt;
&lt;module&gt;
&lt;connector&gt;genericra.rar&lt;/connector&gt;
&lt;/module&gt;
&lt;/application&gt;
</pre>
</div></div>
<p>Deploy the genericra:</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
asadmin deploy --name myapp myapp.ear
</pre>
</div></div>
<p>Create connection pool. Run the following command in one single line:</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
asadmin create-connector-connection-pool
--raname myapp#genericra
--connectiondefinition javax.jms.QueueConnectionFactory
--transactionsupport LocalTransaction
ActiveMQQueueConnectionFactoryPool
</pre>
</div></div>
<p>Create connection factory admin object. Run the following command in one single line, note that, "jms/SimpleQueueConnectionFactory" need to match in your sun-ejb-jar.xml:</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
asadmin create-connector-resource
--poolname ActiveMQQueueConnectionFactoryPool
jms/SimpleQueueConnectionFactory
</pre>
</div></div>
<h2 id="SJSASwithGenericJMSRA-Classloader/commons-logging+log4jissue">Classloader / commons-logging+log4j issue</h2>
<p>In the procedure shown above, you should notice one thing, ie, the RA, ActiveMQ and my MDB is deployed as one single EAR. You can deploy the genericra standalone without putting it into the ear, provided that you are not using commons-logging and log4j (directly or indirectly).</p>
<p>In the application I'm working on, commons-logging is used, and ActiveMQ also use commons-logging. In SJSAS, connector classloader is parent of the application classloader, as a result, if you deploy genericra independently, and then you deploy your application, log4j.xml will never get loaded except you put your application log4j.xml along with the ActiveMQ jars.</p>
<p>With Java standard classloading procedure, classloader will first delegate the loading to parent. When application classloader lookup the LogFactory class, it will first delegate to its parent classloader (ie the connector classloader). As the connector classloader have loaded ActiveMQ and its dependencies (which include commons-logging), LogFactory will finally loaded by the connector classloader. When commons-logging try to initialize the LogFactoryImpl, and hence trigger the standard procedure of log4j initialization, log4j configuration (log4j.xml or log4j.properties) in your application will not get loaded (as log4j logger is loaded by connector classloader).</p>
<p>As a result, deploy genericra independently is not the perfect solution in this case.</p>
<p>Luckily, with some advice from Glassfish forum (thanks Sivakumar), I worked out a better solution, ie, packaged all things (including genericra and activemq) into one EAR. When packaged in this way, all classes will be loaded under one classloader, and the good thing is, you can put your log4j.xml within one of the your jar (ie, the normal way!).</p>
<p>However, you should note one minor point, deploying genericra standalone and packaged with the EAR have one minor difference. If you deploy the RA with EAR, you need to reference the RA using the format 'appName#raName'. As a result, the reference name of the RA is myapp#genericra (see create-resource-adapter-config and sun-ejb-jar.xml part).</p>
<p>If you deploy the RA independently, the ra name is just genericra (as shown in the example by Ramesh).</p>
<h2 id="SJSASwithGenericJMSRA-LocalTransaction">LocalTransaction</h2>
<p>In the instruction described above, LocalTransaction is used. (SupportsXA=false and --transactionsupport LocalTransaction) as I am currently using Non-XA transaction, however, as example shown by Ramesh, XA should also work.</p>
<h2 id="SJSASwithGenericJMSRA-Configuringgenericra">Configuring genericra</h2>
<p>When you create the resource-adapter-config, there is two way to configure. One way is using JNDI, another way is using JavaBean introspection feature of genericra. In the example above, I go with JavaBean introspection feature (so 'ProviderIntegrationMode=javabean'). For more details on this, please checkout the user guide of genericra. Also, I found that expample provided on the genericra website is also valuable resource.</p>
<h2 id="SJSASwithGenericJMSRA-Reference">Reference</h2>
<ul><li><a shape="rect" class="external-link" href="http://weblogs.java.net/blog/rampsarathy/archive/2007/03/glassfish_v2_an_1.html" rel="nofollow">Another example on using genericjmsra and activemq by Ramesh</a></li><li><a shape="rect" class="external-link" href="https://genericjmsra.dev.java.net/docs/userguide/userguide.html" rel="nofollow">Official user guide on configuring genericjmsra</a></li><li><a shape="rect" class="external-link" href="http://forums.java.net/jive/thread.jspa?messageID=211849" rel="nofollow">The thread in Glassfish forum which I found my answer</a></li></ul></div>