Example code fragments

This section includes two example code fragments; Figure 1 and Figure 2. Each one uses a particular connection and includes notes to describe the changes needed to use alternative connections.

Example applet code

The following code fragment demonstrates an applet that uses a TCP/IP connection to:

  1. Connect to a queue manager
  2. Put a message onto SYSTEM.DEFAULT.LOCAL.QUEUE
  3. Get the message back

Figure 1. WebSphere MQ classes for Java example applet


// ===========================================================================
//
// Licensed Materials - Property of IBM
//
// 5639-C34
//
// (c) Copyright IBM Corp. 1995,1999
//
// ===========================================================================
// WebSphere MQ Client for Java sample applet
//
// This sample runs as an applet using the appletviewer and HTML file,
// using the command :-
//            appletviewer MQSample.html
// Output is to the command line, NOT the applet viewer window.
//
// Note. If you receive WebSphere MQ error 2 reason 2059 and you are sure your
// WebSphere MQ and TCP/IP setup is correct,
// you should click on the "Applet" selection in the Applet viewer window
// select properties, and change "Network access" to unrestricted.
import com.ibm.mq.*;            // Include the WebSphere MQ classes for Java package
 
public class MQSample extends java.applet.Applet
{
 
  private String hostname = "your_hostname";      // define the name of your
                                                  // host to connect to
  private String channel  = "server_channel";     // define name of channel
                                                  // for client to use
                                                  // Note. assumes WebSphere MQ Server
                                                  // is listening on the default
                                                  // TCP/IP port of 1414
  private String qManager = "your_Q_manager";     // define name of queue
                                                  // manager object to
                                                  // connect to.
 
  private MQQueueManager qMgr;                    // define a queue manager object
 
  // When the class is called, this initialization is done first.
 
  public void init()
  {
     // Set up WebSphere MQ environment
     MQEnvironment.hostname = hostname;           // Could have put the
                                                  // hostname & channel
     MQEnvironment.channel  = channel;            // string directly here!
 
     MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,//Set TCP/IP or server
                                  MQC.TRANSPORT_MQSERIES);//Connection
 
  } // end of init
 
  public void start()
  {
 
    try {
      // Create a connection to the queue manager
      qMgr = new MQQueueManager(qManager);
 
      // Set up the options on the queue we wish to open...
      // Note. All WebSphere MQ Options are prefixed with MQC in Java.
       int openOptions = MQC.MQOO_INPUT_AS_Q_DEF |
                        MQC.MQOO_OUTPUT ;
      // Now specify the queue that we wish to open, and the open options...
 
      MQQueue system_default_local_queue =
              qMgr.accessQueue("SYSTEM.DEFAULT.LOCAL.QUEUE",
                               openOptions);
                                
      // Define a simple WebSphere MQ message, and write some text in UTF format..
 
      MQMessage hello_world = new MQMessage();
      hello_world.writeUTF("Hello World!");
 
      // specify the message options...
 
      MQPutMessageOptions pmo = new MQPutMessageOptions();  // accept the defaults,
                                                            // same as
                                                            // MQPMO_DEFAULT
                                                            // constant
      // put the message on the queue
 
      system_default_local_queue.put(hello_world,pmo);
 
      // get the message back again...
      // First define a WebSphere MQ message buffer to receive the message into..
 
      MQMessage retrievedMessage = new MQMessage();
      retrievedMessage.messageId = hello_world.messageId;
 
      // Set the get message options..
 
      MQGetMessageOptions gmo = new MQGetMessageOptions();  // accept the defaults
                                                            // same as
                                                            // MQGMO_DEFAULT
      // get the message off the queue..
 
      system_default_local_queue.get(retrievedMessage, gmo);
 
      // And prove we have the message by displaying the UTF message text
 
      String msgText = retrievedMessage.readUTF();
      System.out.println("The message is: " + msgText);
 
      // Close the queue
 
      system_default_local_queue.close();
 
      // Disconnect from the queue manager
 
      qMgr.disconnect();
 
    }
 
    // If an error has occurred in the above, try to identify what went wrong.
    // Was it a WebSphere MQ error?
 
    catch (MQException ex)
    {
      System.out.println("A WebSphere MQ error occurred : Completion code " +
                         ex.completionCode +
                         " Reason code " + ex.reasonCode);
    }
    // Was it a Java buffer space error?
    catch (java.io.IOException ex)
    {
      System.out.println("An error occurred whilst writing to the
      message buffer: " + ex);
    }
 
  } // end of start
 
} // end of sample

Example application code

The following code fragment demonstrates a simple application that uses bindings mode to:

  1. Connect to a queue manager
  2. Put a message onto SYSTEM.DEFAULT.LOCAL.QUEUE
  3. Get the message back again

Figure 2. WebSphere MQ classes for Java example application


// ======================================================================
// Licensed Materials - Property of IBM
// 5639-C34
// (c) Copyright IBM Corp. 1995, 1999
// ======================================================================
// WebSphere MQ classes for Java sample application
//
// This sample runs as a Java application using the command :- java MQSample
 
import com.ibm.mq.*;            // Include the WebSphere MQ classes for Java package
 
  
public class MQSample
{
  private String qManager = "your_Q_manager";  // define name of queue
                                               // manager to connect to.
  private MQQueueManager qMgr;                 // define a queue manager
                                               // object
  public static void main(String args[]) {
     new MQSample();
  }
 
  public MQSample() {
   try {
   
      // Create a connection to the queue manager
 
      qMgr = new MQQueueManager(qManager);
 
      // Set up the options on the queue we wish to open...
      // Note. All WebSphere MQ Options are prefixed with MQC in Java.
 
      int openOptions = MQC.MQOO_INPUT_AS_Q_DEF |
                        MQC.MQOO_OUTPUT ;      
 
      // Now specify the queue that we wish to open,
      // and the open options...
 
      MQQueue system_default_local_queue =
              qMgr.accessQueue("SYSTEM.DEFAULT.LOCAL.QUEUE",
                               openOptions);
                                 
      // Define a simple WebSphere MQ message, and write some text in UTF format..
 
      MQMessage hello_world = new MQMessage();
      hello_world.writeUTF("Hello World!");  
 
      // specify the message options...
 
      MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the // defaults,
                                                           // same as MQPMO_DEFAULT
 
     // put the message on the queue
 
      system_default_local_queue.put(hello_world,pmo);
 
      // get the message back again...
      // First define a WebSphere MQ message buffer to receive the message into..
 
      MQMessage retrievedMessage = new MQMessage();
      retrievedMessage.messageId = hello_world.messageId;
 
      // Set the get message options...
 
      MQGetMessageOptions gmo = new MQGetMessageOptions(); // accept the defaults
                                                           // same as  MQGMO_DEFAULT
      // get the message off the queue...
 
      system_default_local_queue.get(retrievedMessage, gmo);
 
      // And prove we have the message by displaying the UTF message text
 
      String msgText = retrievedMessage.readUTF();
      System.out.println("The message is: " + msgText);
      // Close the queue...
      system_default_local_queue.close();
      // Disconnect from the queue manager
 
      qMgr.disconnect();
    }
      // If an error has occurred in the above, try to identify what went wrong
      // Was it a WebSphere MQ error?
    catch (MQException ex)
    {
      System.out.println("A WebSphere MQ error occurred : Completion code " +
                         ex.completionCode + " Reason code " + ex.reasonCode);
    }
      // Was it a Java buffer space error?
    catch (java.io.IOException ex)
    {
      System.out.println("An error occurred whilst writing to the message buffer: " + ex);
    }
  }
} // end of sample



© IBM Corporation 1997, 2002. All Rights Reserved