Sun Java System Message Queue 4.2 Developer's Guide for Java Clients

Compiling and Running a Client Application

This section leads you through the steps needed to compile and run a simple example client application, HelloWorldMessage, that sends a message to a destination and then retrieves the same message from the destination. The code shown in Example 1–2 is adapted and simplified from an example program provided with the Message Queue installation: error checking and status reporting have been removed for the sake of conceptual clarity. You can find the complete original program in the helloworld directory in the following locations.


Example 1–2 Simple Message Queue Client Application


//  Import the JMS and JNDI API classes

    import javax.jms.*;
    import javax.naming.*;
    import java.util.Hashtable;


public class HelloWorldMessage
  {
    
    /**
      * Main method
      *
      *   Parameter args not used
      *
    */
    
    public static void main (String[]  args)
      {
        try
          { 
            //  Get a connection factory.
            //  
            //  Create the environment for constructing the initial JNDI 
						//   naming context.
                
                Hashtable  env = new Hashtable();
                
                
            //  Store the environment attributes that tell JNDI which
            //  initial context
            //  factory to use and where to find the provider.
            //  (On Unix, use provider URL "file:///imq_admin_objects" 
            // instead of"file:///C:/imq_admin_objects".)            
                
                env.put(Context.INITIAL_CONTEXT_FACTORY,
                        "com.sun.jndi.fscontext.RefFSContextFactory");
                env.put(Context.PROVIDER_URL,"file:///C:/imq_admin_objects");
                
                
            //  Create the initial context.
                
                Context  ctx = new InitialContext(env);
                
                
            //  Look up connection factory object in the JNDI object store.
                
                String  CF_LOOKUP_NAME = "MyConnectionFactory";
                ConnectionFactory  myFactory =
                          (ConnectionFactory) ctx.lookup(CF_LOOKUP_NAME);
            
            
            //  Create a connection.
                
                Connection  myConnection = myFactory.createConnection();
            
            
            //  Create a session.
                
                Session  mySession = myConnection.createSession(false,
                                        Session.AUTO_ACKNOWLEDGE);
            
            
            //  Look up the destination object in the JNDI object store.
                
                String  DEST_LOOKUP_NAME = "MyDest";
                Destination  myDest = (Destination) ctx.lookup
																		(DEST_LOOKUP_NAME);
            
            
            //  Create a message producer.
                
                MessageProducer  myProducer = mySession.createProducer(myDest);
            
            
            //  Create a message consumer.
                
                MessageConsumer  myConsumer = mySession.createConsumer(myDest);
            
            
            //  Create a message.
                
                TextMessage  outMsg = mySession.createTextMessage
																							("Hello, World!");
            
            
            //  Send the message to the destination.
                
                System.out.println("Sending message: " + outMsg.getText());
                myProducer.send(outMsg);
            
            
            //  Start the connection.
                
                myConnection.start();
            
            
            //  Receive a message from the destination.
                
                Message  inMsg = myConsumer.receive();
            
            
            //  Retrieve the contents of the message.
                
                if (inMsg instanceof TextMessage)
                  { TextMessage  txtMsg = (TextMessage) inMsg;
                    System.out.println("Received message: " + 
                                            txtMsg.getText());
                  }
            
            
            //  Close the session and the connection.
                
                mySession.close();
                myConnection.close();
            
          }
        
        catch (Exception jmse)
          { System.out.println("Exception occurred: " + jmse.toString() );
            jmse.printStackTrace();
          }
        
      }
    
  }


         

To compile and run Java clients in a Message Queue environment, it is recommended that you use the Java 2 SDK, Standard Edition, version 1.4 or later. You can download the recommended SDK from the following location:

http://java.sun.com/j2se/1.5

Be sure to set your CLASSPATH environment variable correctly, as described in Setting Up Your Environment, before attempting to compile or run a client application.


Note –

If you are using JDK 1.5, you will get compiler errors if you use the unqualified JMS Queue class along with the following import statement.

import java.util.*

This is because the packagesjava.util and javax.jms both contain a class named Queue. To avoid the compilation errors, you must eliminate the ambiguity by either fully qualifying references to the JMS Queue class as javax.jms.Queue or correcting your import statements to refer to specific individual java.util classes.


The following steps for compiling and running the HelloWorldMessage application are furnished strictly as an example. The program is shipped precompiled; you do not actually need to compile it yourself (unless, of course, you modify its source code).

ProcedureTo Compile and Run the HelloWorldMessage Application

  1. Make the directory containing the application your current directory.

    The Message Queue example applications directory on Solaris is not writable by users, so copy the HelloWorldMessage application to a writable directory and make that directory your current directory.

  2. Compile the HelloWorldMessage application:


    javac HelloWorldMessage.java

    This creates the file HelloWorldMessage.class in your current directory.

  3. Run the HelloWorldMessage application:


    java HelloWorldMessage

    The program should display the following output:


        Sending Message: Hello, World!
        Received Message: Hello, World!