【问题标题】:Not receiving message from producer using ActiveMQ VM Transport使用 ActiveMQ 虚拟机传输未收到来自生产者的消息
【发布时间】: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


【解决方案1】:

我确定,vm 是 VM 内部的传输!并且无法在其外部访问,因此解决方案是 2 个客户端之一需要使用 vm 传输,而另一个 tcp 和 ActiveMQ 在使用 vm 传输的一个中启动,或者将您的 2 个组件嵌入到同一个 VM 中。

查看我对同一用例的另一个答案 How to send Jms message from one spring-boot application to another when both apps use embedded activemq

【讨论】:

  • 感谢您的解释。您在链接中的回答非常有帮助。我将尝试在我的测试用例中实现它。我的经理是一名 C 程序员,所以我需要捍卫 ActiveMQ (JMS) 对于多层应用程序来说足够快。
猜你喜欢
  • 2015-06-19
  • 2013-08-13
  • 1970-01-01
  • 2020-09-06
  • 1970-01-01
  • 2019-07-30
  • 1970-01-01
  • 2019-09-24
  • 1970-01-01
相关资源
最近更新 更多