Example code (server bindings connection)

The following C# code fragment demonstrates an application that uses server bindings mode to perform three actions:

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

using System;
using IBM.WMQ;

public class MQSample1
{
  private String qManager ="your_Q_manager"; //define name of queue
                                             //manager to connect to.
  private MQQueueManager qMgr;  //define a queue manager object

  static void Main(string[] args)
  {
    new MQSample1();
  }
  public MQSample1()
  {
    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
      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);
    }
    //Was it a System error?
    catch (System.Exception ex)
    {
      Console.WriteLine("A System error occurred:"+ex);
    }
  }
}//end of sample