Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » systest » impl » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.activemq.systest.impl;
   18   
   19   import org.apache.activemq.ActiveMQConnectionFactory;
   20   import org.apache.activemq.broker.BrokerService;
   21   import org.apache.activemq.network.NetworkConnector;
   22   import org.apache.activemq.systest.AgentStopper;
   23   import org.apache.activemq.systest.AgentSupport;
   24   import org.apache.activemq.systest.BrokerAgent;
   25   import org.apache.commons.logging.Log;
   26   import org.apache.commons.logging.LogFactory;
   27   
   28   import javax.jms.ConnectionFactory;
   29   
   30   /**
   31    * A simple in-memory broker implementation
   32    * 
   33    * @version $Revision: 1.1 $
   34    */
   35   public class BrokerAgentImpl extends AgentSupport implements BrokerAgent {
   36       private static final Log log = LogFactory.getLog(BrokerAgentImpl.class);
   37   
   38       private static int counter;
   39       private static int port = 61616;
   40   
   41       private BrokerService broker;
   42       private String brokerName;
   43       private boolean persistent;
   44       private String connectionURI;
   45       private boolean started;
   46       private boolean deleteAllMessage=true;
   47   
   48       public BrokerAgentImpl() throws Exception {
   49           brokerName = "broker-" + (++counter);
   50           connectionURI = "tcp://localhost:" + (port++);
   51           
   52           log.info("Creating broker on URI: " + getConnectionURI());
   53       }
   54   
   55       public void kill() throws Exception {
   56           stop();
   57       }
   58   
   59       public ConnectionFactory getConnectionFactory() {
   60           return new ActiveMQConnectionFactory(getConnectionURI());
   61       }
   62   
   63       public String getConnectionURI() {
   64           return connectionURI;
   65       }
   66   
   67       public void connectTo(BrokerAgent remoteBroker) throws Exception {
   68           String remoteURI = "static://"+remoteBroker.getConnectionURI();
   69           log.info("Broker is connecting to network using: " + remoteURI);
   70           NetworkConnector connector = getBroker().addNetworkConnector(remoteURI);
   71           if (started) {
   72               connector.start();
   73           }
   74       }
   75   
   76       public void start() throws Exception {
   77           started = true;
   78           getBroker().start();
   79       }
   80   
   81       public void stop(AgentStopper stopper) {
   82           started = false;
   83           if (broker != null) {
   84               try {
   85                   broker.stop();
   86               }
   87               catch (Exception e) {
   88                   stopper.onException(this, e);
   89               }
   90               finally {
   91                   broker = null;
   92               }
   93           }
   94       }
   95   
   96       public boolean isPersistent() {
   97           return persistent;
   98       }
   99   
  100       
  101       public boolean isStarted() {
  102           return started;
  103       }
  104   
  105       public void setPersistent(boolean persistent) {
  106           this.persistent = persistent;
  107       }
  108   
  109       public BrokerService getBroker() throws Exception {
  110           if (broker == null) {
  111               broker = createBroker();
  112           }
  113           return broker;
  114       }
  115   
  116       protected BrokerService createBroker() throws Exception {
  117           BrokerService answer = new BrokerService();
  118           answer.setBrokerName(brokerName);
  119           answer.setPersistent(isPersistent());
  120           
  121           // Delete all the message the first time the broker is started.
  122           answer.setDeleteAllMessagesOnStartup(deleteAllMessage);
  123           deleteAllMessage=false;
  124           
  125           answer.addConnector(getConnectionURI());
  126           return answer;
  127       }
  128   
  129       public String getBrokerName() {
  130           return brokerName;
  131       }
  132       
  133       
  134   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » systest » impl » [javadoc | source]