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: ListenerCloseTest.java,v 1.7 2004/02/03 21:52:11 tanderson Exp $ 44 */ 45 package org.exolab.jmscts.test.session; 46 47 import javax.jms.Destination; 48 import javax.jms.MessageConsumer; 49 import javax.jms.Session; 50 51 import org.apache.log4j.Category; 52 53 import junit.framework.Test; 54 55 import org.exolab.jmscts.core.AbstractSendReceiveTestCase; 56 import org.exolab.jmscts.core.DelayedAction; 57 import org.exolab.jmscts.core.EchoListener; 58 import org.exolab.jmscts.core.MessageReceiver; 59 import org.exolab.jmscts.core.MessageSender; 60 import org.exolab.jmscts.core.MessagingBehaviour; 61 import org.exolab.jmscts.core.SessionHelper; 62 import org.exolab.jmscts.core.TestContext; 63 import org.exolab.jmscts.core.TestContextHelper; 64 import org.exolab.jmscts.core.TestCreator; 65 import org.exolab.jmscts.core.WaitingListener; 66 67 68 /*** 69 * This class tests the behaviour of stopping and closing a connection 70 * while a listener is active. 71 * 72 * NOTE: Temporary destinations cannot be used as a separate connection is 73 * required to verify that the test is successful. 74 * 75 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a> 76 * @version $Revision: 1.7 $ 77 * @jmscts.session all 78 * @jmscts.message TextMessage 79 * @jmscts.delivery NON_PERSISTENT, administered, asynchronous 80 * @jmscts.delivery PERSISTENT, administered, asynchronous 81 */ 82 public class ListenerCloseTest extends AbstractSendReceiveTestCase { 83 84 /*** 85 * The destination that the message listener receives messages on 86 */ 87 private static final String REQUEST = "requestClose"; 88 89 /*** 90 * The destination that the message listeners send messages to 91 */ 92 private static final String REPLY = "replyClose"; 93 94 /*** 95 * The destinations to create prior to running the test 96 */ 97 private static final String[] DESTINATIONS = {REQUEST, REPLY}; 98 99 /*** 100 * The logger 101 */ 102 private static final Category log = 103 Category.getInstance(ListenerCloseTest.class); 104 105 106 /*** 107 * Construct a new <code>ListenerCloseTest</code> 108 * 109 * @param name the name of test case 110 */ 111 public ListenerCloseTest(String name) { 112 super(name); 113 } 114 115 /*** 116 * Sets up the test suite 117 * 118 * @return an instance of this class that may be run by 119 * {@link org.exolab.jmscts.core.JMSTestRunner} 120 */ 121 public static Test suite() { 122 return TestCreator.createSendReceiveTest(ListenerCloseTest.class); 123 } 124 125 /*** 126 * Returns if this test can share resources with other test cases. 127 * This implementation always returns <code>false</code>, to 128 * ensure that a new connection is created for each test. 129 * 130 * @return <code>false</code> 131 */ 132 public boolean share() { 133 return false; 134 } 135 136 /*** 137 * Returns if the connection should be started prior to running the test. 138 * This implementation always returns <code>false</code> to avoid 139 * conflicts with test cases 140 * 141 * @return <code>false</code> 142 */ 143 public boolean startConnection() { 144 return false; 145 } 146 147 /*** 148 * Returns the list of destination names used by this test case. These 149 * are used to pre-create destinations prior to running the test case. 150 * 151 * @return the list of destinations used by this test case 152 */ 153 public String[] getDestinations() { 154 return DESTINATIONS; 155 } 156 157 /*** 158 * Verify that running MessageListeners have full access to session if 159 * the session is closed. 160 * 161 * @jmscts.requirement session.close.listeners 162 * @throws Exception for any error 163 */ 164 public void testSessionClose() throws Exception { 165 final int count = 10; // the number of messages to send 166 final long delayTime = 500; // 500 ms 167 TestContext context = getContext(); 168 Session session = context.getSession(); 169 MessagingBehaviour behaviour = context.getMessagingBehaviour(); 170 boolean durable = behaviour.getDurable(); 171 Destination request = getDestination(REQUEST); 172 173 // set up a duplicate context, to enable a receiver to be created 174 // for the destination that the listener replies to 175 TestContext local = TestContextHelper.createSendReceiveContext( 176 context); 177 MessageReceiver receiver = createReceiver(local, REPLY); 178 MessageConsumer consumer = null; 179 180 try { 181 // set up the listener 182 String name = (durable) ? SessionHelper.getSubscriberName() : null; 183 consumer = SessionHelper.createConsumer(context, request, name); 184 MessageSender sender = createSender(context, REPLY); 185 EchoListener replier = new EchoListener(context.getSession(), 186 sender, count, true); 187 final WaitingListener listener = new WaitingListener(replier, 1); 188 consumer.setMessageListener(listener); 189 190 // send a message and start the connection 191 send(request, 1); 192 context.getConnection().start(); 193 194 // wait for the listener to start processing before closing 195 // the session 196 log.debug("waiting for listener"); 197 listener.waitForReceipt(); 198 199 // notify the listener in a separate thread that it may proceed 200 // with processing. The notification has to occur in a separate 201 // thread else close would block indefinitely waiting for the 202 // listener to complete. 203 // The half second delay is to help ensure that close is invoked 204 // before the listener begins processing the message 205 log.debug("notifying listener"); 206 DelayedAction action = new DelayedAction(delayTime) { 207 public void runProtected() { 208 listener.notifyContinue(); 209 } 210 }; 211 action.start(); 212 session.close(); 213 } finally { 214 try { 215 consumer.close(); 216 } catch (Exception exception) { 217 fail("Attempting to invoke close() for a consumer on a " 218 + "closed session threw exception=" 219 + exception.getClass().getName() + ", message=" 220 + exception.getMessage()); 221 } 222 } 223 224 try { 225 // start the duplicate connection 226 local.getConnection().start(); 227 228 // verify that the listener sent a message 229 receive(receiver, count); 230 } finally { 231 receiver.remove(); 232 local.getConnection().close(); 233 } 234 } 235 236 }

This page was automatically generated by Maven