How to set message id in Activemq jms queue?

I downloaded activemq version 5.8.0 and wrote a sample program for creating queues. I successfully sent a sample message to the queue.

After that, I tried to set the message identifier to a specific message. A message identifier can be used to retrieve a specific message. I tried to set the message id using message.setJMSMessageID("1234");.

 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();
        }

But it does not work. After setting the message id, when I print it with getJMSMessageID(), it prints random values.

How to add message id to queue message?

+4
source share
2 answers

JMSMessageID. JMS-.

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

:

message.setStringProperty("property_name",property_val);

, .

-1

Source: https://habr.com/ru/post/1525768/


All Articles