如何在ActiveMQ JMS队列中设置消息ID?

4

我已经下载了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();
        }

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

如何将消息ID添加到队列消息中?

2个回答

8

根据规范,用户无法设置JMSMessageID值。这是特定于JMS提供程序的。

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

谢谢,Aniket。是否可以根据JMS提供程序设置消息ID?我想要消息ID。还有其他可能吗?你能再解释一下吗? - Ami
1
不,客户端应用程序无法设置JMSMessageID。因此,如果您想要关联消息,则可以使用特定于客户端应用程序的JMSCorrelationID。 - Aniket Thakur
是的,我已经使用了 correlationID 而不是 messageID。但问题在于 correlation ID 不是唯一的。你可以为单个 correlation 指定多条消息。现在它会创建一个重复项。我该如何找到并检索特定的消息? - Ami
如果您正在使用同一会话创建发送器和接收器,则可以在消息发送后记录设置的JMSMessageID,然后在检索时检查相同的ID。 - Aniket Thakur
或者,如果您想要检索特定的消息,则在发送时设置一些唯一的头属性,例如message.setStringProperty(id),然后使用消息选择器来检索消息。 - Aniket Thakur

-1

您可以为每个消息设置参数:

message.setStringProperty("property_name",property_val);

这样你就可以在生产者和消费者之间传递参数。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接