JMS Domains-Point-to-point JMS messaging domain

In  a previous chapter we discussed the fundamental concepts of Java Messaging Service API.There we discussed the various messaging domains in brief. In this chapter we are looking into the Point to Point Messaging domain in JMS with suitable example.

Point-to-point JMS Messaging Domain

In  Point-to-Point messaging domain, there are three  components :

a)  queue 

b) sender

c) receiver

The sender addresses a message to the queue. The receiver takes the message from the queue. The working is shown below as a schematic. The queue keeps the message till a receiving client receives it or till the message expires.This domain is useful when the message should be processed by  only one client.

p2p

Point-to-point JMS messaging domain example

In this example code , we have two clients . FirstClient.java and SecondClient.java. FirstClient.java addresses a message to a queue and SecondClient.java receives the message from the queue. Here we are sending ObjectMessage object as message between clients.

Objects of class EventMessage.java is sending as message between clients.

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 let us see the FirstClient.java

FirstClient.java

import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class FirstClient {

private Context context = null;
private QueueConnectionFactory factory = null;
private QueueConnection connection = null;
private Queue queue = null;
private QueueSession session = null;
private QueueSender sender = 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");
queue = (Queue) context.lookup("queue1");
connection = factory.createQueueConnection();
session = connection.createQueueSession(false,
QueueSession.AUTO_ACKNOWLEDGE);
sender = session.createSender(queue);
EventMessage eventMessage = new EventMessage(1,
"Message from FirstClient");
ObjectMessage objectMessage = session.createObjectMessage();
objectMessage.setObject(eventMessage);
connection.start();
sender.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();
}
}

The operation can be summarized as follows.

1)Performs JNDI lookup to get QueueConnectionFactory and Queue objects.

2)Create QueueConnection object.

3)Create QueueSession object.

4)Create QueueSender object.

5)Create ObjectMessage instance which contains an EventMessage object.

6)Start the connection and send the message to the queue.

7)Close the Context and Connection objects.

Now lets see the SecondClient.java.This client simply receives the Message sent by the first client.

SecondClient.java 

import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException; 

public class SecondClient {
private Context context = null;
private QueueConnectionFactory factory = null;
private QueueConnection connection = null;
private Queue queue = null;
private QueueSession session = null;
private QueueReceiver receiver = 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");
queue = (Queue) context.lookup("queue1");
connection = factory.createQueueConnection();
session = connection.createQueueSession(false,
QueueSession.AUTO_ACKNOWLEDGE);
receiver = session.createReceiver(queue);
connection.start();
Message message = receiver.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();
}

}

The operation of this client can be summarized as below.

1)Performs JNDI lookup to get QueueConnectionFactory and Queue objects.

2)Create QueueConnection object.

3)Create QueueSession object.

4)Start the connection and receive the object message sent by the  first client.

5)Close the Context and Connection objects.

Output

We are using the OpenJMS as service provider here.Before running these clients , the openJMS should be configured and workspace should be ready as discussed in earlier  chapter.After starting the OpenJMS, we can run our clients.

First run the FirstClient.java.

Output of FirstClient.java

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

Now run the SecondClient.java

Output of SecondClient.java

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

See Related Topics:

JMS overview

Configuring OpenJMS

JMS example

Sending Object messages in JMS

Publish/Subscribe Messaging domain in JMS

Synchronous message consumption in JMS

Asynchronous message consumption in JMS

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">