我现在使用activeMQ tcp // localhost 网址安静一段时间,我没有遇到任何问题。目前,我正在尝试使用" vm // localhost "连接器,但我收到生产者的消息有问题。我使用的是春靴,生产商和消费者都在不同的罐子里。我的消费者正在收到一条空消息。我错过了什么吗?下面是我的代码(从apache网站获取)。提前致谢
Producer.jar
ActiveMQConnectionFactory connectionFactory =
new ActiveMQConnectionFactory("vm://localhost");
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("TEST.FOO");
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
String text = "Hello world! From: " + Thread.currentThread().getName() + " : " + this.hashCode();
TextMessage message = session.createTextMessage(text);
System.out.println("Sent message: " + message.hashCode() + " : " + Thread.currentThread().getName());
producer.send(message);
session.close();
connection.close();
Consumer.jar
ActiveMQConnectionFactory connectionFactory =
new ActiveMQConnectionFactory("vm://localhost");
Connection connection = connectionFactory.createConnection();
connection.start();
connection.setExceptionListener(this);
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("TEST.FOO");
MessageConsumer consumer = session.createConsumer(destination);
// Wait for a message
Message message = consumer.receive(10000);
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
System.out.println("Received 1: " + text);
} else {
System.out.println("Received 2: " + message);
}
consumer.close();
session.close();
connection.close();
答案 0 :(得分:1)
我确定,vm是VM内部的传输!并且无法在其外部访问,因此解决方案是2个客户端中的一个需要使用vm传输,另一个使用vm传输启动tcp和ActiveMQ,或者将2个组件嵌入到同一个VM中。
看到我对同一个用例的另一个答案 How to send Jms message from one spring-boot application to another when both apps use embedded activemq