JMSTimestamp Example

In this post under JMS, I will explain about JMSTimestamp with an example

Note: I will be using JMS 2.0 api for this and future examples.

JMSTimestamp is a header field which is only set by JMS Provider and contains a long value in milliseconds that represents the time when the message was handed over to the provider.

JMSTimestamp is set by the provider when the JMSProducer calls the send method.

Before calling the JMSProducer’s send method, the value will be 0 denoting no value, but after calling the send method, the value will be there.

Below is Producer class example which shows the usage of JMSMessageID.


1  package package12;
3  import javax.jms.ConnectionFactory;
4  import javax.jms.Destination;
5  import javax.jms.JMSContext;
6  import javax.jms.JMSProducer;
7  import javax.jms.Message;
9  public class Producer implements Runnable {
10 	private Destination destination;
11 	private ConnectionFactory connectionFactory;
13 	public Producer(Destination destination, ConnectionFactory connectionFactory) {
14 		this.destination = destination;
15 		this.connectionFactory = connectionFactory;
16 	}
18 	@Override
19 	public void run() {
20 		JMSContext jmsContext =  null;
22 		try {
23 			jmsContext = connectionFactory.createContext();
24 			Message message = jmsContext.createTextMessage("Hello World1");
25 			JMSProducer jmsProducer = jmsContext.createProducer();
26 			System.out.println("Before sending JMSTimestamp: " + message.getJMSTimestamp());
27 			jmsProducer.send(destination, message);
28 			System.out.println("After sending JMSTimestamp: " + message.getJMSTimestamp());
29 		} catch(Exception excep) {
30 			excep.printStackTrace();
31 		} finally {
32 			if(jmsContext != null) {
33 				jmsContext.close();
34 			}
35 		}
36 	}
37 }

In the above code, at line 26, we are retrieving the value of JMSTimestamp before calling the send method. As a result the value will be 0

At line 28, we are again retrieving the value of JMSTimestamp but this time after calling the send method. As a result the value will be there or other than 0.

Below is the output


Before sending JMSTimestamp: 0
Hello World1
After sending JMSTimestamp: 1594478035126

Creating a JMSTimestamp is time consuming and increases message size. We can instruct the JMS Provider to not generate the timestamep by calling “setDisableMessageTimestamp” method. This works only if JMS Provider does not ignore the setting “setDisableMessageTimestamp””. If JMS Provider doesn’t ignore then the value of timestamp will always be 0.

Leave a Reply