In this post under JMS, I will show with example how to set the priority of JMS messages before sending them.
Note: I will be using JMS 2.0 api for this and future examples.
JMS defines a ten level priority with 0 as the lowest priority and 9 as the highest. 0-4 is a normal priority, whereas 5-9 is expedited priority. Messages with expedited priority are delivered ahead of messages with normal priority.
In JMS messages, JMSPriority is the header field that contains the message priority. Usually it is set by the provider to 4.
The client can override that by calling “setPriority” method on the JMSProducer instance. Once set, any message sent by that producer from that point onwards will have the new priority.
Below is the producer class for your reference
Producer
1 package package8;
2
3 import javax.jms.ConnectionFactory;
4 import javax.jms.Destination;
5 import javax.jms.JMSContext;
6 import javax.jms.JMSProducer;
7
8 public class Producer implements Runnable {
9 private Destination destination;
10 private ConnectionFactory connectionFactory;
11
12 public Producer(Destination destination, ConnectionFactory connectionFactory) {
13 this.destination = destination;
14 this.connectionFactory = connectionFactory;
15 }
16
17 @Override
18 public void run() {
19 JMSContext jmsContext = null;
20
21 try {
22 jmsContext = connectionFactory.createContext();
23 JMSProducer jmsProducer = jmsContext.createProducer();
24 jmsProducer.send(destination, "Hello World 1");
25 Thread.sleep(10000);
26 jmsProducer.setPriority(5);
27 jmsProducer.send(destination, "Hello World 2");
28 } catch(Exception excep) {
29 excep.printStackTrace();
30 } finally {
31 if(jmsContext != null) {
32 jmsContext.close();
33 }
34 }
35 }
36 }
In the above code, at line 26 we set the priority to 5, so any messages sent after line 26 will have the priority 5. Any messages sent before line 26 will have priority 4.
Note: In previous versions we were able to set priority for individual messages, which is not possible in the newer version.
Below is the Consumer class for your reference
Consumer
1 package package8;
2
3 import javax.jms.ConnectionFactory;
4 import javax.jms.Destination;
5 import javax.jms.JMSConsumer;
6 import javax.jms.JMSContext;
7 import javax.jms.TextMessage;
8
9 public class Consumer implements Runnable {
10 private Destination destination;
11 private ConnectionFactory connectionFactory;
12
13 public Consumer(Destination destination, ConnectionFactory connectionFactory) {
14 this.destination = destination;
15 this.connectionFactory = connectionFactory;
16 }
17
18 @Override
19 public void run() {
20 JMSContext jmsContext = null;
21 try {
22 jmsContext = connectionFactory.createContext();
23 JMSConsumer jmsConsumer = jmsContext.createConsumer(destination);
24 TextMessage message = (TextMessage)jmsConsumer.receive();
25 System.out.println(message.getText() + ":" + message.getJMSPriority());
26 message = (TextMessage)jmsConsumer.receive();
27 System.out.println(message.getText() + ":" + message.getJMSPriority());
28 jmsConsumer.close();
29 } catch(Exception excep) {
30 excep.printStackTrace();
31 } finally {
32 if(jmsContext != null) {
33 jmsContext.close();
34 }
35 }
36 }
37 }
The output will be
Output
Hello World 1:4
Hello World 2:5
Below is the complete main code
Main class
package package8;
import java.util.Properties;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class Example8 {
public static void main(String[] args) throws NamingException, JMSException {
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
env.put(Context.PROVIDER_URL, "file:///C:/openmq5_1_1/temp");
InitialContext initialContext = new InitialContext(env);
ConnectionFactory connectionFactory = (ConnectionFactory)initialContext.lookup("MyConnectionFactory");
Destination destination = (Destination)initialContext.lookup("MyQueue");
Producer producer = new Producer(destination, connectionFactory);
Consumer consumer = new Consumer(destination, connectionFactory);
Thread producerThread = new Thread(producer);
Thread consumerThread = new Thread(consumer);
producerThread.start();
consumerThread.start();
initialContext.close();
}
}