KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > memory > list > SimpleMessageList


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.memory.list;
19
20 import java.io.IOException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25 import org.apache.activemq.broker.region.MessageReference;
26 import org.apache.activemq.command.ActiveMQDestination;
27 import org.apache.activemq.command.Message;
28 import org.apache.activemq.filter.DestinationFilter;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 /**
33  * A simple fixed size {@link MessageList} where there is a single, fixed size
34  * list that all messages are added to for simplicity. Though this
35  * will lead to possibly slow recovery times as many more messages
36  * than is necessary will have to be iterated through for each subscription.
37  *
38  * @version $Revision: 1.1 $
39  */

40 public class SimpleMessageList implements MessageList {
41     static final private Log log=LogFactory.getLog(SimpleMessageList.class);
42     private LinkedList JavaDoc list = new LinkedList JavaDoc();
43     private int maximumSize = 100 * 64 * 1024;
44     private int size;
45     private Object JavaDoc lock = new Object JavaDoc();
46
47     public SimpleMessageList() {
48     }
49
50     public SimpleMessageList(int maximumSize) {
51         this.maximumSize = maximumSize;
52     }
53
54     public void add(MessageReference node) {
55         int delta = node.getMessageHardRef().getSize();
56         synchronized (lock) {
57             list.add(node);
58             size += delta;
59             while (size > maximumSize) {
60                 MessageReference evicted = (MessageReference) list.removeFirst();
61                 size -= evicted.getMessageHardRef().getSize();
62             }
63         }
64     }
65
66     public List JavaDoc getMessages(ActiveMQDestination destination) {
67         return getList();
68     }
69     
70     public Message[] browse(ActiveMQDestination destination) {
71         List JavaDoc result = new ArrayList JavaDoc();
72         DestinationFilter filter=DestinationFilter.parseFilter(destination);
73         synchronized(lock){
74             for (Iterator JavaDoc i = list.iterator(); i.hasNext();){
75                 MessageReference ref = (MessageReference)i.next();
76                 Message msg;
77                 try{
78                     msg=ref.getMessage();
79                     if (filter.matches(msg.getDestination())){
80                         result.add(msg);
81                     }
82                 }catch(IOException JavaDoc e){
83                    log.error("Failed to get Message from MessageReference: " + ref,e);
84                 }
85                 
86             }
87         }
88         return (Message[])result.toArray(new Message[result.size()]);
89     }
90
91     /**
92      * Returns a copy of the list
93      */

94     public List JavaDoc getList() {
95         synchronized (lock) {
96             return new ArrayList JavaDoc(list);
97         }
98     }
99
100     public int getSize() {
101         synchronized (lock) {
102             return size;
103         }
104     }
105
106     public void clear() {
107         synchronized (lock) {
108             list.clear();
109             size = 0;
110         }
111     }
112
113 }
114
Popular Tags