我正在我的项目中实现Apache ActiveMQ Artemis,该项目连接到Artemis远程服务器。因此,我首先决定与生产者和消费者创建一个独立的项目。
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient;
import org.apache.activemq.artemis.utils.UUIDGenerator;
/**
* A simple JMS Queue example that creates a producer and consumer on a queue and sends then receives a message.
*/
public class QueueExample {
public static void main(final String[] args) throws Exception {
Connection connection = null;
try {
ConnectionFactory cf = ActiveMQJMSClient.createConnectionFactory("tcp://54.67.23.67:61616", UUIDGenerator.getInstance().generateStringUUID());
// Step 4.Create a JMS Connection
connection = cf.createConnection("test","test123");
// Step 5. Create a JMS Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 6. Create a JMS Message Producer
MessageProducer producer = session.createProducer(null);
// Step 7. Create a Text Message
TextMessage message1 = session.createTextMessage("This is a text message1");
Thread.sleep(1000l);
TextMessage message2 = session.createTextMessage("This is a text message2");
System.out.println("Sent message: " + message1.getText());
System.out.println("Sent message: " + message2.getText());
Destination destination = session.createQueue("TESTQUEUE");
// Step 8. Send the Message
producer.send(destination,message1);
producer.send(destination,message2);
// Step 9. Create a JMS Message Consumer
MessageConsumer messageConsumer = session.createConsumer(destination);
// Step 10. Start the Connection
connection.start();
// Step 11. Receive the message
TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
System.out.println("Received message: " + messageReceived.getText());
} finally {
// Step 12. Be sure to close our JMS resources!
if (initialContext != null) {
initialContext.close();
}
if (connection != null) {
connection.close();
}
}
}
}
我从生产者发送了2次消息,但是在消费时我只收到1条消息。以下是我收到的输出:
已收到
Sent message: This is a text message1
Sent message: This is a text message2
Received message: This is a text message1
期望
Sent message: This is a text message1
Sent message: This is a text message2
Received message: This is a text message1
Received message: This is a text message2
当我再次运行代码时,我得到Received message: This is a text message2
,但是我希望消息Message1和Message2同时出现。
这是怎么回事?谁能指导我,因为我尝试搜索此问题,但未找到结果。
我的Artemis服务器配置不正确,还是代码有问题?
答案 0 :(得分:0)
您正在看到预期的行为。呼叫receive
将最多返回1条消息。如果您想收到更多消息,则需要多次拨打receive
-对于您要接收的每条消息,都需要拨打一次,例如:
TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
System.out.println("Received message: " + messageReceived.getText());
messageReceived = (TextMessage) messageConsumer.receive(5000);
System.out.println("Received message: " + messageReceived.getText());
有关其他信息,请参见users.info