KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > walend > somnifugi > juc > PriorityChannel


1 package net.walend.somnifugi.juc;
2
3 import java.util.Comparator JavaDoc;
4 import java.util.Enumeration JavaDoc;
5 import java.util.Collections JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.ArrayList JavaDoc;
8
9 import java.util.concurrent.TimeUnit JavaDoc;
10 import java.util.concurrent.BlockingQueue JavaDoc;
11 import java.util.concurrent.PriorityBlockingQueue JavaDoc;
12
13 import javax.jms.Message JavaDoc;
14 import javax.jms.JMSException JavaDoc;
15
16 import net.walend.somnifugi.SomniRuntimeException;
17 import net.walend.somnifugi.SomniMessage;
18 import net.walend.somnifugi.SomniMessageSelector;
19 import net.walend.somnifugi.SomniMessageSelectorException;
20
21 import net.walend.somnifugi.channel.Channel;
22 import net.walend.somnifugi.channel.Puttable;
23 import net.walend.somnifugi.channel.Takable;
24
25 /**
26 A Channel built on a PriorityBlockingQueue.
27
28 @author <a HREF="http://walend.net">David Walend</a> <a HREF="mailto:david@walend.net">david@walend.net</a>
29  */

30
31 public class PriorityChannel
32     implements Channel<Message JavaDoc>,Puttable<Message JavaDoc>,Takable<Message JavaDoc>
33 {
34     private PriorityBlockingQueue JavaDoc<Message JavaDoc> queue = new PriorityBlockingQueue JavaDoc<Message JavaDoc>(11,new MessageComparator());
35
36
37
38     public PriorityChannel()
39     {
40     }
41
42     //Puttable interface
43
/**
44 Inserts the specified element into this queue, waiting if necessary up to the specified wait time for space to become available.
45
46 @return true if the element was added.
47     */

48     public boolean offer(Message JavaDoc elem,long timeout)
49         throws InterruptedException JavaDoc
50     {
51         return queue.offer(elem,timeout,TimeUnit.MILLISECONDS);
52     }
53
54     /**
55 Adds the specified element to this queue, waiting if necessary for space to become available.
56     */

57     public void put(Message JavaDoc elem)
58         throws InterruptedException JavaDoc
59     {
60         queue.put(elem);
61     }
62
63     //Takable interface
64
/**
65 Retrieves and removes the head of this queue.
66     */

67     public Message JavaDoc poll()
68     {
69         return queue.poll();
70     }
71
72     /**
73 Retrieves and removes the head of this queue, waiting if necessary up to the specified wait time if no elements are present on this queue.
74     */

75     public Message JavaDoc poll(long timeout)
76         throws InterruptedException JavaDoc
77     {
78         return queue.poll(timeout,TimeUnit.MILLISECONDS);
79     }
80
81     /**
82 Retrieves and removes the head of this queue, waiting if no elements are present on this queue.
83     */

84     public Message JavaDoc take()
85         throws InterruptedException JavaDoc
86     {
87         return queue.take();
88     }
89
90     /**
91 Pushes an element back onto the head of a queue.
92     */

93     public void pushBack(Message JavaDoc elem)
94         throws InterruptedException JavaDoc
95     {
96         queue.put(elem);
97     }
98
99     public Message JavaDoc peek()
100     {
101         return queue.peek();
102     }
103
104     //Channel methods
105
public boolean hasRealPushback()
106     {
107         return true;
108     }
109
110     public boolean supportsPriorities()
111     {
112         return true;
113     }
114     
115     public boolean supportsMessageSelectors()
116     {
117         return false;
118     }
119
120     public Enumeration JavaDoc<Message JavaDoc> snapShot()
121     {
122         List JavaDoc<Message JavaDoc> snap = new ArrayList JavaDoc<Message JavaDoc>(queue);
123         Collections.sort(snap,queue.comparator());
124         
125         return new EnumerationBridge<Message JavaDoc>(snap.iterator());
126     }
127     
128     public Enumeration JavaDoc snapShot(SomniMessageSelector messageSelector)
129         throws SomniMessageSelectorException
130     {
131         List JavaDoc<Message JavaDoc> snap = new ArrayList JavaDoc<Message JavaDoc>(queue.size());
132         
133         for(Message JavaDoc message : queue)
134         {
135             if(messageSelector.matches(message))
136             {
137                 snap.add(message);
138             }
139         }
140         
141         return new EnumerationBridge<Message JavaDoc>(snap.iterator());
142     }
143     
144     public Puttable<Message JavaDoc> getPuttable()
145     {
146         return this;
147     }
148     
149     public Takable<Message JavaDoc> getTakable()
150     {
151         return this;
152     }
153     
154     public Takable<Message JavaDoc> getTakable(SomniMessageSelector messageSelector)
155     {
156         throw new UnsupportedOperationException JavaDoc(getClass().getName()+" does not support message selectors.");
157     }
158
159     public int guessSize()
160     {
161         return queue.size();
162     }
163 }
164
165 /* Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 David Walend
166 All rights reserved.
167
168 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
169
170 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
171
172 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
173
174 Neither the name of the SomnifugiJMS Project, walend.net, nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission from David Walend.
175
176 Credits in redistributions in source or binary forms must include a link to http://somnifugi.sourceforge.net .
177
178 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
179 The net.walend.somnifugi.sql92 package is modified code from the openmq project, https://mq.dev.java.net/ , Copyright (c) of Sun, and carries the CDDL license, repeated here: You can obtain a copy of the license at https://glassfish.dev.java.net/public/CDDLv1.0.html. See the License for the specific language governing permissions and limitations under the License.
180
181 =================================================================================
182
183 For more information and the latest version of this software, please see http://somnifugi.sourceforge.net and http://walend.net or email <a HREF="mailto:david@walend.net">david@walend.net</a>.
184  */

185
Popular Tags