View Javadoc

1   /***
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Exoffice Technologies.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Exoffice Technologies. Exolab is a registered
23   *    trademark of Exoffice Technologies.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2002-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: SentMessageCache.java,v 1.5 2005/12/20 20:31:59 tanderson Exp $
44   */
45  package org.exolab.jms.server;
46  
47  import java.util.Collections;
48  import java.util.Iterator;
49  import java.util.LinkedList;
50  import java.util.List;
51  import javax.jms.JMSException;
52  import javax.jms.Session;
53  
54  import org.exolab.jms.messagemgr.MessageHandle;
55  
56  
57  /***
58   * Helper class to cache all sent messages and unacked messages for a session.
59   * It also does some other processing like marking the message as sent to
60   * minimize the number of transactions.
61   * <p/>
62   * Messages will only be added to the cache, if the session is transacted or the
63   * ack mode for the session is set to CLIENT_ACKNOWLEDGE
64   *
65   * @author <a href="mailto:jima@exoffice.com">Jim Alateras</a>
66   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
67   * @version $Revision: 1.5 $ $Date: 2005/12/20 20:31:59 $
68   * @see ServerSessionImpl
69   */
70  class SentMessageCache {
71  
72      /***
73       * The message acknowledgement mode, or <code>Session.TRANSACTED_SESSION</code>
74       * if the session is transactional.
75       */
76      private final int _ackMode;
77  
78      /***
79       * Holds a list of unacked messages in the order they were sent.
80       */
81      private List _unackedMessages = Collections.synchronizedList(
82              new LinkedList());
83  
84  
85      /***
86       * Construct a new <code>SentMessageCache</code>.
87       *
88       * @param ackMode  the message acknowledgement mode, or
89       *                 <code>Session.TRANSACTED_SESSION</code>
90       *                 if the session is transactional
91       */
92      public SentMessageCache(int ackMode) {
93          _ackMode = ackMode;
94      }
95  
96      /***
97       * Process a messge handle prior to it being sent to the client.
98       * Applicable to both synchronous and asynchronous delivery.
99       *
100      * @param handle the message handle
101      * @throws JMSException for any error
102      */
103     public void preSend(MessageHandle handle) throws JMSException {
104         handle.setDelivered(true);
105         _unackedMessages.add(handle);
106         if (handle.isPersistent()) {
107             handle.update();
108         }
109     }
110 
111     /***
112      * Process a message handle after it has been successfully sent to the
113      * client using asynchronous delivery.
114      *
115      * @param handle the message handle
116      * @throws JMSException for any error
117      */
118     public void postSend(MessageHandle handle) throws JMSException {
119         if (_ackMode == Session.AUTO_ACKNOWLEDGE
120                 || _ackMode == Session.DUPS_OK_ACKNOWLEDGE) {
121             _unackedMessages.remove(handle);
122             handle.destroy();
123         }
124     }
125 
126     /***
127      * Acknowledge the specified messages in the cache and all previously sent
128      * messages.
129      *
130      * @param messageId  the id of the message to ack
131      * @param consumerId the consumer id that sent the ack.
132      * @throws JMSException if the acknowledge fails
133      */
134     public void acknowledge(String messageId, long consumerId)
135             throws JMSException {
136         // first check that the message exists in the list of unacked
137         // messages
138         boolean exists = false;
139         Iterator iterator = _unackedMessages.iterator();
140         while (iterator.hasNext()) {
141             MessageHandle handle = (MessageHandle) iterator.next();
142             if (handle.getConsumerId() == consumerId
143                     && handle.getMessageId().equals(messageId)) {
144                 exists = true;
145                 break;
146             }
147         }
148 
149         if (exists) {
150             // start from the top of the cache and remove each
151             // message and then call destroy on it.
152             while (!_unackedMessages.isEmpty()) {
153                 MessageHandle handle
154                         = (MessageHandle) _unackedMessages.remove(0);
155                 handle.destroy();
156 
157                 // if the handle is equal to the source handle then
158                 // break the loop
159                 if (handle.getConsumerId() == consumerId
160                     && handle.getMessageId().equals(messageId)) {
161                     break;
162                 }
163             }
164         }
165     }
166 
167     /***
168      * Acknowledge all the messages in the cache.
169      *
170      * @throws JMSException for any error
171      */
172     public void acknowledgeAll() throws JMSException {
173         // start from the top of the cache and remove each
174         // message and then call destroy on it.
175         while (!_unackedMessages.isEmpty()) {
176             MessageHandle handle = (MessageHandle) _unackedMessages.remove(0);
177             handle.destroy();
178         }
179     }
180 
181     /***
182      * Release all unacknowledged message handles.
183      *
184      * @throws JMSException for any error
185      */
186     public void clear() throws JMSException {
187         if (!_unackedMessages.isEmpty()) {
188             MessageHandle[] handles
189                     = (MessageHandle[]) _unackedMessages.toArray(
190                             new MessageHandle[0]);
191             _unackedMessages.clear();
192 
193             for (int i = 0; i < handles.length; ++i) {
194                 MessageHandle handle = handles[i];
195                 handle.release();
196             }
197         }
198     }
199 
200 }