package durabelesubscribers; import javax.naming.InitialContext; import javax.jms.Topic; import javax.jms.Session; import javax.jms.Message; import javax.jms.TopicSession; import javax.jms.TopicSubscriber; import javax.jms.TopicConnection; import javax.jms.TopicConnectionFactory; public class Consume { public static void main(String[] args) throws Exception { // get the initial context InitialContext ctx = new InitialContext(); // lookup the topic object Topic topic = (Topic) ctx.lookup("topic/topic0"); // lookup the topic connection factory TopicConnectionFactory connFactory = (TopicConnectionFactory) ctx. lookup("topic/connectionFactory"); // create a topic connection TopicConnection topicConn = connFactory.createTopicConnection(); topicConn.setClientID("durable"); // create a topic session TopicSession topicSession = topicConn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); // create a topic subscriber TopicSubscriber topicSubscriber; topicSubscriber = topicSession.createDurableSubscriber(topic, "mySub"); // start the connection topicConn.start(); // get the number of messages to receive int numMsgs = (args.length > 0) ? new Integer(args[0]).intValue() : 100; int firstID = 0, lastID = 0; // receive the message for (int i=0; i < numMsgs; i++) { Message message = topicSubscriber.receive(); if (i == 0) firstID = message.getIntProperty("id"); if (i == numMsgs - 1) lastID = message.getIntProperty("id"); } System.out.println("consumed messages " + firstID + "-" + lastID); // close the topic connection topicConn.close(); } }