Open JMS Example With Remote Client

Aim
The aim of this tutorial is to configure a JMS queue using Open JMS and connect to it from a remote Client via JNDI. We will use destinations/queues and topics in the one example. Leave any questions or problems as comments and I will endeavour to answer them. To learn how to do this using a glassfish JNDI topic have a look at JMS Example using Glassfish and a Remote Client .

Assumptions
This article assumes that you have Open JMS installed/unzipped and ready to go.

Versions used in this example
Sofware/ComponentImage
Windows XP SP2N/A
Open JMSopenjms-0.7.7-beta-1.zip
JDK 1.5.0N/A
Links to these files can be found here

For the purposes of this tutorial Open JMS has been exanded in the D:\downloads\openjms-0.7.7-beta-1 directory. Make sure to replace this with your local directory. The project directory is OpenJMSExample and we will only have two files. One to publish/produce and one to listen/consume.

OpenJMSExample
_|_OpenJMSPublisher.java
_|_OpenJMSListener.java
_|_jndi.properties

Startup the OpenJMS Server
  1. Open a prompt to the Open JMS bin directory and run admin.bat. You have to set the JAVA_HOME to point to your jdk beforehand.
  2. `
  3. Once the little admin GUI window pops up, select Actions -> Start Open JMS. Then select Actions -> Online. Now the server is ready and you can see the destinations and topics. We will be using the default topic1 and queue1 that is already configured. You can change this in the openjms.xml file in the config directory.

Create the properties file
  1. Create the jndi.properties file in your working directory.
     1. java.naming.provider.url=tcp://localhost:3035
     2. java.naming.factory.initial=org.exolab.jms.jndi.InitialContextFactory
     3. java.naming.security.principal=admin
     4. java.naming.security.credentials=openjms
    Hide line numbers
    You can choose not to use a file and put the properties such as Context.PROVIDER_URL directly. Please have a look at JMS Example using Glassfish and a Remote Client on how to do this.

Write and compile the publisher and listener
  1. Write the listener/consumer and save it as OpenJMSListener.java in your working directory.
     1. import java.io.*;
     2. import java.util.*;
     3. 
     4. import javax.jms.*;
     5. import javax.naming.*;
     6. 
     7. public class OpenJMSListener implements MessageListener{
     8.     
     9.     public static void main(String args[]){
    10.         new OpenJMSListener().Go();
    11.     }
    12.     
    13.     public void Go(){
    14.         try{
    15.             Properties props = new Properties();
    16.             props.load(getClass().getClassLoader().getResourceAsStream("jndi.properties"));
    17. 
    18.             Context context = new InitialContext(props);
    19.             TopicConnectionFactory tfactory = (TopicConnectionFactory) context.lookup("ConnectionFactory");
    20.         
    21.             TopicConnection tconnection = tfactory.createTopicConnection();
    22.             TopicSession tsession = tconnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    23.       
    24.             //listen to topic  
    25.             TopicSubscriber subscriber = tsession.createSubscriber((Topic)context.lookup("topic1"));
    26.             
    27.             //listen to queue/destination
    28.             MessageConsumer subscriber2 = tsession.createConsumer((Destination)context.lookup("queue1"));
    29.         
    30.             subscriber.setMessageListener(this);
    31.             subscriber2.setMessageListener(this);
    32.             
    33.             tconnection.start();
    34.         
    35.             (new BufferedReader(new InputStreamReader(System.in))).readLine();
    36.         
    37.             context.close();
    38.             tconnection.close();
    39.         }catch(Exception e){
    40.             e.printStackTrace();
    41.         }
    42.         
    43.     }
    44.     public void onMessage(Message message){
    45.         if(message instanceof TextMessage){
    46.             try{
    47.                 System.out.println( ((TextMessage)message).getText());
    48.             }catch(Exception e){
    49.             }
    50.         }
    51.     }
    52. }
    Hide line numbers
  2. `
  3. Compile the code and install it using

    ..workspace\openJMSExample>javac -extdirs D:\downloads\openjms-0.7.7-beta-1\lib OpenJMSListener.java
    Substitue your local openjms\lib directory for the -extdirs.
  4. `
  5. Create the publisher/producer and save it as OpenJMSPublisher.java in your wroking directory.
     1. import java.io.*;
     2. import java.util.*;
     3. 
     4. import javax.jms.*;
     5. import javax.naming.*;
     6. 
     7. public class OpenJMSPublisher{
     8.     
     9.     public static void main(String args[]){
    10.         new OpenJMSPublisher().Go();
    11.     }
    12.     
    13.     public void Go(){
    14.     try{
    15. 
    16.       Properties props = new Properties();
    17.       props.load(getClass().getClassLoader().getResourceAsStream("jndi.properties"));
    18. 
    19.       Context context = new InitialContext(props);
    20.       TopicConnectionFactory tfactory = (TopicConnectionFactory) context.lookup("ConnectionFactory");
    21.          
    22.       TopicConnection tconnection = tfactory.createTopicConnection();
    23.       TopicSession tsession = tconnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    24. 
    25.       TextMessage message = tsession.createTextMessage();
    26.       message.setText("Hello there from publisher");
    27.       
    28.       //publish the message to topic
    29.       TopicPublisher publisher = tsession.createPublisher((Topic)context.lookup("topic1"));
    30.       publisher.publish(message);
    31.       
    32.       //send message to queue/destinations
    33.       MessageProducer sender = tsession.createProducer((Destination) context.lookup("queue1"));
    34.       sender.send(message);
    35.         
    36.       context.close();
    37.       tconnection.close();
    38.         
    39.     }catch(Exception e){
    40.       e.printStackTrace();
    41.      }
    42.   }
    43. }
    Hide line numbers
  6. `
  7. Now cd out to the workingdirectory compile the client

    ..workspace\openJMSExample>javac -extdirs D:\downloads\openjms-0.7.7-beta-1\lib OpenJMSPublisher.java
    Substitue your local openjms\lib directory for the -extdirs.

Running the example
  1. Open a promt to your working directory and run the listener/consumer

    ..workspace\openJMSExample>java -Djava.ext.dirs=D:\downloads\openjms-0.7.7-beta-1\lib OpenJMSListener
  2. `
  3. Now ppen anotehr promt to your working directory and run the publisher/producer.

    ..workspace\openJMSExample>java -Djava.ext.dirs=D:\downloads\openjms-0.7.7-beta-1\lib OpenJMSPublisher
    You should see "Hello there from publisher" twice. One sent via the topic and one via the destination/queue. If you stop the listener and run the publisher only, you can see the queued message in the admin GUI.

Back to the tutorial trail | Home