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

4 thoughts on “JMS Tutorial-JMS example

  1. Very very good. I literary read your text followed instruction and run program in first attempt. Awesome.

  2. Tks bro!
    But when I followed. I get error:
    javax.naming.ConfigurationException: The scheme is not conformant.
    at org.exolab.jms.jndi.InitialContextFactory.getProviderURI(InitialContextFactory.java:213)
    at org.exolab.jms.jndi.InitialContextFactory.getInitialContext(InitialContextFactory.java:135)
    at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
    at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.init(Unknown Source)
    at javax.naming.InitialContext.(Unknown Source)
    at FirstClient.sendMessage(FirstClient.java:37)
    at FirstClient.main(FirstClient.java:66)

    <javax.naming.ConfigurationException: The scheme is not conformant.
    at org.exolab.jms.jndi.InitialContextFactory.getProviderURI(InitialContextFactory.java:213)
    at org.exolab.jms.jndi.InitialContextFactory.getInitialContext(InitialContextFactory.java:135)
    at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
    at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.init(Unknown Source)
    at javax.naming.InitialContext.(Unknown Source)
    at FirstClient.sendMessage(FirstClient.java:37)
    at FirstClient.main(FirstClient.java:66)
    >

  3. Hi bro,
    I’m learn activemq with topic .
    i have 3 consumer A B C
    and 1 topic

    consumer A B is online , and C is ofline
    i user 1 producer send 1 msg ,
    then consumer A B received,

    after i start consumer C , but not receved this msg

    please help me.

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="">