1 46 package org.mr.api.simple; 47 48 import org.mr.api.jms.MantaQueueConnectionFactory; 49 50 65 public class Producer { 66 private String queueName; 69 70 private javax.jms.Session session; 71 private javax.jms.MessageProducer producer; 72 73 Producer(String queueName) throws SimpleException { 74 this.queueName = queueName; 75 76 javax.jms.QueueConnectionFactory factory; 77 javax.jms.QueueConnection connection; 78 javax.jms.Queue queue; 79 80 try { 81 factory = new MantaQueueConnectionFactory(); 82 connection = factory.createQueueConnection(); 83 this.session = connection.createQueueSession(false, 84 javax.jms.Session. 85 AUTO_ACKNOWLEDGE); 86 queue = session.createQueue(queueName); 87 this.producer = session.createProducer(queue); 88 connection.start(); 89 } catch (javax.jms.JMSException e) { 90 throw new SimpleException(e); 91 } 92 } 94 101 public void send(Message message) throws SimpleException { 102 if (this.producer == null) { 103 throw new SimpleException(SimpleException.ERROR_CLOSED_ACTOR, 104 "Producer for " + this.queueName + 105 " is closed. No further messages " + 106 "can be sent."); 107 } 108 109 try { 110 javax.jms.BytesMessage jmsMessage = 111 message.toBytesMessage(this.session); 112 this.producer.send(jmsMessage); 113 } catch (javax.jms.JMSException e) { 114 throw new SimpleException(e); 115 } 116 } 117 118 123 public void close() throws SimpleException { 124 if (this.producer == null) { 125 throw new SimpleException(SimpleException.ERROR_CLOSED_ACTOR, 126 "Publisher for " + this.queueName + 127 " is already closed."); 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 |