我正在使用activeMQ tcp //本地主機現在安靜一段時間的URL,我沒有問題。目前,我正嘗試使用「vm // localhost」連接器,但我在收到生產者發送的消息時遇到問題。我使用彈簧靴,生產者和消費者在不同的罐子裏。我的消費者正在收到一條空消息。我錯過了什麼嗎?以下是我的代碼(從apache網站獲取)。在此先感謝沒有收到生產者使用ActiveMQ VM傳輸的消息
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();
你能後啓動消費者和生產者的代碼? 您確定生產者在消費者啓動之前發送消息嗎? –
@HassenBennour代碼正在通過SpringBootApplication CommandLineRunner運行。我已將消費者的超時時間設置爲10秒。然後我在那段時間內執行jar。我也嘗試了另一種方式,但仍然沒有收到味精。根據我的製片人的println。 msg已發送。沒有發現異常。 – totoDaryl
正如我所瞭解的消費者和生產者是在不同的罐子裏,但在相同的SpringBootApplication和相同的JVM權利? –