Archives for 

JMS

This section is for Java Message Service(JMS). Explanation with code samples were given

JMS Tutorial-Sending object as message in JMS

In the previous chapters we have seen the fundamental concepts of JMS API.In the previous chapter we have discussed an example to demonstrate the working of JMS API.There we used OpenJMS as service provider. There we sent a text as message between two clients. In practical situations , it is often needed to send objects between applications rather than sending simple text messages.

Sending object as message in JMS

JMS API supports the sending of any Serializable object as ObjectMessage object . In this chapter we are explaining how an object is sent as a message between two client applications.Our FirstClient.java creates an EventMessage object and sends it to the destination.SecondClient.java is receiving the same EventMessage object from the same destination.Configuration of OpenJMS and workspace are prerequisites .It is explained in an  old article .

Now let us see the EventMessage.java .Objects of this class are sending as message between clients.  For sending as a message between clients ,the object should be serializable.

EventMessage.java

import java.io.Serializable;

public class EventMessage implements Serializable {

private static final long serialVersionUID = 1L;

private int messageId;

private String messageText = "";

public EventMessage(int id, String messageText) {

this.messageId = id;

this.messageText = messageText;

}

public int getMessageId() {

return messageId;

}

public void setMessageId(int messageId) {

this.messageId = messageId;

}

public String getMessageText() {

return messageText;

}

public void setMessageText(String messageText) {

this.messageText = messageText;

}

public String toString(){

return "Message Id = "+getMessageId()+" ; Message Text = "+getMessageText();

}

}

Now lets see the FirstClient.java.This class sends an object message to destination. The process is explained as step by step.

1)Initialize JNDI context

2)Do look up for administered objects (ConnectionFactory and Destination objects).

3)Crate Connection object

4)Create Session object

5)Create MessageProducer object and ObjectMessage object

6)Start the connection and send the object to destination

7)Close connection and context objects.

FirstClient.java

import java.util.Properties;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnectionFactory;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

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

public FirstClient() {

}

public void sendObjectMessage() {

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 = (QueueConnectionFactory) context
.lookup("ConnectionFactory");
destination = (Queue) context.lookup("queue1");
connection = factory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(destination);
EventMessage eventMessage = new EventMessage(1,
"Message from FirstClient");
ObjectMessage objectMessage = session.createObjectMessage();
objectMessage.setObject(eventMessage);
connection.start();
producer.send(objectMessage);
System.out.println(this.getClass().getName()
+ "has sent a message : " + eventMessage);

} catch (NamingException e) {

e.printStackTrace();

} catch (JMSException e) {

e.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.sendObjectMessage();
}

}

Now let us see the SecondClient.java. It simply reads the object message from the destination. The operation can be summarized in the following steps.

1)Initialize JNDI context

2)Do look up for administered objects (ConnectionFactory and Destination objects).

3)Crate Connection object

4)Create Session object

5)Create MessageConsumer object

6)Start the connection .

7)Read the object message from destination and display it.

8)Close the  connection and context objects.

SecondClient.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.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnectionFactory;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

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

public SecondClient() {

}

public void receiveObjectMessage() {
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 = (QueueConnectionFactory) context
.lookup("ConnectionFactory");
destination = (Queue) context.lookup("queue1");
connection = factory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
consumer = session.createConsumer(destination);
connection.start();
Message message = consumer.receive();
if (message instanceof ObjectMessage) {
Object object = ((ObjectMessage) message).getObject();
System.out.println(this.getClass().getName()
+ "has received a message : " + (EventMessage) object);
}

} catch (NamingException e) {

e.printStackTrace();
} catch (JMSException e) {

e.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.receiveObjectMessage();
}

}

Now let us examine the output. To get the output , JMS server should be started. And the necessary libraries should be in class path.Please read the configuration page for details.

Output

Run the FirstClient.java first.Once the running stops run the SecondClient.java

Output of FirstClient.java

FirstClient has sent a message : Message Id = 1 ; Message Text = Message from FirstClient

Output of SecondClient.java

SecondClient has received a message : Message Id = 1 ; Message Text = Message from FirstClient

So the object message sent from one client has received by the other client.

See  also:

JMS overview

configuring OpenJMS

JMS example

 Messaging Domains

Point to Point Messaging domain in JMS

Publish/Subscribe Messaging domain JMS

Message consumption

Synchronous message consumption in JMS

Asynchronous message consumption in JMS