Sunday, 5 August 2018

JMS: Message properties

By using properties of a message, you can add extra information to the message.

What values I can set to message properties?
A message property can be one of boolean, byte, short, int, long, float, double, and String.

Can I set a property value after sending the message?
Property values must be set before sending the message. When client receives the message, the property values are in read-only mode.

How can I set properties to a message?
‘javax.jms.Message’ class provides below methods to add properties to a message.

void setBooleanProperty(String name, boolean value) throws JMSException;
void setByteProperty(String name, byte value) throws JMSException;
void setShortProperty(String name, short value) throws JMSException;
void setIntProperty(String name, int value) throws JMSException;
void setLongProperty(String name, long value) throws JMSException;
void setFloatProperty(String name, float value) throws JMSException;
void setDoubleProperty(String name, double value) throws JMSException;
void setStringProperty(String name, String value) throws JMSException;
void setObjectProperty(String name, Object value) throws JMSException;

How can I get the properties of a message?
‘javax.jms.Message’ class provides below methods to get the properties of a message.

boolean getBooleanProperty(String name) throws JMSException;
byte getByteProperty(String name) throws JMSException;
short getShortProperty(String name) throws JMSException;
int getIntProperty(String name) throws JMSException;
long getLongProperty(String name) throws JMSException;
float getFloatProperty(String name) throws JMSException;
double getDoubleProperty(String name) throws JMSException;
String getStringProperty(String name) throws JMSException;
Object getObjectProperty(String name) throws JMSException;

How to check a property exists or not?
‘propertyExists’ method returns true, if the property exists, else false.

boolean propertyExists(String name) throws JMSException;

How to delete all the properties?
A message’s properties are deleted by the clearProperties method. This leaves the message with an empty set of properties.

void clearProperties() throws JMSException;

How to get all the property names?
Use getPropertyNames to retrieve a property name enumeration and then use the various property get methods to retrieve their values.

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.setBooleanProperty("isTestMessage", true);
  message.setStringProperty("TestCaseID", "Unit_Testing");

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

 }
}

JMSConsumer.java

package com.sample.consumer;

import java.util.Enumeration;

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();

  if (message instanceof TextMessage) {
   TextMessage textMessage = (TextMessage) message;
   System.out.println("\nReceived message '" + textMessage.getText() + "'");
  }

  Enumeration propertyNames = message.getPropertyNames();

  while (propertyNames.hasMoreElements()) {
   String propName = (String) propertyNames.nextElement();
   System.out.println(propName + " : " + message.getObjectProperty(propName));
  }

  connection.close();
 }

}


Run JMSProducer.java followed by JMSConsumer.java.

Output
Received message 'Hello, this is my first message.'
TestCaseID : Unit_Testing
isTestMessage : true


Previous                                                 Next                                                 Home

No comments:

Post a Comment