【发布时间】:2017-07-05 05:34:46
【问题描述】:
我现在正在使用 activeMQ tcp//localhost URL 来安静一段时间,我对此没有任何问题。目前,我正在尝试使用“vm//localhost”连接器,但在接收来自生产者的消息时遇到问题。我正在使用弹簧靴,生产者和消费者在不同的罐子里。我的消费者收到一条空消息。我错过了什么吗?下面是我的代码(取自 apache 网站)。提前致谢
生产者.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();
【问题讨论】:
-
你能发布启动消费者和生产者的代码吗?你确定生产者在消费者开始之前发送消息吗?
-
@HassenBennour 代码正在通过 SpringBootApplication CommandLineRunner 运行。我已将消费者的超时时间设置为 10 秒。然后我在这段时间内执行 jar。我也尝试了相反的方法,但仍然没有收到消息。根据我的制片人的 println 。味精已发送。没有发现异常。
-
据我了解,消费者和生产者在不同的 jar 中,但在同一个 SpringBootApplication 和同一个 JVM 上,对吗?
-
@HassenBennour 不幸的是他们在不同的 SpringBootApplication 上,这意味着不同的 JVM。
标签: java spring-boot activemq