JMS Message Consumption-Synchronous Message Consumption in JMS

We already discussed the fundamental concepts of Java Messaging Service API .Messages can be consumed either synchronously or asynchronously.In asynchronous consumption there is no timing dependency between production and consumption of messages.But in synchronous consumption , there is timing dependency . In this chapter we are discussing the synchronous message consumption with an example.

Synchronous Message Consumption in JMS

JMS API supports both synchronous as well as asynchronous consumption of messages. In this chapter we are discussing the concept of synchronous message consumption with a simple example. In this case , we have two JMS clients.Second client establishes connection with the destination object and waiting to get a message.The second client stays on the receive()  method till it receives a message or till the wait timeout occurs.The first client sends a message to destination. We are using openJMS as service provider in this example.Before running the example , we need to configure the OpenJMS and workspace.It is already discussed in a previous chapter.Also we should import the necessary jar files as mentioned there.

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.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

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

public FirstClient() {

}

public void sendMessage() {
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 = (ConnectionFactory) context.lookup("ConnectionFactory");
destination = (Destination) context.lookup("queue1");
connection = factory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(destination);
connection.start();
TextMessage message = session.createTextMessage();
message.setText("Hello ...This is a sample message");
producer.send(message);
System.out.println("Sent: " + message.getText());

} catch (JMSException ex) {
ex.printStackTrace();
} catch (NamingException ex) {
ex.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.sendMessage();
}

}

Now let us see the second client.

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.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

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

public SecondClient() {

}

public void receiveMessage() {

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 = (ConnectionFactory) context.lookup("ConnectionFactory");
destination = (Destination) 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 TextMessage) {
TextMessage text = (TextMessage) message;
System.out.println("Message is : " + text.getText());
}

} catch (JMSException ex) {
ex.printStackTrace();
} catch (NamingException ex) {
ex.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.receiveMessage();
}

}

Output

Make sure that all configuration is done and all dependencies were included as mentioned earlier. Compile and run the SecondClient.java. And then FirstClient.java.

Output of FirstClient.java

Sent: Hello …This is a sample message

Output of SecondClient.java

Message is : Hello …This is a sample message

Conclusion

If we start the second client first , it is found that the operation stays there on receive()  method.When the message reaches in the destination it was received by the second client . This is an example for synchronous message consumption.

See also :

JMS overview

Configuring openJMS

JMS example

Sending object as message in JMS

Point-to-Point messaging domain in JMS

Publish/Subscribe messaging domain in JMS

Asynchronous message consumption in JMS