gpt4 book ai didi

java - 如何在 Activemq jms 队列中设置消息 ID?

转载 作者:搜寻专家 更新时间:2023-11-01 02:26:30 26 4
gpt4 key购买 nike

我已经下载了activemq 5.8.0版本并编写了创建队列的示例程序。我已成功将示例消息发送到队列。

之后,我尝试将消息 ID 设置为特定消息。消息 ID 可用于检索特定消息。我已尝试使用 message.setJMSMessageID("1234"); 设置消息 ID。

 public static void messagestoQueueu(){

// JMS messages are sent and received using a Session. We will
// create here a non-transactional session object. If you want
// to use transactions you should set the first parameter to 'true'
Session session;
try {
// Getting JMS connection from the server and starting it
ConnectionFactory connectionFactory =
new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);

// Destination represents here our queue 'TESTQUEUE' on the
// JMS server. You don't have to do anything special on the
// server to create it, it will be created automatically.
Destination destination = session.createQueue("test");

// MessageProducer is used for sending messages (as opposed
// to MessageConsumer which is used for receiving them)
MessageProducer producer = session.createProducer(destination);

// We will send a small text message saying 'Hello' in Japanese
//BytesMessage byteMessage = session.create;


TextMessage message = session.createTextMessage();
message.setJMSType("sample");
message.setJMSMessageID("1234");
message.setText("sample");


message.setJMSCorrelationID("choole");
message.setJMSMessageID("choo01");
message.setJMSReplyTo(destination);

producer.send(queue, message);
// Here we are sending the message!
producer.send(message);
System.out.println(message.getJMSMessageID()+" "+message.getJMSCorrelationID());
//System.out.println("Sent message '" + message.getText() + "'");

connection.close();
producer.close();
session.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

但它不起作用。在我使用 getJMSMessageID() 打印它时设置消息 ID 后,它会打印随机值。

如何给队列消息添加消息ID?

最佳答案

根据规范,用户不能设置 JMSMessageID 值。它是特定于 JMS 提供程序的。

When a message is sent, JMSMessageID is ignored. When the send method returns
it contains a provider-assigned value.

关于java - 如何在 Activemq jms 队列中设置消息 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21624407/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com