Monday, August 27, 2012

Dynamic queue creation with WMQ


Today's task is dynamically creating persistant queues in WMQ(IBM's Websphere MQ) from within Java code. Out of all of the message brokers that I have researched, WMQ definetely was the hardest to figure out how to create queues from code.  Although, on the positive side, at least it is possible.  

In order to invoke the broker from Java, you must use WMQ's API which they call PCF (Programming Command Format).  PCF allows you to do a wide range of admin tasks such as viewing current queues and creating new queues. In order to perform a specific action, you must create a PCFMessage and add all of the needed parameters to it (You also need to make sure that you add them in the correct order!?). A list of all of the required and optional parameters can be found here. After setting all of the parameters, all you need to do is create a PCFMessageAgent and send the parameters. Done. It seems easy now, but most of my time was spent fishing through IBM documentation looking for the correct syntax and parameter names.

If you are interested in other functionality provided by PCF, there are samples included with the installation of the WMQ client found here.  After installation, by default the samples are location at: C:\Program Files (x86)\IBM\WebSphere MQ\tools\pcf\samples
Source:
   public void createQueue(String queueName) throws Exception {
      PCFMessageAgent agent = null;

      // Create the PCF message type for the create queue.
      // NOTE: The parameters must be added in a specific order or an exception (3015) will be thrown.
      // The required order is Name, type, then all optional parameters
      PCFMessage pcfCmd = new PCFMessage(MQConstants.MQCMD_CREATE_Q);

      // Queue name - Mandatory.
      pcfCmd.addParameter(MQConstants.MQCA_Q_NAME, queueName);

      // Queue Type - Optional.
      pcfCmd.addParameter(MQConstants.MQIA_Q_TYPE, MQConstants.MQQT_LOCAL);

      // Add description.
      pcfCmd.addParameter(MQConstants.MQCA_Q_DESC, "Queue created by PCF test");
     
      //set the max queue depth
      pcfCmd.addParameter(MQConstants.MQIA_MAX_Q_DEPTH, 999999);
     
      //Make queue persistant
      pcfCmd.addParameter(MQConstants.MQIA_DEF_PERSISTENCE, MQConstants.MQPER_PERSISTENT);

      //backout queue
      pcfCmd.addParameter(MQConstants.MQCA_BACKOUT_REQ_Q_NAME, "INVALID." + queueName);
     
      //harden backout count-determines if backout count is saved after restart
      pcfCmd.addParameter(MQConstants.MQIA_HARDEN_GET_BACKOUT,MQConstants.MQQA_BACKOUT_NOT_HARDENED);
     
      try {
          agent = new PCFMessageAgent(BROKER_HOST, BROKER_PORT, DEFAULT_CHANNEL_NAME);
          //An exception will be thrown if the queue already exists
          agent.send(pcfCmd);
          System.out.println("Successfull created queue: " + queueName);
      }
      catch (PCFException pcfe) {
       System.out.println("Failed to create queue");
       if (pcfe.reasonCode == MQConstants.MQRCCF_OBJECT_ALREADY_EXISTS) {
        System.out.println("The queue " + queueName + " already exists on the queue manager.");
       }
      }
      finally{
       // Disconnect the agent.
          agent.disconnect();
      }
      return;
   }

1 comment:

  1. Are you in search of suitable php coders for hire for developing an innovative and customized website? If so, check out this list of.

    ReplyDelete