1 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 ; 30 31 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 brokerName; 44 private boolean persistent; 45 private String connectionURI; 46 private boolean started; 47 private boolean deleteAllMessage=true; 48 49 public BrokerAgentImpl() throws Exception { 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 { 57 stop(); 58 } 59 60 public ConnectionFactory getConnectionFactory() { 61 return new ActiveMQConnectionFactory(getConnectionURI()); 62 } 63 64 public String getConnectionURI() { 65 return connectionURI; 66 } 67 68 public void connectTo(BrokerAgent remoteBroker) throws Exception { 69 String 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 { 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 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 { 111 if (broker == null) { 112 broker = createBroker(); 113 } 114 return broker; 115 } 116 117 protected BrokerService createBroker() throws Exception { 118 BrokerService answer = new BrokerService(); 119 answer.setBrokerName(brokerName); 120 answer.setPersistent(isPersistent()); 121 122 answer.setDeleteAllMessagesOnStartup(deleteAllMessage); 124 deleteAllMessage=false; 125 126 answer.addConnector(getConnectionURI()); 127 return answer; 128 } 129 130 public String getBrokerName() { 131 return brokerName; 132 } 133 134 135 } 136 | Popular Tags |