KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > systest > impl > BrokerAgentImpl


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.systest.impl;
19
20 import org.apache.activemq.ActiveMQConnectionFactory;
21 import org.apache.activemq.broker.BrokerService;
22 import org.apache.activemq.network.NetworkConnector;
23 import org.apache.activemq.systest.AgentStopper;
24 import org.apache.activemq.systest.AgentSupport;
25 import org.apache.activemq.systest.BrokerAgent;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import javax.jms.ConnectionFactory JavaDoc;
30
31 /**
32  * A simple in-memory broker implementation
33  *
34  * @version $Revision: 1.1 $
35  */

36 public class BrokerAgentImpl extends AgentSupport implements BrokerAgent {
37     private static final Log log = LogFactory.getLog(BrokerAgentImpl.class);
38
39     private static int counter;
40     private static int port = 61616;
41
42     private BrokerService broker;
43     private String JavaDoc brokerName;
44     private boolean persistent;
45     private String JavaDoc connectionURI;
46     private boolean started;
47     private boolean deleteAllMessage=true;
48
49     public BrokerAgentImpl() throws Exception JavaDoc {
50         brokerName = "broker-" + (++counter);
51         connectionURI = "tcp://localhost:" + (port++);
52         
53         log.info("Creating broker on URI: " + getConnectionURI());
54     }
55
56     public void kill() throws Exception JavaDoc {
57         stop();
58     }
59
60     public ConnectionFactory getConnectionFactory() {
61         return new ActiveMQConnectionFactory(getConnectionURI());
62     }
63
64     public String JavaDoc getConnectionURI() {
65         return connectionURI;
66     }
67
68     public void connectTo(BrokerAgent remoteBroker) throws Exception JavaDoc {
69         String JavaDoc remoteURI = "static://"+remoteBroker.getConnectionURI();
70         log.info("Broker is connecting to network using: " + remoteURI);
71         NetworkConnector connector = getBroker().addNetworkConnector(remoteURI);
72         if (started) {
73             connector.start();
74         }
75     }
76
77     public void start() throws Exception JavaDoc {
78         started = true;
79         getBroker().start();
80     }
81
82     public void stop(AgentStopper stopper) {
83         started = false;
84         if (broker != null) {
85             try {
86                 broker.stop();
87             }
88             catch (Exception JavaDoc e) {
89                 stopper.onException(this, e);
90             }
91             finally {
92                 broker = null;
93             }
94         }
95     }
96
97     public boolean isPersistent() {
98         return persistent;
99     }
100
101     
102     public boolean isStarted() {
103         return started;
104     }
105
106     public void setPersistent(boolean persistent) {
107         this.persistent = persistent;
108     }
109
110     public BrokerService getBroker() throws Exception JavaDoc {
111         if (broker == null) {
112             broker = createBroker();
113         }
114         return broker;
115     }
116
117     protected BrokerService createBroker() throws Exception JavaDoc {
118         BrokerService answer = new BrokerService();
119         answer.setBrokerName(brokerName);
120         answer.setPersistent(isPersistent());
121         
122         // Delete all the message the first time the broker is started.
123
answer.setDeleteAllMessagesOnStartup(deleteAllMessage);
124         deleteAllMessage=false;
125         
126         answer.addConnector(getConnectionURI());
127         return answer;
128     }
129
130     public String JavaDoc getBrokerName() {
131         return brokerName;
132     }
133     
134     
135 }
136
Popular Tags