| <div class="wiki-content maincontent"> |
| |
| |
| <h2><structured-macro ac:macro-id="3a33b9bc-bf20-4880-8bf3-5bf1c2c71b46" ac:name="excerpt" ac:schema-version="1"><parameter ac:name="atlassian-macro-output-type">INLINE</parameter><rich-text-body><p>Configuring a Session Bean to send messages to ActiveMQ</p></rich-text-body></structured-macro></h2> |
| <p>In the attached <link><attachment ri:filename="activemq-jboss-test.zip"><page ri:content-title="JBoss Integration"></page></attachment><link-body>example application</link-body></link>, the three MDBs use the <code>SenderEJB</code> to send JMS messages to an ActiveMQ queue. In this example, I will be explaining how to:</p> |
| <ol><li>Configure and deploy an ActiveMQ <code>Queue</code> to JBoss</li><li>Configure and deploy an ActiveMQ <code>QueueConnectionFactory</code> to JBoss</li><li>Configure an EJB, deployed to JBoss, to reference the above two.</li></ol> |
| |
| |
| <h3>The Bean</h3> |
| <p>In the <link><attachment ri:filename="ejb-jar.xml"><page ri:content-title="JBoss Integration"></page></attachment><link-body>ejb-jar.xml</link-body></link> deployment descriptor, the <code>SenderEJB</code> is declared as follows:<br clear="none"> |
| <structured-macro ac:macro-id="6286c3f3-10c6-4372-adf8-e9ff10eb6708" ac:name="anchor" ac:schema-version="1"><parameter ac:name="">ejbSenderEJB</parameter></structured-macro></p> |
| <structured-macro ac:macro-id="8609e156-1e6f-45ea-a91a-f25204627979" ac:name="code" ac:schema-version="1"><parameter ac:name="">xml</parameter><parameter ac:name="title">ejb-jar.xml -- session bean declaration</parameter><plain-text-body> |
| <session> |
| ... |
| <ejb-name>SenderEJB</ejb-name> |
| ... |
| <ejb-class>com.panacya.platform.service.bus.sender.SenderBean</ejb-class> |
| ... |
| <resource-ref> |
| <res-ref-name>jms/MyQueueConnectionFactory</res-ref-name> |
| <res-type>javax.jms.QueueConnectionFactory</res-type> |
| ... |
| </resource-ref> |
| <message-destination-ref> |
| <message-destination-ref-name>jms/LogQueue</message-destination-ref-name> |
| <message-destination-type>javax.jms.Queue</message-destination-type> |
| ... |
| <message-destination-link>LoggingQueue</message-destination-link> |
| </message-destination-ref> |
| </session> |
| </plain-text-body></structured-macro> |
| |
| <p>The <code>jms/MyQueueConnectionFactory</code> is the JNDI name the <code>SenderEJB</code> will use to lookup a <code>javax.jms.QueueConnectionFactory</code>. We will configure it to point to an ActiveMQ <code>QueueConnectionFactory</code>.</p> |
| |
| <p>The <code>jms/LogQueue</code> is the JNDI name the <code>SenderEJB</code> will use to lookup the <code>javax.jms.Queue</code> it will send messages to. We use the <code>message-destination-link</code> element to refer to the <code>LoggingQueue</code> which is declared in the <code>assembly-descriptor</code> section of the <link><attachment ri:filename="ejb-jar.xml"><page ri:content-title="JBoss Integration"></page></attachment><link-body>ejb-jar.xml</link-body></link> deployment descriptor as: <br clear="none"> |
| <structured-macro ac:macro-id="efc34da6-ef9e-4ee3-9aa6-3cbd1be026ae" ac:name="anchor" ac:schema-version="1"><parameter ac:name="">ejbLoggingQueue</parameter></structured-macro></p> |
| <structured-macro ac:macro-id="46387c53-07d5-4288-bd88-fb68e51494b9" ac:name="code" ac:schema-version="1"><parameter ac:name="">xml</parameter><parameter ac:name="title">ejb-jar.xml -- assembly descriptor section</parameter><plain-text-body> |
| <assembly-descriptor> |
| ... |
| <message-destination> |
| <message-destination-name>LoggingQueue</message-destination-name> |
| </message-destination> |
| ... |
| </assembly-descriptor> |
| </plain-text-body></structured-macro> |
| |
| <p>This is a standard EJB deployment descriptor, nothing special. </p> |
| |
| <h3>The Connector</h3> |
| <p>The <code>resource-ref</code> element <link ac:anchor="ejbSenderEJB" ac:tooltip="SenderEJB snippet from the ejb-jar.xml file"><link-body>shown above</link-body></link>, will be linked to the following element in the <a shape="rect" href="http://activemq.codehaus.org/checkout/activemq/modules/ra/src/rar/META-INF/ra.xml">ra.xml</a> file, which is contained within the <link ac:anchor="rarfile" ac:tooltip="The ActiveMQ JCA"><page ri:content-title="JBoss Integration"></page><link-body>activemq-ra-1.2.rar</link-body></link> file:<br clear="none"> |
| <structured-macro ac:macro-id="5409f0be-9202-4bbf-baba-d9923ee9daa9" ac:name="anchor" ac:schema-version="1"><parameter ac:name="">raQueueConnectionFactory</parameter></structured-macro></p> |
| <structured-macro ac:macro-id="6c2e648d-24cd-4fa5-8e26-ec2eb2b65ca3" ac:name="code" ac:schema-version="1"><parameter ac:name="">xml</parameter><parameter ac:name="title">ra.xml -- The QueueConnectionFactory</parameter><plain-text-body> |
| <outbound-resourceadapter> |
| ... |
| <connection-definition> |
| ... |
| <connectionfactory-interface>javax.jms.QueueConnectionFactory</connectionfactory-interface> |
| <connectionfactory-impl-class>org.activemq.ra.ActiveMQConnectionFactory</connectionfactory-impl-class> |
| <connection-interface>javax.jms.QueueConnection</connection-interface> |
| ... |
| </connection-definition> |
| ... |
| </outbound-resourceadapter> |
| </plain-text-body></structured-macro> |
| |
| <p>The <code>message-destination</code> element <link ac:anchor="ejbLoggingQueue" ac:tooltip="LoggingQueue snippet from the ejb-jar.xml file"><link-body>shown above</link-body></link>, will be linked to the following element in the <a shape="rect" href="http://activemq.codehaus.org/checkout/activemq/modules/ra/src/rar/META-INF/ra.xml">ra.xml</a> file:<br clear="none"> |
| <structured-macro ac:macro-id="fc2f3d98-3563-4fbe-9f73-8bb775085f39" ac:name="anchor" ac:schema-version="1"><parameter ac:name="">raQueue</parameter></structured-macro></p> |
| <structured-macro ac:macro-id="bd6b04f5-9b2d-4a75-8129-9d85553185dc" ac:name="code" ac:schema-version="1"><parameter ac:name="">xml</parameter><parameter ac:name="title">ra.xml -- The Queue</parameter><plain-text-body> |
| <adminobject> |
| <adminobject-interface>javax.jms.Queue</adminobject-interface> |
| <adminobject-class>org.activemq.message.ActiveMQQueue</adminobject-class> |
| <config-property> |
| <config-property-name>PhysicalName</config-property-name> |
| <config-property-type>java.lang.String</config-property-type> |
| </config-property> |
| </adminobject> |
| </plain-text-body></structured-macro> |
| |
| <h3>The Glue</h3> |
| <p>In JBoss, connecting the resources needed by the <link><attachment ri:filename="ejb-jar.xml"><page ri:content-title="JBoss Integration"></page></attachment><link-body>ejb-jar.xml</link-body></link> file to resources provided by the <a shape="rect" href="http://activemq.codehaus.org/checkout/activemq/modules/ra/src/rar/META-INF/ra.xml">ra.xml</a> file involves two additional files:</p> |
| <ol><li><strong><link><attachment ri:filename="panacya-jms-ds.xml"><page ri:content-title="JBoss Integration"></page></attachment><link-body>panacya-jms-ds.xml</link-body></link></strong> - This is a JBoss data source file. It specifies which connector objects JBoss should instantiate and where in JNDI JBoss should place those objects.</li><li><strong><link><attachment ri:filename="jboss.xml"><page ri:content-title="JBoss Integration"></page></attachment><link-body>jboss.xml</link-body></link></strong> - This is a JBoss deployment descriptor which is contained within the <link ac:anchor="ejbjarfile" ac:tooltip="The standalone ejb module"><page ri:content-title="JBoss Integration"></page><link-body>panacya-mdb-test-1.0.jar</link-body></link> file. It links resources needed by the EJBs to the JNDI names of resources available in JBoss.</li></ol> |
| |
| |
| <h5><link><attachment ri:filename="panacya-jms-ds.xml"><page ri:content-title="JBoss Integration"></page></attachment><link-body>panacya-jms-ds.xml</link-body></link> – <em>The JBoss Data Source File</em></h5> |
| <p>This first snippet configures the <code>QueueConnectionFactory</code>, <link ac:anchor="raQueueConnectionFactory" ac:tooltip="QueueConnectionFactory snippet from the ra.xml file"><link-body>declared above</link-body></link>, and places it in JNDI at <code>activemq/QueueConnectionFactory</code>:<br clear="none"> |
| <structured-macro ac:macro-id="87a71a3f-a15e-41c0-b4a3-dd676a71cbab" ac:name="anchor" ac:schema-version="1"><parameter ac:name="">dsQueueConnectionFactory</parameter></structured-macro></p> |
| <structured-macro ac:macro-id="fdd69607-0a13-4e05-9c68-6e4d70d1c66c" ac:name="code" ac:schema-version="1"><parameter ac:name="">xml</parameter><parameter ac:name="title">panacya-jms-ds.xml -- The QueueConnectionFactory</parameter><plain-text-body> |
| <tx-connection-factory> |
| <jndi-name>activemq/QueueConnectionFactory</jndi-name> |
| <xa-transaction/> |
| <rar-name>activemq-ra-1.2-SNAPSHOT.rar</rar-name> |
| <connection-definition>javax.jms.QueueConnectionFactory</connection-definition> |
| <security-domain-and-application>JmsXARealm</security-domain-and-application> |
| </tx-connection-factory> |
| </plain-text-body></structured-macro> |
| |
| <p>This second snippet configures the <code>Queue</code>, <link ac:anchor="raQueue" ac:tooltip="Queue snippet from the ra.xml file"><link-body>declared above</link-body></link>, and places it in JNDI at <code>activemq/queue/outbound</code>:<br clear="none"> |
| <structured-macro ac:macro-id="cb602599-8c3b-4e04-bafa-a334d512979e" ac:name="anchor" ac:schema-version="1"><parameter ac:name="">dsQueue</parameter></structured-macro></p> |
| <structured-macro ac:macro-id="6fa55612-7e41-4c0c-93b1-9ad345726635" ac:name="code" ac:schema-version="1"><parameter ac:name="">xml</parameter><parameter ac:name="title">panacya-jms-ds.xml -- The Queue</parameter><plain-text-body> |
| <mbean code="org.jboss.resource.deployment.AdminObject" name="activemq.queue:name=outboundQueue"> |
| <attribute name="JNDIName">activemq/queue/outbound</attribute> |
| <depends optional-attribute-name="RARName">jboss.jca:service=RARDeployment,name='activemq-ra-1.2-SNAPSHOT.rar'</depends> |
| <attribute name="Type">javax.jms.Queue</attribute> |
| <attribute name="Properties"> |
| PhysicalName=queue.outbound |
| </attribute> |
| </mbean> |
| </plain-text-body></structured-macro> |
| |
| <p>In the <link><attachment ri:filename="panacya-jms-ds.xml"><page ri:content-title="JBoss Integration"></page></attachment><link-body>panacya-jms-ds.xml</link-body></link> file section shown above, the value of the <code>Properties</code> element is set to <code>PhysicalName=queue.outbound</code>. This value is the physical name of the ActiveMQ destination the <code>SenderEJB</code> will be sending messages to and not a JNDI name. In other words, the value of the <code>PhysicalName</code> property has no meaning to JBoss. It is purely an ActiveMQ setting. </p> |
| |
| <h5><link><attachment ri:filename="jboss.xml"><page ri:content-title="JBoss Integration"></page></attachment><link-body>jboss.xml</link-body></link> – <em>The JBoss Deployment Descriptor</em></h5> |
| <p>This first snippet links the <code><link ac:anchor="ejbSenderEJB" ac:tooltip="SenderEJB snippet from the ejb-jar.xml file"><link-body>jms/MyQueueConnectionFactory</link-body></link></code> JNDI name used by the <code>SenderEJB</code> to the resource name <code>queuefactoryref</code> which is local to the <link><attachment ri:filename="jboss.xml"><page ri:content-title="JBoss Integration"></page></attachment><link-body>jboss.xml</link-body></link> file: <br clear="none"> |
| <structured-macro ac:macro-id="c59f1670-7b5b-4d78-b7b8-b8a362108c0d" ac:name="anchor" ac:schema-version="1"><parameter ac:name="">jbossQueuefactoryref</parameter></structured-macro></p> |
| <structured-macro ac:macro-id="82a93989-89ad-4c6a-8345-50948b89d7b4" ac:name="code" ac:schema-version="1"><parameter ac:name="">xml</parameter><parameter ac:name="title">jboss.xml -- The QueueConnectionFactory for the SenderEJB</parameter><plain-text-body> |
| <enterprise-beans> |
| <session> |
| <ejb-name>SenderEJB</ejb-name> |
| <resource-ref> |
| <res-ref-name>jms/MyQueueConnectionFactory</res-ref-name> |
| <resource-name>queuefactoryref</resource-name> |
| </resource-ref> |
| </session> |
| ... |
| </enterprise-beans> |
| </plain-text-body></structured-macro> |
| |
| <p>This second snippet links the local <code>queuefactoryref</code> name to the global JNDI name <code>java:/activemq/QueueConnectionFactory</code> which was <link ac:anchor="dsQueueConnectionFactory" ac:tooltip="QueueConnectionFactory snippet from the panacya-jms-ds.xml file"><link-body>declared above</link-body></link>: <br clear="none"> |
| <structured-macro ac:macro-id="3c777a05-e82d-4669-8804-056b11f5ba25" ac:name="anchor" ac:schema-version="1"><parameter ac:name="">jbossQueueConnectionFactory</parameter></structured-macro></p> |
| <structured-macro ac:macro-id="25909147-bec4-46d0-a03a-dda5f2ccb828" ac:name="code" ac:schema-version="1"><parameter ac:name="">xml</parameter><parameter ac:name="title">jboss.xml -- Linking queuefactoryref to the global JNDI namespace</parameter><plain-text-body> |
| <resource-managers> |
| <resource-manager> |
| <res-name>queuefactoryref</res-name> |
| <res-jndi-name>java:/activemq/QueueConnectionFactory</res-jndi-name> |
| </resource-manager> |
| ... |
| </resource-managers> |
| </plain-text-body></structured-macro> |
| |
| <p>This third snippet links the <code>LoggingQueue</code>, which was <link ac:anchor="ejbLoggingQueue" ac:tooltip="LoggingQueue snippet from the ejb-jar.xml file"><link-body>declared</link-body></link> in the <code>assembly-descriptor</code> section of the <link><attachment ri:filename="ejb-jar.xml"><page ri:content-title="JBoss Integration"></page></attachment><link-body>ejb-jar.xml</link-body></link>, to the global JNDI name <code>activemq/queue/outbound</code> which was <link ac:anchor="dsQueue" ac:tooltip="Queue snippet from the panacya-jms-ds.xml file"><link-body>declared above</link-body></link>: <br clear="none"> |
| <structured-macro ac:macro-id="2831eeb9-d124-4718-9caa-a1eb11f8a233" ac:name="anchor" ac:schema-version="1"><parameter ac:name="">jbossQueue</parameter></structured-macro></p> |
| <structured-macro ac:macro-id="eab4bd66-e286-4a07-a4ac-79c62efdf353" ac:name="code" ac:schema-version="1"><parameter ac:name="">xml</parameter><parameter ac:name="title">jboss.xml -- Linking LoggingQueue to the global JNDI namespace</parameter><plain-text-body> |
| <assembly-descriptor> |
| <message-destination> |
| <message-destination-name>LoggingQueue</message-destination-name> |
| <jndi-name>activemq/queue/outbound</jndi-name> |
| </message-destination> |
| </assembly-descriptor> |
| </plain-text-body></structured-macro> |
| |
| <p>The above example highlights the key configuration settings needed to enable EJBs deployed in JBoss to send JMS messages to an ActiveMQ destination. </p> |
| |
| <p>You can try the above example, plus a few more, by downloading the <link><attachment ri:filename="activemq-jboss-test.zip"><page ri:content-title="JBoss Integration"></page></attachment><link-body>activemq-jboss-test.zip</link-body></link> file which contains the complete sample project.</p></div> |
| |