In previous part you saw that how easy is to make an MDB to consume messages and a jsp/Servlet Front End to send message to a queue.
in this part i will show you how you can send message to a queue from a remote j2se client. you should know that in jms sending and reciving mesages has similar steps , some small changes require to consume message from a j2se client instead of sending messages.
to make it more clear , the main purpose of an MDB is to consume messages as they arrive , The MDB onMessage(..) is called whenever a message become available in destination that MDB is binded to it.

sure we can do what ever we want after message recieved. for example you can send the message that you recive from a queue to several topics , you can process it to do some database operation.... usually we use JMS for executing Asynchronous operations ,bringing more decoupling of a system components....

but lets come to our own steps to create a simple remote client that will sends some messages to tQueue that we made in first part of this series. then we will see that our messages are reciveing by TMDB. As you know we used a context object to locate the Queue and ConnectionFactory . the servlet code was like:

  Context ctx = new InitialContext();

By default a JNDI client assume that it is in a correctly configured environment , so when we do not pass a HatshTableto IinitialContext , the InitialContextFactory will return a context configured with that environment .In server environment we do not need to explicity pass parameters to InitialContext unless we need to initiate a context for another environment.
But, what are this parameters and how we can use them to initiate a context for none default environemtn or in places that there is no default environemt pre-configured , situation like standalone remote clients?
in a such situation we should configure the InitialContextusing a HashTable that contain some parameters, There are several parameter that can be used to configure the initialContext but Two most important ones are :

  • provider url , for glassfish in iiop format it is :iiop://
  • We can use a key likeContext.PROVIDER_URL to put its value into hashtable , also we may use java.naming.provider.url String to put value of this parameter into HashTable . this parameter is vendor dependent.
  • initial context factory , for glassfish it is :com.sun.appserv.naming.S1ASCtxFactory
  • This is another important parameter that we must set before we can access a JNDI Tree , indeed it is totaly vendor dependent because each vendor has its own implementation for its JNDI access. This factory will create the context object along with the url that you put into the hashtable. we may use Context.INITIAL_CONTEXT_FACTORY or "java.naming.factory.initial" string as a key to put this value into the hashtable

As i said there are some other parameters that you can set , like security parameters for authentication and .... but those are not necessary.
I should tell that we can also make this parameter to be the jvm default parameter and allows the Initialcontext to return a context without need to pass any arguments. in this way we need to pass parameters to java command when we want to start it. for example you can use :
java -Djava.naming.provider.url="iiop://" -Djava.naming.factory.initial="com.sun.appserv.naming.S1ASCtxFactory" to start our application. this way you allows the Initialcontext to return a context without need to configure it by a HashTable. For our Sample we will use a hashtable to configure the InitialContext , but you can try to pass parameters to java instead and observ the execution of your application.
To create a j2se remote client we need to add some jar files to our project , NetBeans provide its own way to manage jar files that may be used in more than one project. it is Libraries....
Run NetBeans, From Tools menu select Library Manager, create a new library and name it jms . Now you can add as much jar files to this library as you need , then it will be reuseable for your other projects.
add following jar files to this library , I use glassfish_home as installation directory of glassfish.

  • glassfish_home/lib/appserv-rt.jar
  • glassfish_home/lib/javaee.jar
  • glassfish_home/lib/install/applications/jmsra/jmsra.jar
  • glassfish_home/lib/install/applications/jmsra/imqbroker.jar
  • glassfish_home/imq/lib/imq.jar
  • glassfish_home/lib/appserv-admin.jar
  • glassfish_home/imq/lib/jms.jar

Create a j2se project using , File > new project > general > java application. name the application jmsClient and allow the IDE to create a Main class for you.
You shoud add that library that you create to this project. to do this , expand the project node , right click on the libraries and select add library Now from the library list select jms .
Up to now you have done 30% of creating an stand alone remote client to interact with your jms resources like connectionFactories and destinations.
Now we need to code the main method of main class . expand the jmsClient node , expand the source packages and finally open the main class of your project.
The overall look of your code shoul be like the following :

public class JmsClient {
    Context ctx;
    public JmsClient() {
        Hashtable properties = new Hashtable(2);
        try {
            ctx = new InitialContext(properties);
        } catch (NamingException ex) {
   public Object lookup(String name){
        try {
            return ctx.lookup(name);
        } catch (NamingException ex) {
        return null;

    public static void main(String[] args) {
        JmsClient client = new JmsClient();
            ConnectionFactory     connectionFactory = (ConnectionFactory)client.lookup("jms/tConnectionFactory");
            Queue     queue = (Queue)client.lookup("jms/tQueue");
            javax.jms.Connection  connection = connectionFactory.createConnection();
            javax.jms.Session        session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
            MessageProducer messageProducer = session.createProducer(queue);
            for(int i=0;i<5;i++) {
                TextMessage message = session.createTextMessage("It is a message from main class "+  ": "+ i);
                System.out.println( "It come from main class:"+ message.getText());
        } catch(Exception ex){

Now lets see what the above code means , i will not go indepth because JMS APIs are discussed too much :-) .
In constructor we configured a context object named ctx
we encapsulate the lookpu task in a method named lookup(...)we usually use a service locator or cached service locator to locate our objects in JNDI because JNDI lokups are time consumer
In main method:

  • we create a ConnectionFactory by looking it up in the jndi by using our context and dummy lookup(...) method.
  • we create a Queue by looking it up in the jndi by using our context and dummy lookup(...) method.
  • Then we create a connection using that ConnectionFactory
  • we obtain a Session (jms Session) using our connection
  • we made the messageproducer which is our tools to send message to that Queue ,
  • in a loop we create and sent 5 text message to the queue.

Now lets run the program and see the result , to run the application you nedd to complete the first part of this series , then you should run the application server , and if you like to have a demonestration like what i will show you , you should deploy the application that we made in first part.
i assume you have completed first part and you have the application server running , Now run the client that we made and what you will see in output window should be like :

and if you take a look at application server log file (in runtime tab , expand the servers node and rigt click on the glassfish instance , now select view server log..) what you should see in the server log should be something like :

messages that we send via standalone client will reach the queue that the MDB is listening on it , as soon as we send a message MDB will pick it up and start processing it.
you can download the standalone client project from here