2017-02-16 52 views
0

我正在使用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(); 
+0

你能後啓動消費者和生產者的代碼? 您確定生產者在消費者啓動之前發送消息嗎? –

+0

@HassenBennour代碼正在通過SpringBootApplication CommandLineRunner運行。我已將消費者的超時時間設置爲10秒。然後我在那段時間內執行jar。我也嘗試了另一種方式,但仍然沒有收到味精。根據我的製片人的println。 msg已發送。沒有發現異常。 – totoDaryl

+0

正如我所瞭解的消費者和生產者是在不同的罐子裏,但在相同的SpringBootApplication和相同的JVM權利? –

回答

1

我確信,VM爲VM中的運輸!並且無法在其外部訪問,所以解決方案是,其中一個客戶端需要使用vm傳輸,另一個使用vm傳輸啓動另一個tcp和ActiveMQ,或者將您的2個組件嵌入到同一個VM中。

看到我的另一個答案用於相同用途的情況下 How to send Jms message from one spring-boot application to another when both apps use embedded activemq

+0

感謝您的解釋。您在鏈接中的回答非常有幫助。我會嘗試在我的測試案例中實現它。我的經理是C程序員,所以我需要捍衛ActiveMQ(JMS)足夠快的多層應用程序。 – totoDaryl

相關問題