Tutorial: Starting with Glassfish and JMS (Part 2)

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”


JMSClient: New Java SE project

2. Import the necessary libraries

Applicationserver JNDI Lookup

Client Lib

(You find them in your glassfish home directory)


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");



        } 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 {


  • 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.

13 thoughts on “Tutorial: Starting with Glassfish and JMS (Part 2)

  1. 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


  2. I hit the same problem. Thats why I added

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

  3. 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?

  4. Great example – thanks! One thing I’m facing is that I want to promptly notify the user if the app is unable to connect to the glassfish server. To test this, I stopped the queue server and ran the app. The line with “jndiContext.lookup” takes a long time before it throws a NamingException. Is there some way to set a shorter timeout? I’ve run across this initial context property “com.sun.jndi.ldap.connect.timeout”, but it didn’t seem to help me. Thanks again!

  5. Use
    instead of

    so you only need to include

    That’s all. 🙂

    • I didnt find any monitoring tool. Something the ActiveMQ admin console would be useful. Even better if integrated in Glassfish !

  6. To follow up on my previous posts… I never found a way to detect a timeout in a reasonable timeframe. I have since switched to ActiveMQ, which responds much quicker when the connection is down.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s