01: /*
02: * Copyright 2005 Joe Walker
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.directwebremoting.jms;
17:
18: import javax.jms.Message;
19: import javax.jms.MessageConsumer;
20: import javax.jms.MessageListener;
21:
22: /**
23: * A {@link MessageListener} especially for {@link DwrMessageConsumer} that
24: * enables us to block waiting for a message.
25: * <p>What happens if we try to do a blocking read while there is a non-blocking
26: * MessageListener waiting? The JavaDoc says the read can be one or the other so
27: * we assume that we don't need to deliver to both syncs.
28: * @author Joe Walker [joe at getahead dot ltd dot uk]
29: */
30: public class BlockingMessageListener implements MessageListener {
31: /* (non-Javadoc)
32: * @see javax.jms.MessageListener#onMessage(javax.jms.Message)
33: */
34: public void onMessage(Message newMessage) {
35: this .message = newMessage;
36: synchronized (lock) {
37: lock.notifyAll();
38: }
39: }
40:
41: /**
42: * @see MessageConsumer#receive(long)
43: */
44: public Message receive(long timeout) {
45: try {
46: synchronized (lock) {
47: lock.wait(timeout);
48: }
49: } catch (InterruptedException ex) {
50: Thread.interrupted();
51: }
52:
53: return message;
54: }
55:
56: /**
57: * The received message
58: */
59: private Message message;
60:
61: /**
62: * The object to wait on
63: */
64: private Object lock = new Object();
65: }
|