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

JMS: Headers

Header fields contains some important information, this is used by client and providers to identify and route messages.

Below table summarize JMS headers.

Header
Data Type
Description
JMSDestination
String
Specifies the destination to which the message is being sent.
JMSDeliveryMode
String
JMS supports two delivery modes.
a.   NON_PERSISTENT
b.   PERSISTENT

NON_PERSISTENT: Message is not stored in stable storage. A JMS provider failure can cause this message to be lost.

PERSISTENT: JMS message is stored in persistent storage. Message is not lost on JMS provider failures.
JMSMessageID
String
It is a unique ID, that uniquely identifies each message sent by a provider. All JMSMessageID values must start with the prefix 'ID:'.
JMSTimestamp
long
It represents the time a message was handed off to a provider to be sent. Time is represented in milliseconds.
JMSCorrelationID
string
It 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.


JMSReplyTo
string
It is the destination where a reply to the message should be sent. This header field is supplied by a client when a message is sent.
JMSRedelivered
boolean
Provider set this header field value to true, when it is redelivering the message to client.

When the provider set this flag to true, it means that the message is delivered previously, but provider do not receive any acknowledgement from the client.
JMSType
string
The JMSType header field contains a message type identifier supplied by a client when a message is sent.
JMSExpiration
long
Specifies the expiration time of the message. It is represented in milliseconds.

When a message is sent, the JMS provider calculates its expiration time by adding the time-to-live value specified on the send method to the time the message was sent.

Example
java.util.Date date = new java.util.Date();
long currentTimeInMillis = date.getTime();
long expiration = currentTimeInMillis + 60000;
message.setJMSExpiration(expiration);

Above statements set the expiration time to 1 minute.
JMSPriority
integer
Priority of the jms message. JMS defines 10 priority values from 0 to 9.

0 – Lowest priority
9 – Highest priority
JMSDeliveryTime
long
When a message is sent, the JMSDeliveryTime header field is left unassigned. After completion of the send or publish method, it holds the delivery time of the message

Not all these header fields are set by client, some are set by client and some are set by JMS provider. Below table summarizes this.

Header Field
Set By
Java API
JMSDestination
JMS provider send method
setJMSDestination (not for client use)
JMSDeliveryMode
JMS provider send method
setJMSDeliveryMode(not for client use)
JMSExpiration
JMS provider send method
setJMSExpiration
(not for client use)
JMSDeliveryTime
JMS provider send method
setJMSDeliveryTime
(not for client use)
JMSPriority
JMS provider send method
setJMSPriority
(not for client use)
JMSMessageID
JMS provider send method
setJMSMessageID
(not for client use)
JMSTimestamp
JMS provider send method
setJMSTimestamp
(not for client use)
JMSCorrelationID
Client application
setJMSCorrelationID, setJMSCorrelationIDAsBytes
JMSReplyTo
Client application
setJMSReplyTo
JMSType
Client application
setJMSType
JMSRedelivered
JMS provider prior to delivery
setJMSRedelivered
(not for client use)

Previous                                                 Next                                                 Home

JMS Messages


Each JMS message contains Headers, properties and the body.

Headers
Header field contains some important information, this is used by client and providers to identify and route messages.

Message Properties
Properties are used to add optional headers fields to a message. You can specify Application specific properties, provider specific properties using optional header fields.

Body
JMS support both text and binary messages.

In my next post, I will explain the header fields in detail.



Previous                                                 Next                                                 Home

JMS Features


a.   Delay the delivery of Message: Producer can specify that a message must not be delivered until after a specified time interval.
b.   Messages can be send asynchronously.
c.   JMS providers must set the JMSXDeliveryCount message property. It specifies the number of delivery attempts for this message.
d.   Applications can create a durable or non-durable topic subscription may now designate them to be “shared”. A shared subscription may have multiple consumers.

Previous                                                 Next                                                 Home

JMS Domains

JMS supports two kinds of messaging models.

a.   Point-to-Point model
b.   Publish-Subscribe model

Point-to-Point Model
In Point-to-Point model, client1 sends messages to client2 via a Queue.




a.   Client 1 send message ‘M’ to the queue.
b.   Client 2 reads the message ‘M’ from queue
c.   Client 2 acknowledges that ‘it received the message’

Publish-Subscribe model
In Publish-subscribe model, client send the message to the topic. All the subscribers subscribe to the topic to receive the messages.




a.   Client 1 publish the message to a topic.
b.   Client 2, Client 3…Client N subscribe to this topic to receive the messages.
c.   Topic deliver the messages to all the subscribed clients.

Previous                                                 Next                                                 Home

Introduction To JMS

JMS stands for Java Message Service, it is an open standard, that documents common way to create, send, receive and read an enterprise messaging system’s messages.

JMS documents, how the asynchronous communication between enterprise applications takes place.

Why JMS?
Prior to JMS specification, every messaging service vendor has their own implementation of messaging apis, it is quite difficult to a user to migrate from one messaging vendor to other. Developers end up in writing vendor specific implementation. JMS exists to address these issues.

By using JMS client, you can able to connect to any JMS compliant provider applications like Active MQ, Rabbit MQ etc.,

What are the different versions of JMS specification?
At the time of writing this tutorial, JMS 2.0 is the latest.

·       JMS 1.0.2b (June 26, 2001)
·       JMS 1.1 (April 12, 2002)
·       JMS 2.0 (May 21, 2013)

What are other products that implements JMS specification?
There are multiple products that implement JMS specification.

·       Amazon SQS with Java Messaging Library
·       Apache ActiveMQ
·       Apache Qpid
·       Oracle Weblogic
·       EMS from TIBCO
·       FFMQ, GNU LGPL licensed
·       FioranoMQ
·       IBM MQ
·       JBoss Messaging and HornetQ from JBoss
·       JORAM, from the OW2 Consortium
·       Lightstreamer JMS Extender
·       Open Message Queue, from Oracle
·       OpenJMS, from The OpenJMS Group
·       RabbitMQ, from Pivotal (JMS Client for RabbitMQ)
·       Solace JMS from Solace Systems
·       SAP NetWeaver Process Integration
·       SonicMQ from Aurea Software
·       SwiftMQ
·       Tervela
·       Ultra Messaging
·       webMethods from Software AG
·       WebSphere Application Server from IBM

What are the components of a JMS Application?
A JMS Application contains below 5 components.
a.   JMS Clients
b.   Non-JMS Clients
c.   Messages
d.   JMS provider
e.   Administered Objects

JMS Clients
These clients are JMS compliant ones, used to send/receive data from a JMS provider.

Non-JMS Clients
These clients use Message system native APIs, instead of JMS APIs.

Messages
Represents the piece of information, that is communicated between clients.

JMS Provider
It is a software that implement JMS specification. For example, Rabbit MQ and ActiveMQ are JMS providers.

Administered Objects
Administered objects are preconfigured JMS objects (For ex: Queues, Topics) created by an administrator for the use of clients.




Previous                                                 Next                                                 Home

ActiveMQ: Delete a queue programmatically in Java

In this post, I am going to show you how can you delete a queue programmatically.

If the ActiveMQ instance is running on port 5050, you can see all the queues from below url.



As you see above image, you can able to see two queues ‘DEMO_QUEUE’ and ‘Hello World’ queues there.

Test.java
package com.sample.app;

import javax.jms.ConnectionFactory;
import javax.jms.JMSException;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

public class Test {
 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);
  ActiveMQConnection connection = (ActiveMQConnection) connectionFactory.createConnection();

  connection.destroyDestination(new ActiveMQQueue(QUEUE));
  
  connection.close();
 }
}

When you ran above application, it deletes the queue ‘DEMO_QUEUE’.


In my next posts, you can learn more about JMS specification.

Previous                                                 Next                                                 Home