blob: d55147e2dbf18f9c198ad5a0bcd8ae2fa4f0e313 [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>
<structured-macro ac:macro-id="7dd5eabb-c111-44ea-8f88-6ef5041b9159" ac:name="code" ac:schema-version="1"><parameter ac:name="">xml</parameter><plain-text-body>
&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;
</plain-text-body></structured-macro>
<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>
<structured-macro ac:macro-id="b64cbd8a-16e8-45e4-ab6b-2dacc0a5b5c2" ac:name="code" ac:schema-version="1"><plain-text-body>
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
}
</plain-text-body></structured-macro></div>