blob: be8efc3e71faf7f733afeecaea42893594230022 [file] [log] [blame]
<div class="wiki-content maincontent"><p>Create the file &lt;webapp-root&gt;/META-INF/context.xml. Here is an example:</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;Context antiJARLocking="true"&gt;
&lt;Resource
name="jms/ConnectionFactory"
auth="Container"
type="org.apache.activemq.ActiveMQConnectionFactory"
description="JMS Connection Factory"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
brokerURL="tcp://localhost:61616"
brokerName="LocalActiveMQBroker"
useEmbeddedBroker="false"/&gt;
&lt;Resource name="jms/topic/MyTopic"
auth="Container"
type="org.apache.activemq.command.ActiveMQTopic"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
physicalName="MY.TEST.FOO"/&gt;
&lt;Resource name="jms/queue/MyQueue"
auth="Container"
type="org.apache.activemq.command.ActiveMQQueue"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
physicalName="MY.TEST.FOO.QUEUE"/&gt;
&lt;/Context&gt;
</pre>
</div></div>
<p>This will setup the JNDI for the ConectionFactory and Topic to work within Tomcat.</p>
<p>Here is some example code that will publish a test message to the MY.TEST.FOO Topic:</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;">
try {
InitialContext initCtx = new InitialContext();
Context envContext = (Context) initCtx.lookup("java:comp/env");
ConnectionFactory connectionFactory = (ConnectionFactory) envContext.lookup("jms/ConnectionFactory");
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer((Destination) envContext.lookup("jms/topic/MyTopic"));
Message testMessage = session.createMessage();
testMessage.setStringProperty("testKey", "testValue");
producer.send(testMessage);
} catch (NamingException e) {
// TODO handle exception
} catch (JMSException e) {
// TODO handle exception
}
</pre>
</div></div></div>