KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > cache > JMSMessageQueue


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Developer of the Shared Modifications is Jahia Solution Sarl.
30  * Portions created by the Initial Developer are Copyright (C) 2002 by the
31  * Initial Developer. All Rights Reserved.
32  *
33  * Contributor(s):
34  * 05-SEP-2003, Jahia Solutions Sarl, Fulco Houkes : Initial version
35  *
36  * ----- END LICENSE BLOCK -----
37  */

38
39
40 package org.jahia.services.cache;
41
42 import java.util.ArrayList JavaDoc;
43
44
45 /** This class handles a queue of
46  * {@link org.jahia.services.cache.JMSCacheMessage JMSCacheMessage} object in a
47  * <i>first-in first-out</i> manner. The adding process also take care of removing
48  * obsolete messages, for exemple, removing all the previous messages when a <i>flush</i>
49  * message is inserted in the queue, see the
50  * {@link org.jahia.services.cache.JMSMessageQueue#add add()} method's description.
51  *
52  * @author Fulco Houkes, Jahia Ltd.
53  * @version 1.0
54  * @since Jahia 4.0
55  *
56  * @see org.jahia.services.cache.JMSCacheMessage
57  */

58 class JMSMessageQueue {
59
60     /** logging. */
61     final private static org.apache.log4j.Logger logger =
62             org.apache.log4j.Logger.getLogger (JMSMessageQueue.class);
63
64     /** the message queue. */
65     private ArrayList JavaDoc queue;
66
67
68     /** Default constructor, creates a new <code>JMSMessageQueue</code> instance.*/
69     public JMSMessageQueue () {
70         // initialize the queue
71
queue = new ArrayList JavaDoc();
72     }
73
74     /** <p>Add the message at the end of the queue. The following rules are applied:</p>
75      *
76      * <li>When a <i>flush</i> message is added, all the previous messages are cleared,
77      * as they have no sens anymore: they'll be removed by the flush event anyway.</li>
78      *
79      * <li>When a <i>put</i> message is added, all the previous <i>put</i> messages on the
80      * same entry key are removed.</li>
81      *
82      * <li>When a <i>remove</i> message is added, all the previous messages (put & remove)
83      * on the same entry key are removed.</li>
84      *
85      * @param message the message to be added, <code>null</code> messages are ignored.
86      */

87     public synchronized void add (final JMSCacheMessage message) {
88         // don't know what to do with a null message
89
if (message == null)
90             return;
91
92         // optimizeQueue(message);
93

94         // add the message at the end of the queue
95
queue.add(message);
96         if (logger.isDebugEnabled()) {
97             logger.debug("Added message " + message + " to queue. Queue size is now " + queue.size());
98         }
99     }
100
101     private void optimizeQueue (JMSCacheMessage message) {
102         // if the message is of type FLUSH, then all the current messages
103
// in the queue have no reason to exist anymore, because they
104
// will be flush anyway by this message.
105
if (message.isFlush()) {
106             logger.debug("Clearing queue because of flush message");
107             queue.clear();
108
109         // if the message is of type REMOVE, check if there is not already
110
// a previous PUT message for the same entry key. If it's the case,
111
// the PUT message has not meaning anymore.
112
// It has also no sense to have duplicated PUT and REMOVE message
113
// on the same entry key.
114
} else {
115             // look for a put message
116
for (int i=0; i<queue.size(); i++) {
117                 JMSCacheMessage msg = (JMSCacheMessage)queue.get(i);
118
119                 // handle PUT message
120
if (message.isPut()) {
121                     // remove duplicated PUT msg
122
if (msg.isPut() && (message.getEntryKey().equals (msg.getEntryKey()))) {
123                         logger.debug("Removing duplicate PUT message for entry key " + message.getEntryKey());
124                         queue.remove(i);
125                     }
126
127                 // handle REMOVE message
128
} else {
129
130                     // remove duplicated REMOVE msg on the same entry key
131
if (msg.isRemove() && (message.getEntryKey().equals(msg.getEntryKey()))) {
132                         logger.debug("Removing duplicate REMOVE message for entry key " + message.getEntryKey());
133                         queue.remove(i);
134
135                     // remove previous PUT msg on the same entry key, as they
136
// will be overriden by this REMOVE message anyway.
137
} else if (msg.isPut() && (message.getEntryKey().equals(msg.getEntryKey()))) {
138                         logger.debug("Removing previous PUT message because of REMOVE message on entry key " + message.getEntryKey());
139                         queue.remove(i);
140                     }
141                 }
142             }
143         }
144     }
145
146     /** Extract all the messages out of the queue. All the returned messages will
147      * be removed from the queue. Calling this method will leave the queue empty.
148      * When the queue is empty, an empty non-<code>null</code> array is returned.
149      *
150      * @return the extracted messages object array
151      */

152     public synchronized JMSCacheMessage[] extractMessages(int sendCountThreshold) {
153         if (queue.size() == 0)
154             return new JMSCacheMessage[0];
155
156         JMSCacheMessage[] messages = null;
157         if (queue.size() < sendCountThreshold) {
158             // extract all the messages
159
messages = (JMSCacheMessage[]) queue.toArray(new JMSCacheMessage[queue.size()]);
160
161             // clear the queue
162
queue.clear();
163         } else {
164             messages = new JMSCacheMessage[sendCountThreshold];
165             for (int i=0; i < sendCountThreshold; i++) {
166                 messages[i] = (JMSCacheMessage) queue.remove(0);
167             }
168         }
169         return messages;
170     }
171
172
173     /** Retrieve the number of message present in the queue.
174      *
175      * @return the queue size
176      */

177     final public int size() {
178         return queue.size();
179     }
180
181 }
182
Popular Tags