Example code (client connection)

The following C# code fragment demonstrates an application that uses a client connection to perform three actions:

  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 .NET example code (client connection)
// ===========================================================================
// Licensed Materials - Property of IBM
// 5639-C34
// (c) Copyright IBM Corp. 2003, 2005
// ===========================================================================

using System;
using IBM.WMQ;

class MQSample
{

  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.

  //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 and channel
    MQEnvironment.Channel = channel;             //string directly here!
  }//end of init

  
  public void start()
  {
    try 
    {
      //Create a connection to the queue manager
      MQQueueManager qMgr =new MQQueueManager(qManager);

      //Set up the options on the queue we wish to open...
      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();
      Console.WriteLine("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)
    {
      Console.WriteLine("A WebSphere MQ error occurred :Completion code "+
      ex.CompletionCode +
      "Reason code "+ex.ReasonCode);
    }
    catch (System.Exception ex)
    {
      Console.WriteLine("A System error occurred:"+ex);
    }
  }//end of start
}//end of sample