View Javadoc
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 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved. 42 * 43 * $Id: WaitingListener.java,v 1.3 2004/01/31 13:44:24 tanderson Exp $ 44 */ 45 package org.exolab.jmscts.core; 46 47 import javax.jms.Message; 48 import javax.jms.MessageListener; 49 50 import org.apache.log4j.Category; 51 52 import EDU.oswego.cs.dl.util.concurrent.Semaphore; 53 54 55 /*** 56 * This class implements the {@link MessageListener} interface and enables 57 * clients to synchronize behaviour with the {@link #onMessage} method. 58 * 59 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a> 60 * @version $Revision: 1.3 $ 61 */ 62 public class WaitingListener extends DelegatingListener { 63 64 /*** 65 * The message listener to delegate to 66 */ 67 private MessageListener _listener = null; 68 69 /*** 70 * The number of times that {@link #onMessage} is expected to be invoked. 71 * If set to -1, then {@link #onMessage} can be invoked any number of 72 * times. 73 */ 74 private int _count = -1; 75 76 /*** 77 * The number of times that {@link #onMessage} has been invoked. 78 */ 79 private int _invoked = 0; 80 81 /*** 82 * Semaphore used to synchronize on message receipt 83 */ 84 private Semaphore _receiptLock = new Semaphore(0); 85 86 /*** 87 * Semaphore used to synchronize message processing 88 */ 89 private Semaphore _processLock = new Semaphore(0); 90 91 /*** 92 * Semaphore used to synchronize process completion 93 */ 94 private Semaphore _completedLock = new Semaphore(0); 95 96 /*** 97 * The logger 98 */ 99 private static final Category _log = 100 Category.getInstance(WaitingListener.class); 101 102 103 /*** 104 * Construct an instance with the listener to delegate messages. 105 * The {@link #onMessage} method may be called any number of times. 106 * 107 * @param listener the message listener that messages will be delegated to 108 */ 109 public WaitingListener(MessageListener listener) { 110 this(listener, -1); 111 } 112 113 /*** 114 * Construct an instance with the listener to delegate messages, and 115 * the expected number of times that {@link #onMessage} will be invoked. 116 * 117 * @param listener the message listener that messages will be delegated to 118 * @param count the expected number of times that the {@link #onMessage} 119 * will be called 120 */ 121 public WaitingListener(MessageListener listener, int count) { 122 super(listener); 123 _count = count; 124 } 125 126 /*** 127 * Wait for the listener to receive a message 128 * 129 * @throws InterruptedException if interrupted 130 */ 131 public void waitForReceipt() throws InterruptedException { 132 _receiptLock.acquire(); 133 } 134 135 /*** 136 * Notify the listener that it may continue processing 137 */ 138 public void notifyContinue() { 139 _processLock.release(); 140 } 141 142 /*** 143 * Wait for the listener to complete processing 144 * 145 * @throws InterruptedException if interrupted 146 */ 147 public void waitForCompletion() throws InterruptedException { 148 _completedLock.acquire(); 149 } 150 151 /*** 152 * Invoked when the consumer asynchronously receives a message. 153 * On entry, it notifies the client blocked on {@link #waitForReceipt}. 154 * It then waits for the client to invoke {@link #notifyContinue}, 155 * before invoking the listener passed at construction. 156 * This enables clients to synchronize their behaviour with the listener. 157 * <br> 158 * On completion of the listener, it notifies the client blocked on 159 * {@link #waitForCompletion} 160 * <p> 161 * If an invocation count was specified at construction, and 162 * {@link #onMessage} is invoked more than the specified count, an error 163 * is logged, and the method returns immediately. 164 * 165 * @param message the received message 166 */ 167 public void onMessage(Message message) { 168 _log.debug("WaitingListener.onMessage() - begin"); 169 boolean fail = false; 170 synchronized (this) { 171 // notify the client that a message has been received 172 _receiptLock.release(); 173 if (_count != -1) { 174 if (++_invoked > _count) { 175 _log.error( 176 "WaitingListener.onMessage() has been invoked too " 177 + "many times. Expected invocation=" + _count 178 + ", current invocation=" + _invoked); 179 fail = true; 180 } 181 } 182 if (!fail) { 183 try { 184 _log.debug("WaitingListener.onMessage() - waiting"); 185 _processLock.acquire(); 186 _log.debug( 187 "WaitingListener.onMessage() - done waiting"); 188 } catch (InterruptedException ignore) { 189 _log.debug( 190 "WaitingListener.onMessage() - interrupted"); 191 } 192 } 193 } 194 195 try { 196 if (!fail) { 197 // delegate behaviour to the listener 198 super.onMessage(message); 199 } 200 } catch (RuntimeException exception) { 201 _log.error(exception, exception); 202 throw exception; 203 } finally { 204 _completedLock.release(); 205 _log.debug("WaitingListener.onMessage() - end"); 206 } 207 } 208 209 }

This page was automatically generated by Maven