1 6 7 package org.jfox.jms; 8 9 import javax.jms.Destination ; 10 import javax.jms.JMSException ; 11 import javax.jms.Message ; 12 import javax.jms.MessageConsumer ; 13 import javax.jms.MessageListener ; 14 import javax.jms.Queue ; 15 import javax.jms.QueueReceiver ; 16 import javax.jms.Topic ; 17 import javax.jms.TopicSubscriber ; 18 19 import org.jfox.ioc.util.UUID; 20 21 24 25 public class JMSConsumer implements MessageConsumer , QueueReceiver , TopicSubscriber { 26 private JMSSession session = null; 27 private JMSDestination destination = null; 28 private String msgSelector = null; 29 private boolean noLocal; 30 31 35 private boolean durable = false; 36 private String name = ""; 38 39 private MessageListener listener = null; 40 41 private boolean closed = false; 42 43 private String consumerId = UUID.randomUUID().toString(); 44 45 public JMSConsumer(JMSSession session, Destination destination, String msgSelector, boolean noLocal) { 46 this.session = session; 47 this.destination = (JMSDestination) destination; 48 this.msgSelector = msgSelector; 49 this.noLocal = noLocal; 50 if (!this.destination.isTopic()) { 51 durable = true; 52 } 53 } 54 55 public String getMessageSelector() throws JMSException { 56 checkClosed(); 57 throw new JMSException ("not support now!"); 58 } 59 60 public MessageListener getMessageListener() throws JMSException { 61 checkClosed(); 62 return listener; 63 } 64 65 public void setMessageListener(MessageListener listener) throws JMSException { 66 checkClosed(); 67 this.listener = listener; 68 if (listener != null) { 69 session.setConsumerAsync(this, true); 70 } else { 71 session.setConsumerAsync(this, false); 72 } 73 } 74 75 public Message receive() throws JMSException { 76 checkClosed(); 77 return receive(0); 78 } 79 80 public Message receive(long timeout) throws JMSException { 81 checkClosed(); 82 return session.receiveMessage(this, timeout); 83 } 84 85 public Message receiveNoWait() throws JMSException { 86 checkClosed(); 87 return receive(-1); 88 } 89 90 public void close() throws JMSException { 91 if (closed) return; 92 closed = true; 93 session.closeConsumer(consumerId); 94 } 95 96 public Queue getQueue() throws JMSException { 97 checkClosed(); 98 return (Queue ) destination; 99 } 100 101 public Topic getTopic() throws JMSException { 102 checkClosed(); 103 return (Topic ) destination; 104 } 105 106 public boolean getNoLocal() throws JMSException { 107 checkClosed(); 108 return noLocal; 109 } 110 111 public String getName() { 112 return name; 113 } 114 115 120 public void setName(String name) { 121 if (destination.isTopic()) { 122 this.name = name; 123 } 124 durable = true; 125 } 126 127 String getConsumerId() { 128 return consumerId; 129 } 130 131 Destination getDestination() { 132 return destination; 133 } 134 135 private void checkClosed() throws javax.jms.IllegalStateException { 136 if (closed) { 137 throw new javax.jms.IllegalStateException ("MessageConsumer closed"); 138 } 139 } 140 141 public static void main(String [] args) { 142 143 } 144 } 145 | Popular Tags |