KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

68     public Message JavaDoc poll()
69     {
70         return queue.poll();
71     }
72
73     /**
74 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.
75     */

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

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

94     public void pushBack(Message JavaDoc elem)
95         throws InterruptedException JavaDoc
96     {
97         queue.put(elem);
98     }
99
100     public Message JavaDoc peek()
101     {
102         return queue.peek();
103     }
104     
105     //Channel methods
106
public boolean hasRealPushback()
107     {
108         return false;
109     }
110
111     public boolean supportsPriorities()
112     {
113         return false;
114     }
115     
116     public boolean supportsMessageSelectors()
117     {
118         return false;
119     }
120     
121     public Enumeration JavaDoc snapShot()
122     {
123         List JavaDoc<Message JavaDoc> snap = new ArrayList JavaDoc<Message JavaDoc>(queue);
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