1 46 package org.mr.api.simple; 47 48 import org.mr.api.jms.MantaTopicConnectionFactory; 49 50 65 public class Subscriber { 66 private String topicName; 67 private javax.jms.TopicSubscriber subscriber; 68 private MessageListener listener; 69 70 Subscriber(String topicName) throws SimpleException { 71 this.topicName = topicName; 72 73 javax.jms.TopicConnectionFactory factory; 74 javax.jms.TopicConnection connection; 75 javax.jms.TopicSession session; 76 javax.jms.Topic topic; 77 78 try { 79 factory = new MantaTopicConnectionFactory(); 80 connection = factory.createTopicConnection(); 81 session = connection.createTopicSession(false, javax.jms.Session. 82 AUTO_ACKNOWLEDGE); 83 topic = session.createTopic(topicName); 84 85 this.subscriber = session.createSubscriber(topic); 86 connection.start(); 87 } catch (javax.jms.JMSException e) { 88 throw new SimpleException(e); 89 } 90 91 this.listener = null; 92 } 94 101 public void subscribe(MessageListener listener) throws SimpleException { 102 try { 103 this.listener = listener; 104 this.subscriber.setMessageListener(new javax.jms.MessageListener () 105 { 106 public void onMessage(javax.jms.Message message) { 107 Message simpleMessage = null; 108 try { 109 simpleMessage = 110 new Message((javax.jms.BytesMessage ) message); 111 } catch (javax.jms.JMSException e) { 112 System.out.println("Simple API Error: " + e); 113 } 115 Subscriber.this.listener. 116 onMessage(simpleMessage, 117 Subscriber.this.topicName); 118 } 119 }); 120 } catch (javax.jms.JMSException e) { 121 throw new SimpleException(e); 122 } 123 } 124 125 131 public void close() throws SimpleException { 132 if (this.listener == null) { 133 throw new SimpleException(SimpleException.ERROR_CLOSED_ACTOR, 134 "Suscriber for topic " + this.topicName + 135 " is already closed."); 136 } 137 138 try { 139 this.subscriber.close(); 140 } catch (javax.jms.JMSException e) { 141 throw new SimpleException(e); 142 } 143 this.listener = null; 144 } 145 146 149 public String getTopicName() { 150 return topicName; 151 } 152 } | Popular Tags |