我编写了一个使用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);
}
}
}
最佳答案
etc / hosts文件上的错误记录导致此问题。清除后问题解决。