Inhibit/Allow the Get & Put Actions of a Queue

On StackOverflow, someone asked a question about inhibiting get actions against a queue.

Here is a simple Java/MQ program that will set inhibit or allow for the GET and PUT attributes of a queue.

You can download the source code from here.

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;

import com.ibm.mq.MQException;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.constants.CMQC;

/**
 * Program Name
 *  MQTest15
 *
 * Description
 *  This java class will connect to a remote queue manager with the
 *  MQ setting stored in a HashTable and set the queue's get & put attributes to 
 *  either inhibit or allow.
 *
 * Sample Command Line Parameters
 *  -m MQA1 -h 127.0.0.1 -p 1414 -c TEST.CHL -q TEST.Q1 -i 0/1 -o 0/1 -u UserID -x Password
 *
 * @author Roger Lacroix
 */
public class MQTest15
{
   private static final SimpleDateFormat  LOGGER_TIMESTAMP = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");

   private Hashtable<String,String> params;
   private Hashtable<String,Object> mqht;
   private String qMgrName;
   private String outputQName;
   private boolean getInhibit = false;
   private boolean putInhibit = false;

   /**
    * The constructor
    */
   public MQTest15()
   {
      super();
      params = new Hashtable<String,String>();
      mqht = new Hashtable<String,Object>();
   }

   /**
    * Make sure the required parameters are present.
    * @return true/false
    */
   private boolean allParamsPresent()
   {
      boolean b = params.containsKey("-h") && params.containsKey("-p") &&
                  params.containsKey("-c") && params.containsKey("-m") &&
                  params.containsKey("-q") && 
                  params.containsKey("-i") && params.containsKey("-o") &&
                  params.containsKey("-u") && params.containsKey("-x");
      if (b)
      {
         try
         {
            Integer.parseInt((String) params.get("-p"));
            Integer.parseInt((String) params.get("-i"));
            Integer.parseInt((String) params.get("-o"));
         }
         catch (NumberFormatException e)
         {
            b = false;
         }
      }

      return b;
   }

   /**
    * Extract the command-line parameters and initialize the MQ HashTable.
    * @param args
    * @throws IllegalArgumentException
    */
   private void init(String[] args) throws IllegalArgumentException
   {
      int port = 1414;
      if (args.length > 0 && (args.length % 2) == 0)
      {
         for (int i = 0; i < args.length; i += 2)
         {
            params.put(args[i], args[i + 1]);
         }
      }
      else
      {
         throw new IllegalArgumentException();
      }

      if (allParamsPresent())
      {
         qMgrName = (String) params.get("-m");
         outputQName = (String) params.get("-q");

         try
         {
            port = Integer.parseInt((String) params.get("-p"));
         }
         catch (NumberFormatException e)
         {
            port = 1414;
         }

         try
         {
            int temp = Integer.parseInt((String) params.get("-i"));
            if (temp == 1)
               getInhibit = true;
         }
         catch (NumberFormatException e)
         {
            getInhibit = false;
         }

         try
         {
            int temp = Integer.parseInt((String) params.get("-o"));
            if (temp == 1)
               putInhibit = true;
         }
         catch (NumberFormatException e)
         {
            putInhibit = false;
         }
         
         mqht.put(CMQC.CHANNEL_PROPERTY, params.get("-c"));
         mqht.put(CMQC.HOST_NAME_PROPERTY, params.get("-h"));
         mqht.put(CMQC.PORT_PROPERTY, new Integer(port));
         mqht.put(CMQC.USER_ID_PROPERTY, params.get("-u"));
         mqht.put(CMQC.PASSWORD_PROPERTY, params.get("-x"));

         // I don't want to see MQ exceptions at the console.
         MQException.log = null;
      }
      else
      {
         throw new IllegalArgumentException();
      }
   }

   /**
    * Connect, open queue, set the queue's get & put attributes with 
    * either inhibit or allow, close queue and disconnect.
    *
    */
   private void doIt()
   {
      MQQueueManager qMgr = null;
      MQQueue queue = null;
      int openOptions = CMQC.MQOO_SET + CMQC.MQOO_FAIL_IF_QUIESCING;

      try
      {
         qMgr = new MQQueueManager(qMgrName, mqht);
         MQTest15.logger("successfully connected to "+ qMgrName);

         queue = qMgr.accessQueue(outputQName, openOptions);
         MQTest15.logger("successfully opened "+ outputQName);

         if (getInhibit)
         {
            queue.setInhibitGet(CMQC.MQQA_GET_INHIBITED);
            MQTest15.logger("Set inhibited get on queue: " +outputQName);
         }
         else
         {
            queue.setInhibitGet(CMQC.MQQA_GET_ALLOWED);
            MQTest15.logger("Set allowed get on queue: " +outputQName);
         }

         if (putInhibit)
         {
            queue.setInhibitPut(CMQC.MQQA_PUT_INHIBITED);
            MQTest15.logger("Set inhibited put on queue: " +outputQName);
         }
         else
         {
            queue.setInhibitPut(CMQC.MQQA_PUT_ALLOWED);
            MQTest15.logger("Set allowed put on queue: " +outputQName);
         }
      }
      catch (MQException e)
      {
         MQTest15.logger("CC=" +e.completionCode + " : RC=" + e.reasonCode);
      }
      finally
      {
         try
         {
            if (queue != null)
            {
               queue.close();
               MQTest15.logger("closed: "+ outputQName);
            }
         }
         catch (MQException e)
         {
            MQTest15.logger("CC=" +e.completionCode + " : RC=" + e.reasonCode);
         }
         try
         {
            if (qMgr != null)
            {
               qMgr.disconnect();
               MQTest15.logger("disconnected from "+ qMgrName);
            }
         }
         catch (MQException e)
         {
            MQTest15.logger("CC=" +e.completionCode + " : RC=" + e.reasonCode);
         }
      }
   }

   /**
    * A simple logger method
    * @param data
    */
   public static void logger(String data)
   {
      String className = Thread.currentThread().getStackTrace()[2].getClassName();

      // Remove the package info.
      if ( (className != null) && (className.lastIndexOf('.') != -1) )
         className = className.substring(className.lastIndexOf('.')+1);

      System.out.println(LOGGER_TIMESTAMP.format(new Date())+" "+className+": "+Thread.currentThread().getStackTrace()[2].getMethodName()+": "+data);
   }

   /**
    * main line
    * @param args
    */
   public static void main(String[] args)
   {
      MQTest15 write = new MQTest15();

      try
      {
         write.init(args);
         write.doIt();
      }
      catch (IllegalArgumentException e)
      {
         MQTest15.logger("Usage: java MQTest15 -m QueueManagerName -h host -p port -c channel -q QueueName -i 0/1 -o 0/1 -u UserID -x Password");
         System.exit(1);
      }

      System.exit(0);
   }
}

Regards,
Roger Lacroix
Capitalware Inc.

This entry was posted in HPE NonStop, IBM i (OS/400), IBM MQ, IBM MQ Appliance, Java, Linux, macOS (Mac OS X), Open Source, Programming, Raspberry Pi, Unix, Windows, z/OS.

Comments are closed.