Sunday, 5 August 2018

JMS: Why should I disable message ID?

JMSMessageID uniquely identifies each message sent by a provider. All JMSMessageID values must start with the prefix 'ID:'.

Since to achieve uniqueness across all the messages, message ID creation takes some effort, and it also increases the message size. But JMSMessageID may not be required in all the cases. In those cases, JMS Producers can hit providers, they do not require message id.

How can a JMS producer disable the message id?
javax.jms.MessageProducer and javax.jms.JMSProducer interfaces provide setDisableMessageID method, which allows the application to provide a hint to disable message ID.

Note
Even though JMS producer given a hint to disable message id, provider may not disable the message id.  If the JMS provider accepts this hint, these messages must have the message ID set to null; if the provider ignores the hint, the message ID must be set to its normal unique value.

JMSProducer.java
package com.sample.producer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class JMSProducer {
 private static final String JMS_PROVIDER_URL = "tcp://127.0.0.1:5050";
 private static final String QUEUE = "DEMO_QUEUE";

 public static void main(String[] args) throws JMSException {
  ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(JMS_PROVIDER_URL);
  Connection connection = connectionFactory.createConnection();
  connection.start();

  Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

  Destination destination = session.createQueue(QUEUE);

  MessageProducer producer = session.createProducer(destination);

  TextMessage message = session.createTextMessage("Hello, this is my first message.");

  producer.send(message);

  connection.close();
 }
}

JMSConsumer.java

package com.sample.consumer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class JMSConsumer {
 private static final String JMS_PROVIDER_URL = "tcp://127.0.0.1:5050";
 private static final String QUEUE = "DEMO_QUEUE";

 public static void main(String args[]) throws JMSException {
  ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(JMS_PROVIDER_URL);
  Connection connection = connectionFactory.createConnection();
  connection.start();

  Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

  Destination destination = session.createQueue(QUEUE);

  MessageConsumer consumer = session.createConsumer(destination);

  Message message = consumer.receive();

  String jmsMessageId = message.getJMSMessageID();

  System.out.println("jmsMessageId : " + jmsMessageId);

  if (message instanceof TextMessage) {
   TextMessage textMessage = (TextMessage) message;
   System.out.println("Received message '" + textMessage.getText() + "'");
  }

  connection.close();
 }
}


Run JMSProducer.java followed by JMSConsumer.java.

Output

jmsMessageId : ID:INLN50911363A-55694-1524808729129-1:1:1:1:1
Received message 'Hello, this is my first message.'

As you see the output, there is a unique id is assigned to this message.

Let’s disable the message id.

Add below statement in JMSProducer class and rerun JMSProducer Application.

producer.setDisableMessageID(true);

Re run the application ‘JMSConsumer.java’.

I got below output.

jmsMessageId : ID:INLN50911363A-55754-1524808920803-1:1:1:1:1
Received message 'Hello, this is my first message.'


As you observe the above output, even though I give hint to disable the message id, ActiveMQ is not ignoring my hint.


Previous                                                 Next                                                 Home

No comments:

Post a Comment