'Cannot create queue from Websphere JMS API
I wanted to create IBM queues from REST endpoint as described in documentation https://www.ibm.com/docs/en/ibm-mq/9.1?topic=applications-creating-destinations-in-jms-application
IBM version - 9.1.0.5
Queue Manager is started and listener is running
Application and IBM server are on the same host
Authorization is turned off on the server side
Connection and session is successfully created but the problem is that I'm not able to create queue directly in the server (queue is not listed in /var/mqm/qmgrs/QMGR/queues). In the application I see that the object with expected fields is created but it's not propagated to WebSphere. There is no error in AMQERR01.LOG.
Do you know what could be wrong with my configuration?
Do I need to use PCF to accomplish this?
I was trying a lot of ways to create queue (some examples below):
MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
mqQueueConnectionFactory.setQueueManager("QMGR");
mqQueueConnectionFactory.setHostName("X.X.X.X");
mqQueueConnectionFactory.setChannel("CHANNEL.X");
mqQueueConnectionFactory.setTransportType(com.ibm.mq.jms.JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
QueueConnection queueConnection = mqQueueConnectionFactory.createQueueConnection();
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queueSession.createQueue(name);
----------------------------------------------------------------------------------------
JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
JmsConnectionFactory cf = ff.createConnectionFactory();
cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, "X.X.X.X");
cf.setIntProperty(WMQConstants.WMQ_PORT, 1414);
cf.setStringProperty(WMQConstants.WMQ_CHANNEL, "CHANNEL.X");
cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, "QMGR");
cf.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, "MQ provider");
cf.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, false);
JMSContext context = cf.createContext();
Destination destination = context.createQueue("queue:///" + name);
System.out.println(destination.toString());
I was able to achieve this functionality with python pymqi libray but I need to do it in Java:
import pymqi
from flask import Flask
app = Flask(__name__)
HOST = 'X.X.X.X'
PORT = '1414'
CONN_INFO = '%s(%s)' % (HOST, PORT)
QUEUE_MANAGER = 'QMGR'
@app.route('/queue/<string:channel_name>/<string:queue_name>')
def create_queue(channel_name, queue_name):
queue_type = pymqi.CMQC.MQQT_LOCAL
max_depth = 123456
args = {pymqi.CMQC.MQCA_Q_NAME: queue_name.encode('ascii'),
pymqi.CMQC.MQIA_Q_TYPE: queue_type,
pymqi.CMQC.MQIA_MAX_Q_DEPTH: max_depth}
qmgr = pymqi.connect(QUEUE_MANAGER, channel_name.encode('ascii'), CONN_INFO)
pcf = pymqi.PCFExecute(qmgr)
pcf.MQCMD_CREATE_Q(args)
qmgr.disconnect()
return "OK"
app.run(host='0.0.0.0', port=8080)
Solution 1:[1]
cf.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, false);
In this day & age, that's a bad idea. Your program should be supplying a valid UserId and Password when connecting to a remote queue manager.
You can create IBM MQ queues using PCF via JMS but it is a lot more complicated.
Here is a fully functioning Java/PCF (non-JMS) program that creates a queue.
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import com.ibm.mq.MQException;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.constants.CMQC;
import com.ibm.mq.constants.MQConstants;
import com.ibm.mq.headers.MQDataException;
import com.ibm.mq.headers.pcf.PCFException;
import com.ibm.mq.headers.pcf.PCFMessage;
import com.ibm.mq.headers.pcf.PCFMessageAgent;
/**
* Program Name
* MQCreateQueue01
*
* Description
* This java class issues a PCF "create queue" request message to create a local queue
* specifying: queue type, max message length & max queue depth on a remote queue manager.
*
* This PCF code is equivalent to issuing the runmqsc command of:
* DEFINE QL('TEST.QUEUE') MAXMSGL(10485760) MAXDEPTH(12345)
*
* Sample Command Line Parameters
* -m MQA1 -q Queue_Name -h 127.0.0.1 -p 1414 -c TEST.CHL -u UserID -x Password
*
* @author Roger Lacroix
*/
public class MQCreateQueue01
{
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;
public MQCreateQueue01()
{
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("-u") && params.containsKey("-x");
if (b)
{
try
{
Integer.parseInt((String) params.get("-p"));
}
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())
{
try
{
port = Integer.parseInt((String) params.get("-p"));
}
catch (NumberFormatException e)
{
port = 1414;
}
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();
}
}
/**
* Handle connecting to the queue manager, issuing PCF command then
* looping through PCF response messages and disconnecting from
* the queue manager.
*/
private void doPCF()
{
String qMgrName = (String) params.get("-m");
String qName = (String) params.get("-q");
MQQueueManager qMgr = null;
PCFMessageAgent agent = null;
PCFMessage request = null;
try
{
qMgr = new MQQueueManager(qMgrName, mqht);
logger("successfully connected to "+ qMgrName);
agent = new PCFMessageAgent(qMgr);
logger("successfully created agent");
// https://www.ibm.com/docs/en/ibm-mq/latest?topic=formats-change-copy-create-queue
request = new PCFMessage(MQConstants.MQCMD_CREATE_Q);
// Specify the queue name to be created.
request.addParameter(MQConstants.MQCA_Q_NAME, qName);
// Add parameter to request local queue
request.addParameter(MQConstants.MQIA_Q_TYPE, MQConstants.MQQT_LOCAL);
// Add parameter to set the max message length to be 10MB
request.addParameter(MQConstants.MQIA_MAX_MSG_LENGTH, 10 * 1024 * 1024);
// Add parameter to set the max queue depth to be 12,345
request.addParameter(MQConstants.MQIA_MAX_Q_DEPTH, 12345);
agent.send(request);
logger("successfully created queue: " + qName);
}
catch (PCFException pcfe)
{
if (pcfe.reasonCode == MQConstants.MQRCCF_OBJECT_ALREADY_EXISTS)
logger("Error: The queue '" + qName+ "' already exists on the queue manager '" + qMgrName + "'.");
else
logger("CC=" +pcfe.completionCode + " [" + MQConstants.lookupCompCode(pcfe.completionCode) + "] : RC=" + pcfe.reasonCode + " ["+MQConstants.lookupReasonCode(pcfe.reasonCode)+"]");
}
catch (MQException e)
{
logger("CC=" +e.completionCode + " [" + MQConstants.lookupCompCode(e.completionCode) + "] : RC=" + e.reasonCode + " ["+MQConstants.lookupReasonCode(e.reasonCode)+"]");
}
catch (IOException e)
{
logger("IOException:" +e.getLocalizedMessage());
}
catch (MQDataException e)
{
logger("MQDataException:" +e.getLocalizedMessage());
}
catch (Exception e)
{
logger("Exception:" +e.getLocalizedMessage());
}
finally
{
try
{
if (agent != null)
{
agent.disconnect();
logger("disconnected from agent");
}
}
catch (MQDataException e)
{
logger("CC=" +e.completionCode + " : RC=" + e.reasonCode);
}
try
{
if (qMgr != null)
{
qMgr.disconnect();
logger("disconnected from "+ qMgrName);
}
}
catch (MQException e)
{
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);
}
public static void main(String[] args)
{
MQCreateQueue01 mqlqs = new MQCreateQueue01();
try
{
mqlqs.init(args);
mqlqs.doPCF();
}
catch (IllegalArgumentException e)
{
logger("Usage: java MQCreateQueue01 -m QueueManagerName -q Queue_Name -h host -p port -c channel -u UserID -x Password");
System.exit(1);
}
System.exit(0);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |