KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.usecases;
19
20 import junit.framework.TestCase;
21 import org.apache.activemq.ActiveMQConnectionFactory;
22 import org.apache.activemq.broker.BrokerService;
23 import org.apache.activemq.broker.BrokerFactory;
24 import org.apache.activemq.broker.TransportConnector;
25
26 import javax.jms.JMSException JavaDoc;
27 import java.net.URI JavaDoc;
28
29 /**
30  * @author Oliver Belikan
31  * @version $Revision: 1.1.1.1 $
32  */

33 public class StartAndStopBrokerTest extends TestCase {
34     public void testStartupShutdown() throws Exception JavaDoc {
35         // This systemproperty is used if we dont want to
36
// have persistence messages as a default
37
System.setProperty("activemq.persistenceAdapter",
38                 "org.apache.activemq.store.vm.VMPersistenceAdapter");
39
40         // configuration of container and all protocolls
41
BrokerService broker = createBroker();
42
43         // start a client
44
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:9100");
45         factory.createConnection();
46
47         // stop activemq broker
48
broker.stop();
49
50         // start activemq broker again
51
broker = createBroker();
52
53         // start a client again
54
factory = new ActiveMQConnectionFactory("tcp://localhost:9100");
55         factory.createConnection();
56
57         // stop activemq broker
58
broker.stop();
59
60     }
61
62     protected BrokerService createBroker() throws JMSException JavaDoc {
63         BrokerService broker = null;
64
65         try {
66             broker = BrokerFactory.createBroker(new URI JavaDoc("broker://()/localhost"));
67             broker.setBrokerName("DefaultBroker");
68             broker.addConnector("tcp://localhost:9100");
69             broker.setUseShutdownHook(false);
70             
71             broker.start();
72         } catch (Exception JavaDoc e) {
73             e.printStackTrace();
74         }
75
76         return broker;
77     }
78
79 }
80
Popular Tags