1 16 package org.apache.activemq.usecases; 17 18 import org.apache.activemq.ActiveMQConnectionFactory; 19 import org.apache.activemq.broker.BrokerService; 20 import org.apache.activemq.spring.ConsumerBean; 21 22 import javax.jms.Connection ; 23 import javax.jms.MessageConsumer ; 24 import javax.jms.MessageProducer ; 25 import javax.jms.Queue ; 26 import javax.jms.Session ; 27 import javax.jms.TextMessage ; 28 29 import junit.framework.TestCase; 30 31 35 public class StartAndStopClientAndBrokerDoesNotLeaveThreadsRunningTest extends TestCase { 36 37 public static interface Task { 38 public void execute() throws Exception ; 39 } 40 41 public void setUp() throws Exception { 42 } 43 44 public void testStartAndStopClientAndBrokerAndCheckNoThreadsAreLeft() throws Exception { 45 runTest(new Task() { 46 47 public void execute() throws Exception { 48 BrokerService broker = new BrokerService(); 49 broker.setPersistent(false); 50 broker.start(); 51 52 ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost"); 53 Connection connection = factory.createConnection(); 54 connection.start(); 55 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 56 Queue destination = session.createQueue(getName()); 57 58 MessageConsumer consumer = session.createConsumer(destination); 60 ConsumerBean listener = new ConsumerBean(); 61 consumer.setMessageListener(listener); 62 63 MessageProducer producer = session.createProducer(destination); 65 TextMessage message = session.createTextMessage("Hello World!"); 66 producer.send(message); 67 producer.close(); 68 69 listener.assertMessagesArrived(1); 70 71 consumer.close(); 72 session.close(); 73 connection.close(); 74 75 broker.stop(); 76 } 77 }); 78 } 79 80 public void runTest(Task task) throws Exception { 81 int numThreads = Thread.currentThread().getThreadGroup().activeCount(); 82 84 task.execute(); 85 86 Thread.yield(); 87 Thread.sleep(2000); 89 int activeCount = Thread.currentThread().getThreadGroup().activeCount(); 91 assertTrue("Should be at most one more thread but was: " + activeCount, numThreads + 1 <= activeCount); 92 } 93 } 94 | Popular Tags |