4
votes

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

After that I tried to set the message ID to the particular message. The message ID can be used to retrieve the specific message. I have 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's not working. After setting the Message ID when I print it using getJMSMessageID() it prints random values.

How to add a message ID to the queue message?

2

2 Answers

6
votes

As per specs user cannot set JMSMessageID value. It is JMS provider specific.

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

You can set parameters to each message:

message.setStringProperty("property_name",property_val);

That way you can pass parameters between the producer and consumer.