KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > usecases > StartAndStopClientAndBrokerDoesNotLeaveThreadsRunningTest


1 /*
2  * Copyright 2005-2006 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.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 JavaDoc;
23 import javax.jms.MessageConsumer JavaDoc;
24 import javax.jms.MessageProducer JavaDoc;
25 import javax.jms.Queue JavaDoc;
26 import javax.jms.Session JavaDoc;
27 import javax.jms.TextMessage JavaDoc;
28
29 import junit.framework.TestCase;
30
31 /**
32  *
33  * @version $Revision: $
34  */

35 public class StartAndStopClientAndBrokerDoesNotLeaveThreadsRunningTest extends TestCase {
36
37     public static interface Task {
38         public void execute() throws Exception JavaDoc;
39     }
40
41     public void setUp() throws Exception JavaDoc {
42     }
43
44     public void testStartAndStopClientAndBrokerAndCheckNoThreadsAreLeft() throws Exception JavaDoc {
45         runTest(new Task() {
46
47             public void execute() throws Exception JavaDoc {
48                 BrokerService broker = new BrokerService();
49                 broker.setPersistent(false);
50                 broker.start();
51
52                 ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost");
53                 Connection JavaDoc connection = factory.createConnection();
54                 connection.start();
55                 Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
56                 Queue JavaDoc destination = session.createQueue(getName());
57
58                 // consumer
59
MessageConsumer JavaDoc consumer = session.createConsumer(destination);
60                 ConsumerBean listener = new ConsumerBean();
61                 consumer.setMessageListener(listener);
62
63                 // producer
64
MessageProducer JavaDoc producer = session.createProducer(destination);
65                 TextMessage JavaDoc 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 JavaDoc {
81         int numThreads = Thread.currentThread().getThreadGroup().activeCount();
82 // Thread.currentThread().getThreadGroup().list();
83

84         task.execute();
85
86         Thread.yield();
87         Thread.sleep(2000); // Wait for the threads to exit on their own
88

89 // Thread.currentThread().getThreadGroup().list();
90
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