KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > memory > buffer > MessageQueue


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.buffer;
19
20 import org.apache.activemq.broker.region.MessageReference;
21 import org.apache.activemq.command.ActiveMQMessage;
22 import org.apache.activemq.command.Message;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28
29 /**
30  * Allows messages to be added to the end of the buffer such that they are kept
31  * around and evicted in a FIFO manner.
32  *
33  * @version $Revision: 1.1 $
34  */

35 public class MessageQueue {
36
37     private MessageBuffer buffer;
38     private LinkedList JavaDoc list = new LinkedList JavaDoc();
39     private int size;
40     private Object JavaDoc lock = new Object JavaDoc();
41     private int position;
42
43     public MessageQueue(MessageBuffer buffer) {
44         this.buffer = buffer;
45     }
46
47     public void add(MessageReference messageRef) {
48         Message message = messageRef.getMessageHardRef();
49         int delta = message.getSize();
50         int newSize = 0;
51         synchronized (lock) {
52             list.add(messageRef);
53             size += delta;
54             newSize = size;
55         }
56         buffer.onSizeChanged(this, delta, newSize);
57     }
58     
59     public void add(ActiveMQMessage message) {
60         int delta = message.getSize();
61         int newSize = 0;
62         synchronized (lock) {
63             list.add(message);
64             size += delta;
65             newSize = size;
66         }
67         buffer.onSizeChanged(this, delta, newSize);
68     }
69
70     public int evictMessage() {
71         synchronized (lock) {
72             if (!list.isEmpty()) {
73                 ActiveMQMessage message = (ActiveMQMessage) list.removeFirst();
74                 int messageSize = message.getSize();
75                 size -= messageSize;
76                 return messageSize;
77             }
78         }
79         return 0;
80     }
81
82     /**
83      * Returns a copy of the list
84      */

85     public List JavaDoc getList() {
86         synchronized (lock) {
87             return new ArrayList JavaDoc(list);
88         }
89     }
90
91     public void appendMessages(List JavaDoc answer) {
92         synchronized (lock) {
93             for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();) {
94                 answer.add(iter.next());
95             }
96         }
97     }
98
99     public int getSize() {
100         synchronized (lock) {
101             return size;
102         }
103     }
104
105     public int getPosition() {
106         return position;
107     }
108
109     public void setPosition(int position) {
110         this.position = position;
111     }
112
113     public void clear() {
114         synchronized (lock) {
115             list.clear();
116             size = 0;
117         }
118     }
119
120 }
121
Popular Tags