Apache ActiveMQ ™ -- Setting up ActiveMQ with Tomcat 5.5.9
Connectivity > Containers > Tomcat > Setting up ActiveMQ with Tomcat 5.5.9
Create the file
This will setup the JNDI for the ConectionFactory and Topic to work within Tomcat.
Here is some example code that will publish a test message to the MY.TEST.FOO Topic:
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 }