activemq-artemis/examples/javaee/jca-config/readme.html

109 lines
5.4 KiB
HTML

<html>
<head>
<title>HornetQ Java EE JCA Configuration Example</title>
<link rel="stylesheet" type="text/css" href="../../common/common.css" />
<link rel="stylesheet" type="text/css" href="../../common/prettify.css" />
<script type="text/javascript" src="../../common/prettify.js"></script>
</head>
<body onload="prettyPrint()">
<h1>Java EE Resource Adapter Configuration Example</h1>
<p>This example demonstrates how to configure several properties on the HornetQ Resource Adapter. We setup two
WildFly. The enterprise application is being deployed in one application server while the MDBs and JMS
Connections are pointing to a remote server</p>
<p>This example is composed of two message-driven beans (MDB), MDBQueueA and MDBQueueB, and a stateless session
bean StatelessSender, and a main class MDBRemoteClientExample.<p>
<p>MDBRemoteClientExample will invoke the StatelessSender bean on the second server which will in turn send a
message to 2 queues on the first server which will then be consumed by each MDB.</p>
<p>MDBQueueA consumes the message locally.</p>
<p>MDBQueueB is on the second server who's JCA Adapter is configured to consume remotely from the first server</p>
<h2>WildFly configuration</h2>
<p>The example leverages the Arquillian framework to run multiple WildFly instances and deploy the MDBs and SLSB.</p>
<h2>Example step-by-step</h2>
<p><i>download WildFly 8.0.0.Final from <a href="http://wildfly.org/downloads/">here</a> and install.</i></p>
<p><i>set the JBOSS_HOME property to point to the WildFly install directory</i></p>
<p><i>type <code>mvn verify</code> from the example directory to run</i></p>
<ol>
<li>First we need to get an initial context so we can look-up the EJB. This initial context will get it's properties from the jboss-ejb-client.properties.</li>
<pre class="prettyprint">
Properties env = new Properties();
env.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
initialContext = new InitialContext(env);
</pre>
<li>Getting a reference to the Stateless Bean</li>
<pre class="prettyprint">
StatelessSenderService sender = (StatelessSenderService)initialContext.lookup("ejb:/test//StatelessSender!org.apache.activemq.javaee.example.server2.StatelessSenderService");
</pre>
<li>Calling the Stateless Bean</li>
<pre class="prettyprint">
sender.sendHello("Hello there MDB!");
</pre>
<li>On the second server we invoke the EJB StatelessSender: This will send 2 messages to server 1 using the configured outbound adapter</li>
<pre class="prettyprint">
Queue destQueueA = HornetQJMSClient.createQueue("A");
Queue destQueueB = HornetQJMSClient.createQueue("B");
</pre>
<li>Create a connection to a remote server using a connection-factory we injected. JCA will actually manage this through a <i>connection pool</i></li>
<pre class="prettyprint">
Connection conn = connectionFactory.createConnection("guest", "password");
</pre>
<li>Send a message to a QueueA on server1, which will be received by MDBQueueA on server1</li>
<pre class="prettyprint">
Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer prodA = sess.createProducer(destQueueA);
prod.send(sess.createTextMessage(message));
</pre>
<li>Send a message to a QueueB on the remote server1, which will be received by MDBQueueA on server2</li>
<pre class="prettyprint">
MessageProducer prodB = sess.createProducer(destQueueB);
prodB.send(sess.createTextMessage(message));
</pre>
<li>Close the connection. (Since this is a JCA connection, this close will return the connection back to the connection pool)</li>
<pre class="prettyprint">
conn.close();
</pre>
<li>The message is received by MDBQueueA on server1 which consumed from a local queue</li>
<pre class="prettyprint">
@MessageDriven(name = "MDB_QueueA",
activationConfig =
{
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/A")
})
public class MDBQueueA implements MessageListener
{
public void onMessage(Message message)...
</pre>
<li>Another message is received on MDBQueueB on server2 which consumed from a remote queue on server1.
<pre class="prettyprint">
@MessageDriven(name = "MDB_QueueB",
activationConfig =
{
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "B"),
@ActivationConfigProperty(propertyName = "useJNDI", propertyValue = "false")
})
@ResourceAdapter("hornetq-ra-remote.rar")
public class MDBQueueB implements MessageListener
{
public void onMessage(Message message)...
</pre>
</ol>
</body>
</html>