Search code examples
javaibm-mqpcf

IBM MQ list queues with username/password


I'm pretty new to IBM MQ, but I have looked at samples from IBM and written a tool in Java that can list queue names. I'm using PCFAgent:

PCFAgent agent = new PCFAgent(hostname, port, channelName);

PCFParameter[] parameters = { new MQCFST(CMQC.MQCA_Q_NAME, "*"),
   new MQCFIN(CMQC.MQIA_Q_TYPE, CMQC.MQQT_LOCAL)};

MQMessage[] responses = agent.send(CMQCFC.MQCMD_INQUIRE_Q_NAMES, parameters);
            MQCFH cfh = new MQCFH(responses[0]);

for (int i = 0; i < cfh.parameterCount; i++) {
    System.out.println (PCFParameter.nextParameter (responses [0]));
}

This works fine when there are no username/password authorization on the MQ, but when this is in place, I get the error RC2035 "MQRC_NOT_AUTHORIZED".

Is it possible to authenticate by specifying username/password somehow, using PCFAgent like this?

(Or, is there another way to authenticate and list queues?)


Solution

  • You can use the alternate PCFAgent constructor which takes an MQQueueManager object.

    To set connection parameters and UserID/password when creating the MQQueueManager object, use either the static MQEnvironment class, or pass a hashtable of desired properties on the MQQueueManager constructor.

    https://www.ibm.com/support/knowledgecenter/en/SSFKSJ_8.0.0/com.ibm.mq.javadoc.doc/WMQJavaClasses/com/ibm/mq/MQQueueManager.html

    https://www.ibm.com/support/knowledgecenter/en/SSFKSJ_8.0.0/com.ibm.mq.javadoc.doc/WMQJavaClasses/com/ibm/mq/MQEnvironment.html

    Hashtable<String, Comparable<?>> props = new Hashtable<String, Comparable<?>>();
    
    props.put(MQConstants.TRANSPORT_PROPERTY, MQConstants.TRANSPORT_MQSERIES_CLIENT);
    props.put(MQConstants.HOST_NAME_PROPERTY, hostname);
    props.put(MQConstants.CHANNEL_PROPERTY, channel);
    props.put(MQConstants.PORT_PROPERTY, new Integer(port));
    props.put(MQConstants.USER_ID_PROPERTY, userName);
    props.put(MQConstants.USE_MQCSP_AUTHENTICATION_PROPERTY, Boolean.TRUE);
    props.put(MQConstants.PASSWORD_PROPERTY, password);
    
    MQQueueManager qm = new MQQueueManager("MY.QM", props);
    
    PCFAgent agent = new PCFAgent(qm);