The other day I created a C program to send an MQSC command to a z/OS Manager and since I have done the same thing in Java, I thought I would publish a Java program to send an MQSC command to a z/OS queue manager.
You can download it as a zip file from here or you can copy from below:
/**
* Program Name
* SendCmdTozOS.java
*
* Description
* SendCmdTozOS is a simple MQ program that will send an MQSC command
* to a z/OS queue manager's command queue and then wait for a response.
*
* Input Parameters:
* QMgrName ChlName hostname port "MQSC command"
*
* Where
* QMgrName is the queue manager name
* ChlName is the name of the channel to be used
* hostname is the hostname
* port is the port number
* "MQSC command" is the MQSC command you want to send to the z/OS queue manager
* i.e.
* SendCmdTozOS MQT1 TEST.CHL 10.10.10.10 1415 "DIS QL(*)"
*
* @author Roger Lacroix, Capitalware Inc.
* @return 0 for ok or 1 for failed.
* @version 1.0.0
* @license Apache 2 License
*/
import com.ibm.mq.*;
import java.io.IOException;
import java.util.Hashtable;
/**
* ==================================================================
*
* Description
* This class will:
* - Connect to a remote z/OS queue manager
* - Open a temporary dynamic queue
* - Put a message on the queue manager's command queue
* - Get a reply message from the temporary dynamic queue
* - Close the temporary dynamic queue
* - Disconnect from the queue manager
*
* ------------------------------------------------------------------
*/
public class SendCmdTozOS
{
private final static String VERSION = "1.0.0";
private final static String SYSTEM_COMMAND_INPUT = "SYSTEM.COMMAND.INPUT";
private final static String SYSTEM_MODEL_REPLY_QUEUE = "SYSTEM.COMMAND.REPLY.MODEL";
private final static String TEMP_DYNAMIC_QUEUE_PREFIX = "CSQ.*";
/**
* main entry point for this class.
*
* Input parameters
* @param args - pointer to a string array of input parameters
*/
public static void main(String[] args)
{
if (args.length != 5)
{
System.out.println("Usage: java SendCmdTozOS QMgrName ChlName hostname port \"MQSC command\"");
System.exit(1);
}
SendCmdTozOS sctz = new SendCmdTozOS(args[0], /* z/OS queue manager name */
args[1], /* z/OS channel name */
args[2], /* z/OS hostname */
args[3], /* z/OS port # */
args[4]); /* MQSC command for z/OS queue manager */
}
/**
* The constructor
*
* This routine will will send an MQSC command to a z/OS queue manager's
* command queue and then wait for a response.
*
* @param QMgrName - Queue Manager name
* @param chlName - channel name to qeue manager
* @param hostname - hostname/IP address
* @param port - port #
* @param MQSCCommand- MQSC command to be sent to the z/OS queue manager
*/
public SendCmdTozOS(String qMgrName, String chlName, String hostname, String port, String MQSCCommand)
{
super();
MQQueueManager qMgr = null;
MQQueue inQ = null;
MQQueue cmdQ = null;
int openInputOptions = MQC.MQOO_INQUIRE + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_INPUT_SHARED;
int openOutputOptions = MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING;
System.out.println("SendCmdTozOS v"+VERSION + " starting");
try
{
qMgr = doConnect(qMgrName, chlName, hostname, port);
System.out.println("Connected to '" + qMgrName + "'");
inQ = qMgr.accessQueue( SYSTEM_MODEL_REPLY_QUEUE,
openInputOptions,
null,
TEMP_DYNAMIC_QUEUE_PREFIX,
null );
System.out.println("Opened temporary dynamic queue '" + inQ.name + "'");
cmdQ = qMgr.accessQueue( SYSTEM_COMMAND_INPUT,
openOutputOptions,
null,
null,
null );
System.out.println("Opened command queue '" + SYSTEM_COMMAND_INPUT + "'");
sendMessage(qMgr, cmdQ, qMgrName, inQ.name, MQSCCommand);
getCmdMessages(inQ);
}
catch (MQException e)
{
System.err.println("MQException: CC=" + e.completionCode + " : RC=" + e.reasonCode);
}
catch (IOException e)
{
System.err.println("IOException: " + e.getLocalizedMessage());
}
finally
{
handleClosingQ(inQ);
handleClosingQ(cmdQ);
handleDisconnecting(qMgr);
}
System.out.println("SendCmdTozOS ending");
}
/**
* This routine will establish a connection between this application
* and a remote queue manager.
*
* @param QMgrName - Queue Manager name
* @param chlName - channel name to qeue manager
* @param hostname - hostname/IP address
* @param port - port #
*
* @exception MQException
*
* @return qMgr - MQQueueManager object
*/
private MQQueueManager doConnect(String qMgrName, String chlName, String hostname, String port) throws MQException
{
Hashtable mqht = new Hashtable();
mqht.put(MQC.CHANNEL_PROPERTY, chlName);
mqht.put(MQC.HOST_NAME_PROPERTY, hostname);
mqht.put(MQC.PORT_PROPERTY, new Integer(port));
return new MQQueueManager(qMgrName, mqht);
}
/**
* This routine will close the queue.
*
* @param q - queue object
*/
private void handleClosingQ(MQQueue q)
{
String qName = null;
try
{
if (q != null)
{
qName = q.name.trim();
q.close();
System.out.println("Closed queue '" + qName + "'");
}
}
catch (MQException e)
{
System.err.println("MQException: close() '"+qName+"' CC=" + e.completionCode + " : RC=" + e.reasonCode);
}
}
/**
* This routine will disconnect from the queue manager.
*
* @param qMgr - queue manager object
*/
private void handleDisconnecting(MQQueueManager qMgr)
{
String qMgrName = null;
try
{
if (qMgr != null)
{
qMgrName = qMgr.name;
qMgr.disconnect();
System.out.println("Disconnected from '" + qMgrName + "'");
}
}
catch (MQException e)
{
System.err.println("MQException: qMgr.disconnect() CC=" + e.completionCode + " : RC=" + e.reasonCode);
}
}
/**
* This routine will put the MQSC command on the queue manager's command queue.
*
* @param qMgr - connection object
* @param cmdQ - Object command Q
* @param qMgrName - Q Manager Name
* @param replyQName - Reply Q name
* @param MQSCCommand- MQSC Command
*
* @exception MQException
* @exception IOException
*/
private void sendMessage(MQQueueManager qMgr, MQQueue cmdQ, String qMgrName, String replyQName, String MQSCCommand) throws MQException, IOException
{
MQPutMessageOptions pmo = new MQPutMessageOptions();
pmo.options = MQC.MQPMO_FAIL_IF_QUIESCING | MQC.MQPMO_NO_SYNCPOINT;
MQMessage sendmsg = new MQMessage();
sendmsg.format = MQC.MQFMT_STRING;
sendmsg.feedback = MQC.MQFB_NONE;
sendmsg.messageType = MQC.MQMT_REQUEST;
sendmsg.replyToQueueName = replyQName;
sendmsg.replyToQueueManagerName = qMgrName;
sendmsg.messageId = MQC.MQMI_NONE;
sendmsg.correlationId = MQC.MQCI_NONE;
sendmsg.writeString(MQSCCommand);
// put the message on the command queue
cmdQ.put(sendmsg, pmo);
System.out.println("Cmd Sent: " + MQSCCommand);
}
/**
* This routine will get all reply messages from our temporary dynamic queue.
*
* Input parameters
* @param inQ - Object handle for reply Q
*
* Output
* Response messages will be written to stdout
*/
private void getCmdMessages(MQQueue inQ)
{
MQMessage getMsg = null;
MQGetMessageOptions getOptions = new MQGetMessageOptions();
getOptions.options = MQC.MQGMO_WAIT + MQC.MQGMO_FAIL_IF_QUIESCING + MQC.MQGMO_CONVERT + MQC.MQGMO_NO_SYNCPOINT;
getOptions.waitInterval = 5 * 1000; /* wait up to 5 seconds */
boolean more = true;
while (more)
{
getMsg = new MQMessage();
try
{
inQ.get(getMsg, getOptions);
byte[] b = new byte[getMsg.getMessageLength()];
getMsg.readFully(b);
String replyMsg = new String(b);
System.out.println("Response: " + replyMsg);
if (replyMsg.indexOf("CSQ9022I") != -1)
{
more = false; /* no more messages to follow */
}
}
catch (IOException e)
{
System.err.println("IOException: " + e.getMessage());
more = false;
}
catch (MQException e)
{
if ( (e.completionCode == MQException.MQCC_FAILED) &&
(e.reasonCode == MQException.MQRC_NO_MSG_AVAILABLE) )
{
// System.out.println("All messages read.");
}
else
{
System.err.println("MQException: CC=" + e.completionCode + " : RC=" + e.reasonCode);
}
more = false;
}
}
}
}
Regards,
Roger Lacroix
Capitalware Inc.