1 package net.walend.somnifugi; 2 3 import javax.jms.QueueSession ; 4 import javax.jms.Queue ; 5 import javax.jms.TemporaryQueue ; 6 import javax.jms.QueueSender ; 7 import javax.jms.QueueReceiver ; 8 import javax.jms.JMSException ; 9 import javax.jms.Message ; 10 11 17 public class SomniQueueRequestor 18 { 19 private QueueSession session; private Queue queue; private TemporaryQueue replyQueue; 22 private QueueSender requestSender; 23 private QueueReceiver replyReceiver; 24 25 39 public SomniQueueRequestor(QueueSession session, Queue queue) throws JMSException 40 { 41 this.session = session; 42 this.queue = queue; 43 replyQueue = session.createTemporaryQueue(); 44 requestSender = session.createSender(queue); 45 replyReceiver = session.createReceiver(replyQueue); 46 } 47 48 59 60 public Message request(Message message) 61 throws JMSException 62 { 63 message.setJMSReplyTo(replyQueue); 64 requestSender.send(message); 65 return (replyReceiver.receive()); 66 } 67 68 69 84 85 public void close() throws JMSException 86 { 87 session.close(); 89 replyQueue.delete(); 90 } 91 92 109 public Message request(Message message,long timeout) 110 throws JMSException 111 { 112 long timesUp = System.currentTimeMillis()+timeout; 113 114 message.setJMSExpiration(timesUp); 115 message.setJMSReplyTo(replyQueue); 116 requestSender.send(message); 117 118 long timeLeft = timesUp - System.currentTimeMillis(); 119 120 return replyReceiver.receive(timeLeft); 121 } 122 } 123 124 144 | Popular Tags |