PCF with IBM's MQ Classes for .NET

For about 2 years IBM has been providing a supported class library that allows .NET apps to connect to MQSeries. There has always been PCF capability in the class library, but it's not documented and I suppose not supported either. (PCF is the Programmable Command Formats, and in a nutshell it defines a way you can administer MQSeries artifacts by sending and receiving messages to a queue. Rather than a separate admin API, MQ defines a set of message formats. When your app sends messages in the PCF format, you can query or update MQ resources, like queues, channels, queue managers, etc.)

If you are facile with Lutz Roeder's Reflector and have programmed PCF before using other environments (like Java or C or C++), then it's pretty straightforward to figure out how to use the PCF stuff in .NET. For those who don't fall under that category, here's an example of .NET PCF code using the MQ Classes for .NET that shipped in MQ v6.0.

    private bool MqQueueExists(string queuename) {

      PCFMessageAgent agent = new PCFMessageAgent(c.MQ_QueueManager);

      PCFMessage request= new PCFMessage(CMQCFC.MQCMD_INQUIRE_Q_NAMES);

      request.AddParameter (MQC.MQCA_Q_NAME, queuename);

      request.AddParameter (MQC.MQIA_Q_TYPE, MQC.MQQT_LOCAL);

      PCFMessage[] responses = agent.Send(request);

 

      if (responses==null) throw new Exception("bogus response from MQ PCF.");

      bool exists= (responses.Length!=1) || (responses[0].GetCompCode()!=0);

 

      return exists;

    }

 

    private void MqCreateQueue(string queuename) {

      PCFMessageAgent agent = new PCFMessageAgent(c.MQ_QueueManager);

      PCFMessage request= new PCFMessage(CMQCFC.MQCMD_CREATE_Q);

      request.AddParameter (MQC.MQCA_Q_NAME, queuename);

      request.AddParameter (MQC.MQIA_Q_TYPE, MQC.MQQT_LOCAL);

      request.AddParameter(MQC.MQCA_Q_DESC, "created by MQ.Create.exe on " + System.DateTime.Now.ToString("u"));

 

      PCFMessage[] responses = agent.Send(request);

    }

 

    private void ClearQueue(string queuename) {

      agent = new PCFMessageAgent(c.MQ_QueueManager);

      PCFMessage request= new PCFMessage(CMQCFC.MQCMD_CLEAR_Q);

      request.AddParameter (MQC.MQCA_Q_NAME, queuename);

      PCFMessage[] responses = agent.Send(request);

    }

 

There's lots more in PCF, not just creating queues or testing for their existence. Check the MQ header files and IBM's PCF docs for other platforms.

Disclaimer: I'd be wary of using this .NET PCF stuff in production. Since it is not documented, and probably not supported by IBM, you'd be out on a limb if something went bad with your mission-critical system. But, I found this PCF stuff to be really useful in test/development scenario, where I wanted to reset a queue to a known state, or create a queue, etc.

See the rest of the source code here

-Dino