JMS
provides, five forms of the message bodies.
a. StreamMessage
b. MapMessage
c. TextMesage
d. ObjectMessage
e. BytesMessage
StreamMessage
StreamMessage
is used to send a stream of primitive types in the Java programming language.
It read sequentially.
MapMessage
A
message whose body contains a set of name-value pairs where names are String
objects and values are Java primitive types. The entries can be accessed
sequentially by enumerator or randomly by name. The order of the entries is
undefined.
TextMessage
A
message whose body contains a java.lang.String.
ObjectMessage
A
message that contains a serializable Java object.
BytesMessage
It
is used to send a message containing a stream of uninterpreted bytes.
'javax.jms.Session'
interface provides below methods to create different forms of messges.
BytesMessage
createBytesMessage() throws JMSException;
MapMessage
createMapMessage() throws JMSException;
Message
createMessage() throws JMSException;
ObjectMessage
createObjectMessage() throws JMSException;
ObjectMessage
createObjectMessage(Serializable object) throws JMSException;
StreamMessage
createStreamMessage() throws JMSException;
TextMessage
createTextMessage() throws JMSException;
TextMessage
createTextMessage(String text) throws JMSException;
Below
application creates a TextMessage and send it to the consumer.
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(); if (message instanceof TextMessage) { TextMessage textMessage = (TextMessage) message; System.out.println("\nReceived message '" + textMessage.getText() + "'"); } connection.close(); } }
Run
JMSProducer followed by JMSConsumer, you can able to see below message in the
console.
Received
message 'Hello, this is my first message.'
No comments:
Post a Comment