2014-10-31 06:20:28 -04:00
|
|
|
<html>
|
|
|
|
<head>
|
2014-11-19 03:44:57 -05:00
|
|
|
<title>ActiveMQ Java EE Injected JMSContext Example</title>
|
2014-10-31 06:20:28 -04:00
|
|
|
<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 Injected JMSContext Example</h1>
|
|
|
|
|
|
|
|
<p>This example shows you how to inject a JMSContext into an MDB and use it to send a reply to a JMS Client</p>
|
|
|
|
|
|
|
|
<h2>Wildfly configuration</h2>
|
|
|
|
|
|
|
|
<p>The example leverages the JBoss Arquillian framework to run an Wildfly 8 instance and deploy the MDB.</p>
|
|
|
|
|
|
|
|
<h2>Example step-by-step</h2>
|
|
|
|
<p><i>download The latest Wildfly 8 from <a href="http://www.wildfly.org/download/">here</a> and install.</i></p>
|
|
|
|
<p><i>set the JBOSS_HOME property to point to the WildFly install directory</i></p>
|
|
|
|
<p><i>To run the example simply type <code>mvn test</code>from the example directory</i></p>
|
|
|
|
|
|
|
|
<ol>
|
|
|
|
<li> Firstly in the MDB we inject the JMSContext. This will use the Default Connection Factory configured.
|
|
|
|
</li>
|
|
|
|
<pre class="prettyprint">
|
|
|
|
<code>
|
|
|
|
@Inject
|
|
|
|
javax.jms.JMSContext context;</code>
|
|
|
|
|
|
|
|
<li>We then map the reply queue as a resource.
|
|
|
|
</li>
|
|
|
|
<pre class="prettyprint">
|
|
|
|
<code>
|
|
|
|
@Resource(mappedName = "java:/queue/replyQueue")
|
|
|
|
Queue replyQueue;</code>
|
|
|
|
</pre>
|
|
|
|
<li>First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI.</li>
|
|
|
|
<pre class="prettyprint">
|
|
|
|
<code>
|
|
|
|
final Properties env = new Properties();
|
|
|
|
|
|
|
|
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
|
|
|
|
|
|
|
|
env.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
|
|
|
|
|
|
|
|
initialContext = new InitialContext(env);
|
|
|
|
|
|
|
|
</code>
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<li>We look up the JMS queue object from JNDI</li>
|
|
|
|
<pre class="prettyprint">
|
|
|
|
<code>Queue queue = (Queue)initialContext.lookup("jms/queues/testQueue");</code>
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<li>We look up the JMS connection factory object from JNDI</li>
|
|
|
|
<pre class="prettyprint">
|
|
|
|
<code>ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("jms/RemoteConnectionFactory");</code>
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<li>We create a JMSContext inside the try-with-resource block so it auto closes</li>
|
|
|
|
<pre class="prettyprint">
|
|
|
|
<code>
|
|
|
|
try
|
|
|
|
(
|
|
|
|
// Step 6.Create a JMS Connection inside the try-with-resource block so it will auto close
|
|
|
|
JMSContext context = cf.createContext("guest", "password")
|
|
|
|
)
|
|
|
|
</code>
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<li>We create a JMS Producer and send a String as a message.</li>
|
|
|
|
<pre class="prettyprint">
|
|
|
|
<code>context.createProducer().send(queue, "This is a text message");</code>
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<li>We start the context so we can receive messages.</li>
|
|
|
|
<pre class="prettyprint">
|
|
|
|
<code>context.start();</code>
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<li>We look up the reply queue.</li>
|
|
|
|
<pre class="prettyprint">
|
|
|
|
<code>Queue replyQueue = (Queue)initialContext.lookup("jms/queues/replyQueue");</code>
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<li>We receive the body, as a String, of the reply message.</li>
|
|
|
|
<pre class="prettyprint">
|
|
|
|
<code> String text = context.createConsumer(replyQueue).receiveBody(String.class);</code>
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<li>And finally, close the initial context.</li>
|
|
|
|
|
|
|
|
<pre class="prettyprint">
|
|
|
|
<code>finally
|
|
|
|
{
|
|
|
|
if (initialContext != null)
|
|
|
|
{
|
|
|
|
initialContext.close();
|
|
|
|
}
|
|
|
|
}</code>
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</ol>
|
|
|
|
</body>
|
|
|
|
</html>
|