Tutorial: Starting with Glassfish and JMS (Part 2)

9 01 2009

Today we proceed with last weeks JMS tutorial and create a standalone java application that sends a message to a topic.

It puzzles me to have a client application that I need to start “from within”. Lets find out how an application looks like that can run by itself, connect to a JNDI server and sends a message to topic. The key information I took from this discussion (link, Thanks to Foli and TravelEntity) at forums.sun.com.

Using: Netbeans 6.5 and a local Glassfish V2

1. Lets create a standard SE java project. We call it “JMSClient”

tut09010005_new_java_application

JMSClient: New Java SE project

2. Import the necessary libraries

Applicationserver JNDI Lookup
/lib/appserv-rt.jar
/lib/appserv-admin.jar
/lib/javaee.jar
/lib/j2ee.jar

Client Lib
/imq/lib/jms.jar
/imq/lib/imq.jar
/imq/lib/imqutil.jar
/lib/install/applications/jmsra/jmsra.jar

(You find them in your glassfish home directory)

Libraries

JMSClient: Libraries

3. The complete source


package jmsclient;

import java.util.Hashtable;
import java.util.Queue;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.Topic;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Main {

    public static void main(String[] args) {

        Context jndiContext = null;
        ConnectionFactory connectionFactory = null;
        Connection connection = null;
        Session session = null;
        // ---- Same sample with a queue ----
        // Queue queue = null;
        Topic topic = null;
        MessageProducer messageProducer = null;
        MapMessage message = null;

        Hashtable properties = new Hashtable(2);
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.appserv.naming.S1ASCtxFactory");
        properties.put(Context.PROVIDER_URL, "iiop://localhost:3700");

        try {
            jndiContext = new InitialContext(properties);
            connectionFactory = (ConnectionFactory)jndiContext.lookup("jms/ConnectionFactory");
            // ---- Same sample with a queue ----
            //queue = (Queue)jndiContext.lookup("jms/Queue");
            topic = (Topic)jndiContext.lookup("jms/Topic");

            connection = connectionFactory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            // ---- Same sample with a queue ----
            //messageProducer = session.createProducer(queue);
            messageProducer = session.createProducer(topic);
            message = session.createMapMessage();

            // ---- Preparing Mapped Message ----
            message.setString("lastname", "Myer");
            message.setString("firstname", "Fred");
            message.setString("id", "0200");

            messageProducer.send(message);

            connection.close();

        } catch (NamingException e) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, e);
        }
        catch (JMSException e) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, e);
        }
        finally {
            System.out.println("ENDED.");
            System.exit(0);
        }
    }
}

Remarks:

  • The tutorial is looking for a local server. Change (Context.PROVIDER_URL, “iiop://localhost:3700″) to the relevant server and port info.
  • We use a topic (publish/subscribe) in this sample. If you want to use a queue instead, change to the lines which are commented out and remove the topic related lines.

Actions

Information

4 responses

14 03 2009
Cristiano

Hi, did your client exit when you close the JMS Connection?

I was trying to write a simple JMS Client to connect a JMS Queue in Glassfish but is not able to quit. It is stuck.

Thanks in advantage

Cristiano

14 03 2009
devdude

I hit the same problem. Thats why I added
System.exit(0);

Not sure if this is the proper approach nor know the reason why the application remains running.

17 09 2009
Claude

Wow, thanks a lot. How did you find out which jar files to add? I searched the web for two hours to find the answer and finally got it in your page :)

3 12 2009
JB

Question – What if I want to restrict access to my JMS queue or topic to only clients in my Glassfish server – no external clients. How do I do this?

Leave a comment