JMS Reliablity Mechanisms-Setting Message Priority Levels in JMS

We already discussed the fundamental concepts of JMS API. In the previous chapter we have seen the difference between persistent and non persistent message delivery modes.In this chapter we are discussing how setting message priority levels in JMS works.

Setting Message Priority Levels in JMS

It is possible to set the message priority levels before sending a message. It forces the JMS provider to deliver high priority messages first.JMS API has message priority levels from 0(lowest priority) to 9(highest priority).

If we have not specify any priority level , then the default priority will be 4.

In a JMS application , priority can be set in either of the following ways:

a)By using setPriority(int value) method of MessageProducer interface.

b)By using the overloaded publish() method.The third argument  will be the priority.

topicPublisher.publish(message, DeliveryMode.NON_PERSISTENT, 3,

20000)

Setting Message Priority Levels in JMS Example

We are using OpenJMS as JMS provider. Before looking into the codes , the openJMS should be configured and started, and the necessary libraries should be there in the project path as discussed earlier .

In our example , we have 3 clients. First client sends a low priority message to destination.Second client sends a high priority message to destination. Then the third client (which is the receiving client)is starting. As per our discussion so far , the high priority message should  be reached first and then the low priority message.

FirstClient.java

import java.util.Properties;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class FirstClient {
Context context = null;
ConnectionFactory factory = null;
Connection connection = null;
Destination destination = null;
Session session = null;
MessageProducer producer = null;
int priorityIndex = 0;

public FirstClient() {

}

public void sendMessage() {
Properties initialProperties = new Properties();
initialProperties.put(InitialContext.INITIAL_CONTEXT_FACTORY,
"org.exolab.jms.jndi.InitialContextFactory");
initialProperties.put(InitialContext.PROVIDER_URL,
"tcp://localhost:3035");

try {

context = new InitialContext(initialProperties);
factory = (ConnectionFactory) context.lookup("ConnectionFactory");
destination = (Destination) context.lookup("queue1");
connection = factory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
connection.start();
TextMessage message = session.createTextMessage();
message.setText("Hello ...This is a low priority message");
producer.setPriority(priorityIndex);
producer.send(message);
System.out.println("Sent low priority message: "
+ message.getText());

} catch (JMSException ex) {
ex.printStackTrace();
} catch (NamingException ex) {
ex.printStackTrace();
}

if (context != null) {
try {
context.close();
} catch (NamingException ex) {
ex.printStackTrace();
}
}

if (connection != null) {
try {
connection.close();
} catch (JMSException ex) {
ex.printStackTrace();
}
}
}

public static void main(String[] args) {
FirstClient firstClient = new FirstClient();
firstClient.sendMessage();
}

}

SecondClient.java

import java.util.Properties;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class SecondClient {
Context context = null;
ConnectionFactory factory = null;
Connection connection = null;
Destination destination = null;
Session session = null;
MessageProducer producer = null;
int priorityIndex = 9;

public SecondClient() {

}

public void sendMessage() {
Properties initialProperties = new Properties();
initialProperties.put(InitialContext.INITIAL_CONTEXT_FACTORY,
"org.exolab.jms.jndi.InitialContextFactory");
initialProperties.put(InitialContext.PROVIDER_URL,
"tcp://localhost:3035");

try {

context = new InitialContext(initialProperties);
factory = (ConnectionFactory) context.lookup("ConnectionFactory");
destination = (Destination) context.lookup("queue1");
connection = factory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
connection.start();
TextMessage message = session.createTextMessage();
message.setText("Hello ...This is a high priority message");
producer.setPriority(priorityIndex);
producer.send(message);
System.out.println("Sent high priority message: "
+ message.getText());

} catch (JMSException ex) {
ex.printStackTrace();
} catch (NamingException ex) {
ex.printStackTrace();
}

if (context != null) {
try {
context.close();
} catch (NamingException ex) {
ex.printStackTrace();
}
}

if (connection != null) {
try {
connection.close();
} catch (JMSException ex) {
ex.printStackTrace();
}
}
}

public static void main(String[] args) {
SecondClient secondClient = new SecondClient();
secondClient.sendMessage();
}

}

ReceivingClient.java

import java.util.Properties;
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 javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class ReceivingClient {
Context context = null;
ConnectionFactory factory = null;
Connection connection = null;
Destination destination = null;
Session session = null;
MessageConsumer consumer = null;

public ReceivingClient() {

}

public void receiveMessage() {
Properties initialProperties = new Properties();
initialProperties.put(InitialContext.INITIAL_CONTEXT_FACTORY,
"org.exolab.jms.jndi.InitialContextFactory");
initialProperties.put(InitialContext.PROVIDER_URL,
"tcp://localhost:3035");
try {
context = new InitialContext(initialProperties);
factory = (ConnectionFactory) context.lookup("ConnectionFactory");
destination = (Destination) context.lookup("queue1");
connection = factory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
consumer = session.createConsumer(destination);
connection.start();
Message firstMessage = consumer.receive();
if (firstMessage instanceof TextMessage) {
TextMessage text = (TextMessage) firstMessage;
System.out.println("First Message Received is : "
+ text.getText());
}
Message secondMessage = consumer.receive();
if (secondMessage instanceof TextMessage) {
TextMessage text = (TextMessage) secondMessage;
System.out.println("Second Message Received is : "
+ text.getText());
}

} catch (JMSException ex) {
ex.printStackTrace();
} catch (NamingException ex) {
ex.printStackTrace();
}

if (context != null) {
try {
context.close();
} catch (NamingException ex) {
ex.printStackTrace();
}
}

if (connection != null) {
try {
connection.close();
} catch (JMSException ex) {
ex.printStackTrace();
}
}
}

public static void main(String[] args) {
ReceivingClient receivingClient = new ReceivingClient();
receivingClient.receiveMessage();
}

}

Output

Start the OpenJMS by using startup script from %OPENJMS_HOME%/bin if not started already.

Then run the FirstClient.java.

Output of FirstClient.java

Sent low priority message: Hello …This is a low priority message

When the first client terminates , run the SecondClient.java.

Output of SecondClient.java

Sent high priority message: Hello …This is a high priority message

When the SecondClient.java exits its running , run the ReceivingClient.java . As per our discussion so far in this chapter , the second message sent to the destination should be received first. Because it has highest priority.

Output of  ReceivingClient.java

First Message Received is : Hello …This is a high priority message

Second Message Received  is : Hello …This is a low priority message

Conclusion

If  more messages are there in the destination to deliver to a particular client, then the delivery will be on the basis of priority. The above example verifies the same.

Leave a Reply

Your email address will not be published. Required fields are marked *