我正在连接到Artemis远程服务器的项目中实现Apache ActiveMQ 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
,但我想要同时发送消息消息 1 和消息 2。
这是怎么回事?任何人都可以指导我,因为我尝试搜索此问题但没有找到结果。
我的Artemis服务器配置不正确,还是代码存在问题?
您看到的是预期的行为。调用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());
有关其他信息,请参阅 JavaDoc for javax.jms.MessageConsumer.receive(long)