我写了一个使用 activemq 嵌入式代理的小程序。程序在一台机器上成功运行,但不能在另一台机器上运行。两台服务器都有 sun os 10 & java 7 以及 activemq 5.5.1。我的意思是不起作用,新的 BrokerService() 构造函数调用不会返回一段时间(几分钟)。片段代码如下所示。
谢谢建议。。
public static void main(String[] args) {
// ....
try {
p.start("tcp://192.168.4.2:61616");
} catch (MessagingException e) {
e.printStackTrace();
}
// ....
}
public void start(String brokerAddress) throws MessagingException {
try {
System.out.println(">>> initialize 2");
broker = new JmsBroker(brokerAddress, brokerAddress.substring(6));
System.out.println(">>> initialize 3");
broker.start();
System.out.println(">>> initialize 4");
messageProducer = new JmsProducer(brokerAddress,
"MESSAGING_IF_NAME",
false,
5000);
System.out.println(">>> initialize 5");
} catch (JMSException e) {
System.out.println(e);
}
}
public class JmsBroker extends Thread {
private BrokerService broker;
private Object lock;
private static final Logger logger = LoggerFactory.getLogger(JmsBroker.class);
private String connector;
public JmsBroker(String jmsAddress, String brokerName) throws MessagingException {
broker = new BrokerService(); // !!!!! PROBLEM
broker.setBrokerName(brokerName);
broker.setUseJmx(true);
broker.setUseLoggingForShutdownErrors(true);
broker.setSchedulerSupport(false);
broker.setPersistent(false);
connector = jmsAddress;
try {
System.out.println(">>> s1");
broker.addConnector(connector);
System.out.println(">>> s2");
broker.start(true);
System.out.println(">>> s3");
while (!broker.isStarted()) {
Thread.sleep(10);
System.out.println("BROKER NOT STARTED");
}
logger.info("JMS BROKER STARTED");
System.out.println("JMS BROKER STARTED");
} catch (InterruptedException e) {
throw new MessagingException(e);
} catch (Exception e) {
throw new MessagingException(e);
}
}
public void run() {
try {
lock = new Object();
synchronized (lock) {
lock.wait();
}
} catch (Exception e) {
logger.error("", e);
}
}
}