Sunday 5 August 2018

JMS: JMSPriority: Set priority to the jms message

‘JMSPriority’ header is used to set the priority of the jms message. JMS defines 10 priority values from 0 to 9.

0 – Lowest priority
9 – Highest priority

Example
TextMessage message = session.createTextMessage("Hello, this is my first message.");
message.setJMSPriority(8);

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.");
  message.setJMSPriority(8);

  producer.send(message);
  connection.close();

 }
}



Previous                                                 Next                                                 Home

No comments:

Post a Comment