1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact tma@netspace.net.au.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2003-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: ThreadedReceiver.java,v 1.4 2004/02/03 21:52:06 tanderson Exp $
44 */
45 package org.exolab.jmscts.core;
46
47 import java.util.List;
48
49
50 /***
51 * Helper class which performs message receipt in a separate thread
52 *
53 * @version $Revision: 1.4 $ $Date: 2004/02/03 21:52:06 $
54 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
55 */
56 public class ThreadedReceiver extends ThreadedAction {
57
58 /***
59 * The receiver to receive messages with
60 */
61 private final MessageReceiver _receiver;
62
63 /***
64 * The expected no. of messages to receive
65 */
66 private final int _count;
67
68 /***
69 * The maximum time to wait for each message. If set to 0,
70 * then it waits until a message becomes available.
71 */
72 private final long _timeout;
73
74 /***
75 * Listener for asynchronous receipt
76 */
77 private final CountingListener _listener;
78
79 /***
80 * List of received messages
81 */
82 private volatile List _messages = null;
83
84
85 /***
86 * Construct a new <code>ThreadedReceiver</code>, for synchronous receipt
87 *
88 * @param receiver the message receiver
89 * @param count the expected no. of messages to receive
90 * @param timeout the maximum time to wait for each message. If set to 0,
91 * then it waits until a message becomes available.
92 */
93 public ThreadedReceiver(MessageReceiver receiver, int count,
94 long timeout) {
95 this(receiver, count, timeout, null, null);
96 }
97
98 /***
99 * Construct a new <code>ThreadedReceiver</code>, for synchronous receipt
100 *
101 * @param receiver the message receiver
102 * @param count the expected no. of messages to receive
103 * @param timeout the maximum time to wait for each message. If set to 0,
104 * then it waits until a message becomes available.
105 * @param completion the listener to notify on completion
106 */
107 public ThreadedReceiver(MessageReceiver receiver, int count,
108 long timeout, CompletionListener completion) {
109 this(receiver, count, timeout, null, completion);
110 }
111
112 /***
113 * Construct a new <code>ThreadedReceiver</code>, for asynchronous receipt
114 *
115 * @param receiver the message receiver
116 * @param timeout the maximum time to wait for each message. If set to 0,
117 * then it waits until a message becomes available.
118 * @param listener the listener to handle received messages
119 */
120 public ThreadedReceiver(MessageReceiver receiver,
121 long timeout, CountingListener listener) {
122 this(receiver, 0, timeout, listener, null);
123 }
124
125 /***
126 * Construct a new <code>ThreadedReceiver</code>, for asynchronous receipt
127 *
128 * @param receiver the message receiver
129 * @param count the expected no. of messages to receive
130 * @param timeout the maximum time to wait for each message. If set to 0,
131 * then it waits until a message becomes available.
132 * @param listener the listener to handle received messages
133 * @param completion the listener to notify on completion
134 */
135 public ThreadedReceiver(MessageReceiver receiver, int count,
136 long timeout, CountingListener listener,
137 CompletionListener completion) {
138 super(completion);
139 _receiver = receiver;
140 _count = count;
141 _timeout = timeout;
142 _listener = listener;
143 }
144
145 /***
146 * The received messages, when synchronous receipt is used
147 *
148 * @return the received messages, when synchronous receipt is used,
149 * or <code>null</code> if no message has been received or this hasn't
150 * completed
151 */
152 public List getMessages() {
153 return _messages;
154 }
155
156 /***
157 * Run the action
158 *
159 * @throws Exception for any error
160 */
161 public void runProtected() throws Exception {
162 if (_listener == null) {
163 _messages = _receiver.receive(_count, _timeout);
164 } else {
165 _receiver.receive(_timeout, _listener);
166 }
167 }
168
169 }
This page was automatically generated by Maven