1 18 package org.apache.activemq; 19 20 import javax.jms.Connection ; 21 import javax.jms.JMSException ; 22 import javax.jms.Message ; 23 import javax.jms.MessageConsumer ; 24 import javax.jms.MessageProducer ; 25 import javax.jms.Session ; 26 import javax.jms.TextMessage ; 27 import javax.jms.Topic ; 28 29 import org.apache.activemq.ActiveMQConnectionFactory; 30 31 34 public class JmsConnectionStartStopTest extends TestSupport { 35 36 private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory 37 .getLog(JmsConnectionStartStopTest.class); 38 39 private Connection startedConnection; 40 private Connection stoppedConnection; 41 42 45 protected void setUp() throws Exception { 46 47 log.info(getClass().getClassLoader().getResource("log4j.properties")); 48 49 ActiveMQConnectionFactory factory = createConnectionFactory(); 50 startedConnection = factory.createConnection(); 51 startedConnection.start(); 52 stoppedConnection = factory.createConnection(); 53 } 54 55 58 protected void tearDown() throws Exception { 59 stoppedConnection.close(); 60 startedConnection.close(); 61 } 62 63 68 public void testStoppedConsumerHoldsMessagesTillStarted() throws JMSException { 69 Session startedSession = startedConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); 70 Session stoppedSession = stoppedConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); 71 72 Topic topic = startedSession.createTopic("test"); 74 MessageConsumer startedConsumer = startedSession.createConsumer(topic); 75 MessageConsumer stoppedConsumer = stoppedSession.createConsumer(topic); 76 77 MessageProducer producer = startedSession.createProducer(topic); 79 TextMessage message = startedSession.createTextMessage("Hello"); 80 producer.send(message); 81 82 Message m = startedConsumer.receive(1000); 84 assertNotNull(m); 85 86 m = stoppedConsumer.receive(1000); 87 assertNull(m); 88 89 stoppedConnection.start(); 90 m = stoppedConsumer.receive(5000); 91 assertNotNull(m); 92 93 startedSession.close(); 94 stoppedSession.close(); 95 } 96 97 102 public void testMultipleConnectionStops() throws Exception { 103 testStoppedConsumerHoldsMessagesTillStarted(); 104 stoppedConnection.stop(); 105 testStoppedConsumerHoldsMessagesTillStarted(); 106 stoppedConnection.stop(); 107 testStoppedConsumerHoldsMessagesTillStarted(); 108 } 109 } 110 | Popular Tags |