Sunday 5 August 2018

JMS: When Can I use JMSCorrelationID?

JMSCorrelationID is used to link one message with another. Mostly it is used to link a response message with its related request message.

JMSCorrelationID can hold one of the following:
a.   A provider-specific message ID
b.   An application-specific String
c.   A provider-native byte[] value.

Applications can use this header to link the messages. Application-specified values must not start with the 'ID:' prefix; this is reserved for provider-generated message ID values.

How to set the correlation Id?
javax.jms.Message interface provides below methods to set the correlation id.
void setJMSCorrelationIDAsBytes(byte[] correlationID) throws JMSException;
void setJMSCorrelationID(String correlationID) throws JMSException;

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.");
  message.setJMSCorrelationID("Message1_1524810163656");

  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 jmsCorrelationId = message.getJMSCorrelationID();
  byte[] jmsCorrelationIDAsBytes = message.getJMSCorrelationIDAsBytes();

  System.out.println("jmsCorrelationId : " + jmsCorrelationId);
  System.out.print("jmsCorrelationIDAsBytes : ");

  printByteArray(jmsCorrelationIDAsBytes);

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

  connection.close();
 }

 private static void printByteArray(byte[] arr) {
  for (byte b : arr) {
   System.out.print(b + ",");
  }
 }
}


Run JMSProducer.java followed by JMSConsumer.java.

Output
jmsCorrelationId : Message1_1524810163656
jmsCorrelationIDAsBytes : 77,101,115,115,97,103,101,49,95,49,53,50,52,56,49,48,49,54,51,54,53,54,
Received message 'Hello, this is my first message.'






Previous                                                 Next                                                 Home

No comments:

Post a Comment