KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jms > memory > MemoryTopicConsumer


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jms.memory;
30
31 import com.caucho.jms.message.MessageImpl;
32 import com.caucho.jms.session.MessageConsumerImpl;
33 import com.caucho.jms.session.SessionImpl;
34 import com.caucho.log.Log;
35 import com.caucho.util.L10N;
36
37 import javax.jms.JMSException JavaDoc;
38 import javax.jms.Topic JavaDoc;
39 import javax.jms.TopicSubscriber JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41
42 /**
43  * Represents a memory topic consumer.
44  */

45 public class MemoryTopicConsumer extends MessageConsumerImpl
46   implements TopicSubscriber JavaDoc {
47   static final Logger JavaDoc log = Log.open(MemoryTopicConsumer.class);
48   static final L10N L = new L10N(MemoryTopicConsumer.class);
49
50   private MemoryTopic _topic;
51   private MemoryQueue _queue;
52
53   private int _consumerId;
54
55   private boolean _autoAck;
56
57   public MemoryTopicConsumer(SessionImpl session, String JavaDoc messageSelector,
58                  MemoryTopic topic)
59     throws JMSException JavaDoc
60   {
61     this(session, messageSelector, topic, null);
62   }
63
64   public MemoryTopicConsumer(SessionImpl session, String JavaDoc messageSelector,
65                  MemoryTopic topic, String JavaDoc name)
66     throws JMSException JavaDoc
67   {
68     super(session, messageSelector, topic, false);
69     
70     _topic = topic;
71
72     if (session.getAcknowledgeMode() == session.AUTO_ACKNOWLEDGE ||
73     session.getAcknowledgeMode() == session.DUPS_OK_ACKNOWLEDGE)
74       _autoAck = true;
75
76     if (name != null)
77       _queue = topic.createDurableSubscriber(name);
78     else
79       _queue = topic.createSubscriberQueue();
80
81     _queue.addListener(this);
82   }
83
84   /**
85    * Returns the topic.
86    */

87   public Topic JavaDoc getTopic()
88   {
89     return _topic;
90   }
91
92   /**
93    * Receives a message from the topic.
94    */

95   protected MessageImpl receiveImpl()
96     throws JMSException JavaDoc
97   {
98     // purgeExpiredConsumers();
99
// _topic.purgeExpiredMessages();
100

101     return _queue.receive(_selector, 1, true);
102   }
103
104   /**
105    * Acknowledges all received messages from the session.
106    */

107   public void acknowledge()
108     throws JMSException JavaDoc
109   {
110     if (_autoAck)
111       return;
112   }
113
114   /**
115    * Rollback all received messages from the session.
116    */

117   public void rollback()
118     throws JMSException JavaDoc
119   {
120     if (_autoAck)
121       return;
122   }
123
124   /**
125    * Closes the consumer.
126    */

127   public void close()
128   {
129     _topic.removeSubscriber(_queue);
130   }
131
132   /**
133    * Returns a printable view of the topic.
134    */

135   public String JavaDoc toString()
136   {
137     return "MemoryTopicConsumer[" + _topic + "," + _consumerId + "]";
138   }
139 }
140
141
Popular Tags