KickJava   Java API By Example, From Geeks To Geeks.

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


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.Queue JavaDoc;
39 import javax.jms.QueueReceiver JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41
42 /**
43  * Represents a memory queue consumer.
44  */

45 public class MemoryQueueConsumer extends MessageConsumerImpl
46   implements QueueReceiver JavaDoc {
47   static final Logger JavaDoc log = Log.open(MemoryQueueConsumer.class);
48   static final L10N L = new L10N(MemoryQueueConsumer.class);
49
50   private MemoryQueue _queue;
51
52   private int _consumerId;
53
54   private boolean _autoAck;
55
56   public MemoryQueueConsumer(SessionImpl session, String JavaDoc messageSelector,
57                  MemoryQueue queue)
58     throws JMSException JavaDoc
59   {
60     super(session, messageSelector, queue, false);
61
62     if (queue == null)
63       throw new NullPointerException JavaDoc();
64     
65     _queue = queue;
66
67     _consumerId = queue.generateConsumerId();
68
69     if (session.getAcknowledgeMode() == session.AUTO_ACKNOWLEDGE ||
70     session.getAcknowledgeMode() == session.DUPS_OK_ACKNOWLEDGE)
71       _autoAck = true;
72   }
73
74   /**
75    * Returns the queue.
76    */

77   public Queue JavaDoc getQueue()
78   {
79     return _queue;
80   }
81
82   /**
83    * Receives a message from the queue.
84    */

85   protected MessageImpl receiveImpl()
86     throws JMSException JavaDoc
87   {
88     // purgeExpiredConsumers();
89
// _queue.purgeExpiredMessages();
90

91     return _queue.receive(_selector, _consumerId, _autoAck);
92   }
93
94   /**
95    * Acknowledges all received messages from the session.
96    */

97   public void acknowledge()
98     throws JMSException JavaDoc
99   {
100     if (_autoAck)
101       return;
102
103     _queue.acknowledge(_consumerId, Long.MAX_VALUE);
104   }
105
106   /**
107    * Rollback all received messages from the session.
108    */

109   public void rollback()
110     throws JMSException JavaDoc
111   {
112     if (_autoAck)
113       return;
114
115     _queue.rollback(_consumerId);
116   }
117
118   /**
119    * Returns a printable view of the queue.
120    */

121   public String JavaDoc toString()
122   {
123     return "MemoryQueueConsumer[" + _queue + "," + _consumerId + "]";
124   }
125 }
126
127
Popular Tags