// DurableSubscriber.java import javax.jms.*; import javax.naming.*; public class DurableSubscriber { InitialContext contexto = null; TopicConnectionFactory topicConnectionFactory = null; TopicConnection topicConnection = null; TopicSession topicSession = null; Topic topic = null; TopicSubscriber topicSubscriber = null; TextListener topicListener = null; public final String topicName = "asunto"; public DurableSubscriber() { try { contexto = new InitialContext(); topicConnectionFactory = (TopicConnectionFactory)contexto.lookup("TopicDurable"); topicConnection = topicConnectionFactory.createTopicConnection(); topicSession = topicConnection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE); topic = (Topic)contexto.lookup("asunto"); } catch (Exception e) { System.out.println("Error en DurableSubscriber"); e.printStackTrace(); } } public void startSubscriber() { try { System.out.println("Iniciando suscriptor"); topicConnection.stop(); topicSubscriber = topicSession.createDurableSubscriber(topic,"MakeItLast"); topicListener = new TextListener(); topicSubscriber.setMessageListener(topicListener); topicConnection.start(); } catch (Exception e) { System.out.println("Error en metodo startSubscriber() de DurableSubscriber"); e.printStackTrace(); } } public void closeSubscriber() { try { topicListener.monitor.espera(); System.out.println("Cerrando Suscriptor"); topicSubscriber.close(); } catch (Exception e) { System.out.println("Error en metodo closeSubscriber() de DurableSubscriber"); e.printStackTrace(); } } public void finish() { if (topicConnection != null) { try { System.out.println("Anulando suscripcion"); topicSession.unsubscribe("MakeItLast"); topicConnection.close(); } catch (JMSException e) {} } } }