props.put("java.naming.provider.url", "tcp://localhost:61616");
props.put("topic.topicName", topicName);
javax.naming.Context ctx = new InitialContext(props);
TopicConnectionFactory factory = (TopicConnectionFactory) ctx.lookup("ConnectionFactory");
TopicConnection conn = factory.createTopicConnection();
final Topic topic = (Topic) ctx.lookup("topicName");
TopicSession session = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSubscriber receiver = session.createSubscriber(topic, selectors, false);
receiver.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
try {
if (message instanceof TextMessage) {
TextMessage txtMsg = (TextMessage) message;
String msg = txtMsg.getText();
LOG.info("Topic Message Received: " + topicName + " - " + msg);
}
message.acknowledge();
} catch (Exception e) {
e.printStackTrace();
}
}
});
conn.start();
} catch (Exception e) {
e.printStackTrace();
}
}