KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > jms > JMSConsumer


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.jms;
8
9 import javax.jms.Destination JavaDoc;
10 import javax.jms.JMSException JavaDoc;
11 import javax.jms.Message JavaDoc;
12 import javax.jms.MessageConsumer JavaDoc;
13 import javax.jms.MessageListener JavaDoc;
14 import javax.jms.Queue JavaDoc;
15 import javax.jms.QueueReceiver JavaDoc;
16 import javax.jms.Topic JavaDoc;
17 import javax.jms.TopicSubscriber JavaDoc;
18
19 import org.jfox.ioc.util.UUID;
20
21 /**
22  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
23  */

24
25 public class JMSConsumer implements MessageConsumer JavaDoc, QueueReceiver JavaDoc, TopicSubscriber JavaDoc {
26     private JMSSession session = null;
27     private JMSDestination destination = null;
28     private String JavaDoc msgSelector = null;
29     private boolean noLocal;
30
31     /**
32      * P2P receiver is durable
33      * Durable Topic subscriber is durable
34      */

35     private boolean durable = false;
36     // only a durable TopicSubscriber has a name
37
private String JavaDoc name = "";
38
39     private MessageListener JavaDoc listener = null;
40
41     private boolean closed = false;
42
43     private String JavaDoc consumerId = UUID.randomUUID().toString();
44
45     public JMSConsumer(JMSSession session, Destination JavaDoc destination, String JavaDoc 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 JavaDoc getMessageSelector() throws JMSException JavaDoc {
56         checkClosed();
57         throw new JMSException JavaDoc("not support now!");
58     }
59
60     public MessageListener JavaDoc getMessageListener() throws JMSException JavaDoc {
61         checkClosed();
62         return listener;
63     }
64
65     public void setMessageListener(MessageListener JavaDoc listener) throws JMSException JavaDoc {
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 JavaDoc receive() throws JMSException JavaDoc {
76         checkClosed();
77         return receive(0);
78     }
79
80     public Message JavaDoc receive(long timeout) throws JMSException JavaDoc {
81         checkClosed();
82         return session.receiveMessage(this, timeout);
83     }
84
85     public Message JavaDoc receiveNoWait() throws JMSException JavaDoc {
86         checkClosed();
87         return receive(-1);
88     }
89
90     public void close() throws JMSException JavaDoc {
91         if (closed) return;
92         closed = true;
93         session.closeConsumer(consumerId);
94     }
95
96     public Queue JavaDoc getQueue() throws JMSException JavaDoc {
97         checkClosed();
98         return (Queue JavaDoc) destination;
99     }
100
101     public Topic JavaDoc getTopic() throws JMSException JavaDoc {
102         checkClosed();
103         return (Topic JavaDoc) destination;
104     }
105
106     public boolean getNoLocal() throws JMSException JavaDoc {
107         checkClosed();
108         return noLocal;
109     }
110
111     public String JavaDoc getName() {
112         return name;
113     }
114
115     /**
116      * only a durable TopicSubscriber has a name
117      *
118      * @param name
119      */

120     public void setName(String JavaDoc name) {
121         if (destination.isTopic()) {
122             this.name = name;
123         }
124         durable = true;
125     }
126
127     String JavaDoc getConsumerId() {
128         return consumerId;
129     }
130
131     Destination JavaDoc getDestination() {
132         return destination;
133     }
134
135     private void checkClosed() throws javax.jms.IllegalStateException JavaDoc {
136         if (closed) {
137             throw new javax.jms.IllegalStateException JavaDoc("MessageConsumer closed");
138         }
139     }
140
141     public static void main(String JavaDoc[] args) {
142
143     }
144 }
145
Popular Tags