1 46 package org.mr.api.simple; 47 48 import org.mr.api.jms.MantaTopicConnectionFactory; 49 50 65 public class Publisher { 66 private String topicName; 67 68 private javax.jms.Session session; 69 private javax.jms.MessageProducer producer; 70 71 Publisher(String topicName) throws SimpleException { 72 this.topicName = topicName; 73 74 javax.jms.TopicConnectionFactory factory; 75 javax.jms.TopicConnection connection; 76 javax.jms.Topic topic; 77 78 try { 79 factory = new MantaTopicConnectionFactory(); 80 connection = factory.createTopicConnection(); 81 this.session = connection.createTopicSession(false, 82 javax.jms.Session. 83 AUTO_ACKNOWLEDGE); 84 topic = session.createTopic(topicName); 85 this.producer = session.createProducer(topic); 86 connection.start(); 87 } catch (javax.jms.JMSException e) { 88 throw new SimpleException(e); 89 } 90 } 92 99 public void send(Message message) throws SimpleException { 100 if (this.producer == null) { 101 throw new SimpleException(SimpleException.ERROR_CLOSED_ACTOR, 102 "Publisher for " + this.topicName + 103 " is closed. No further messages " + 104 "can be sent."); 105 } 106 107 try { 108 javax.jms.BytesMessage jmsMessage = 109 message.toBytesMessage(this.session); 110 this.producer.send(jmsMessage); 111 } catch (javax.jms.JMSException e) { 112 throw new SimpleException(e); 113 } 114 115 } 116 117 122 public void close() throws SimpleException { 123 if (this.producer == null) { 124 throw new SimpleException(SimpleException.ERROR_CLOSED_ACTOR, 125 "Publisher for " + this.topicName + 126 " is already closed."); 127 } 128 129 try { 130 this.producer.close(); 131 this.producer = null; 132 } catch (javax.jms.JMSException e) { 133 throw new SimpleException(e); 134 } 135 } 136 } | Popular Tags |