JMS Tutorial-JMS example

We discussed the fundamental concepts of Java Message Service API .In the previous chapter we discussed the way by which configuration of OpenJMS can be done.In this chapter we are discussing an example to demonstrate the working of JMS. Here we are using OpenJMS as the service provider.(If OpenJMS is not configured , please read the previous article).

JMS example

In this case we have two clients.FirstClient.java and SecondClient.java.The FirstClient.java sends a text message to SecondClient.java . This uses Point to Point Message domain.

FirstClient.java

It does the following things.

1)Look up for JNDI initial context.(In this case , the initial properties are hard coding.We can do it by specifying jndi.properties as discussed earlier )

2)Looking for connection factory ‘ConnectionFactory’. This is pre-configured one in openjms.xml file in OPENJMS_HOME/config/.

3)Looking for destination -‘queue1‘ . This is also pre-configured one in openjms.xml file in OPENJMS_HOME/config/.

4)Create Connection object.

5)Create Session object

6)Create MessageProducer object.

7)Start Connection.

8)Create and send text message.

9)Close the context and connection objects.

Now let us see the code.

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import javax.jms.JMSException;
import javax.jms.Destination;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

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..sending from FirstClient");
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();
}

}

SecondClient.java

It does the following things.

1)Look up for JNDI initial context.(In this case , the initial properties are hard coding.We can do it by specifying jndi.properties as discussed earlier )

2)Looking for connection factory ‘ConnectionFactory’. This is pre-configured one in openjms.xml file in OPENJMS_HOME/config/.

3)Looking for destination -‘queue1‘ . This is also pre-configured one in openjms.xml file in OPENJMS_HOME/config/.

4)Create Connection object.

5)Create Session object

6)Create MessageConsumer object.

7)Start Connection.

8)Receive and display message.

9)Close the context and connection objects.

Now lets see the code.

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Destination;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Session;
import javax.jms.MessageConsumer;
import javax.jms.TextMessage;

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

Compile and run the FirstClient.java .Wait for FirsClient.java to exit .Then compile and run SecondClient.java.

Outputof FirstClient.java

Sent: Hello …This is a sample message..sending from FirstClient

Output of SecondClient.java

Message is : Hello …This is a sample message..sending from FirstClient

So the message sent by FirstClient. is received in SecondClient.

See also:

JMS Fundamentals

Configuring OpenJMS

Sending Object Message in JMS

 Messaging Domains in JMS

Point to Point Messaging domain in JMS

Publish/Subscribe Messaging domain JMS

Message consumption

Synchronous message consumption in JMS

Asynchronous message consumption in JMS