<html>
<head>
<title>HornetQ Java EE JCA Remote Server 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 JCA Resource Adapter Remote Server Configuration Example</h1>
<p>This example demonstrates how to configure the Resource adapter to connect to a remote HornetQ server</p>
<p>This example is composed of a message driven bean and a client<p>
<p>MDBRemoteServerClientExample will send a message to the MDB via a queue and wait for the MDB to send a response via
a reply queue</p>
<h2>WildFly configuration</h2>
<p>The example leverages the Arquillian framework to run a WildFly instance and deploy the MDB.</p>
<h2>Configuring the incoming JCA resource adapter</h2>
<p>The MDB will consume messages via the inflow JCA resource adapter. This can be configured in 2 ways:</p>
<ol>
<li>via a pooled-connection-factory in the "messaging" subsystem</li>
<li>via the activation configuration properties on the MDB set either via annotations or ejb-jar.xml</li>
</ol>
<p>In this example the MDB is annotated with <code>@ResourceAdapter("hornetq-ra-remote.rar")</code> which refers to
this pooled-connection-factory:</p>
<pre class="prettyprint">
<pooled-connection-factory name="hornetq-ra-remote">
<transaction mode="xa"/>
<user>guest</user>
<password>password</password>
<connectors>
<connector-ref connector-name="remote-http-connector"/>
</connectors>
<entries>
<entry name="java:/RemoteJmsXA"/>
</entries>
</pooled-connection-factory>
</pre>
<p>This configuration ensures the MDB will consume from the remote queue.</p>
<h2>Configuring the outgoing JCA resource adapter</h2>
<p>The same pooled-connection-factory used for JCA inflow also configures an outgoing JCA connection factory which Java EE components can use to send messages to the same remote HornetQ Server.</p>
<pre class="prettyprint">
<pooled-connection-factory name="hornetq-ra-remote">
<transaction mode="xa"/>
<user>guest</user>
<password>password</password>
<connectors>
<connector-ref connector-name="remote-http-connector"/>
</connectors>
<entries>
<entry name="java:/RemoteJmsXA"/>
</entries>
</pooled-connection-factory>
</pre>
<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 send a JMS message to the second server.</li>
<pre class="prettyprint">
final Properties env = new Properties();
env.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
env.put(Context.PROVIDER_URL, "http-remoting://localhost:8180");
initialContext = new InitialContext(env);
</pre>
<li>Look up the MDB's queue</li>
<pre class="prettyprint">
Queue queue = (Queue) initialContext.lookup("queues/mdbQueue");
</pre>
<li>look up the connection factory</li>
<pre class="prettyprint">
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("jms/RemoteConnectionFactory");
</pre>
<li>We then create a connection</li>
<pre class="prettyprint">
connection = cf.createConnection("guest", "password");
</pre>
<li>we then create a session</li>
<pre class="prettyprint">
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
</pre>
<li>we now create a message producer to send the message</li>
<pre class="prettyprint">
MessageProducer producer = session.createProducer(queue);
</pre>
<li>create a text message and send it</li>
<pre class="prettyprint">
producer.send(session.createTextMessage("a message"));
</pre>
<li>The MDB receives the text message</li>
<pre class="prettyprint">
TextMessage tm = (TextMessage)message;
</pre>
<li>The MDB looks up the reply queue</li>
<pre class="prettyprint">
Queue destQueue = HornetQJMSClient.createQueue("mdbReplyQueue");
</pre>
<li>The MDB creates a connection</li>
<pre class="prettyprint">
Connection connection = connectionFactory.createConnection();
</pre>
<li>The MDB creates a session</li>
<pre class="prettyprint">
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
</pre>
<li>The MDB creates a message producer to send the message</li>
<pre class="prettyprint">
MessageProducer producer = session.createProducer(destQueue);
</pre>
<li>The MDB creates and sends a text message</li>
<pre class="prettyprint">
producer.send(session.createTextMessage("A reply message"));
</pre>
<li>The MDB closes the connection which returns it to the pool</li>
<pre class="prettyprint">
connection.close();
</pre>
<li>The client now looks up the reply queue</li>
<pre class="prettyprint">
Queue replyQueue = (Queue) initialContext.lookup("queues/mdbReplyQueue");
</pre>
<li>and creates a message consumer to receive the message</li>
<pre class="prettyprint">
MessageConsumer consumer = session.createConsumer(replyQueue);
</pre>
<li>starting the connection starts delivery</li>
<pre class="prettyprint">
connection.start();
</pre>
<li>The message consumer receives the text message</li>
<pre class="prettyprint">
TextMessage textMessage = (TextMessage) consumer.receive(5000);
</pre>
<li>and we always clear up out JMS resources</li>
<pre class="prettyprint">
if (initialContext != null)
{
initialContext.close();
}
if (connection != null)
{
connection.close();
}
</pre>
</ol>
</body>
</html>