Sunday, 5 August 2018

JMS: Working with JMSTimestamp

JMSTimestamp represents the time that a message was handed off to a provider to be sent. Time is represented in milliseconds.

Since timestamps take some effort to create and increase a message’s size, some JMS providers may be able to optimize message overhead if they are given a hint that timestamp is not used by an application.

How can a JMS producer disable the timestamp?
javax.jms.MessageProducer and javax.jms.JMSProducer interfaces provide  setDisableMessageTimestamp method, which allows the application to provide a hint to disable time stamp.

Note
Even though JMS producer given a hint to disable time stamp, provider may not disable the time stamp.  If the JMS provider accepts this hint, these messages must have the time stamp set to zero; if the provider ignores the hint, the time stamp must be set to its normal 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();

  long jmsTimestamp = message.getJMSTimestamp();

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

  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
jmsTimestamp : 1524809522317
Received message 'Hello, this is my first message.'

Let’s give a hint to JMS provider that, we do not want time stamp.

How can I give a hint to JMS provider to disable time stamp?
Add below statement in 'JMSProducer' class.
producer.setDisableMessageTimestamp(true);

Re run JMSProducer application.

Re run JMSConsumer application.

I got below output.

jmsTimestamp : 0
Received message 'Hello, this is my first message.'


As you see the above message, I seen jmsTimeStamp is set to 0. That means ActiveMQ is respecting the hint given by jms producer.


Previous                                                 Next                                                 Home

No comments:

Post a Comment