| <div class="wiki-content maincontent"><p>Create the file <webapp-root>/META-INF/context.xml. Here is an example:</p> |
| <div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl"> |
| <script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[ |
| <Context antiJARLocking="true"> |
| <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"/> |
| |
| <Resource name="jms/topic/MyTopic" |
| auth="Container" |
| type="org.apache.activemq.command.ActiveMQTopic" |
| factory="org.apache.activemq.jndi.JNDIReferenceFactory" |
| physicalName="MY.TEST.FOO"/> |
| <Resource name="jms/queue/MyQueue" |
| auth="Container" |
| type="org.apache.activemq.command.ActiveMQQueue" |
| factory="org.apache.activemq.jndi.JNDIReferenceFactory" |
| physicalName="MY.TEST.FOO.QUEUE"/> |
| </Context> |
| ]]></script> |
| </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"> |
| <script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[ |
| 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 |
| } |
| ]]></script> |
| </div></div></div> |
| |