I'm building a Node.js middleware server that connects to IBM MQ 9.4 running in a Docker container. I am using the official IBM MQ Node.js library (ibmmq
) to send and receive messages.
I can successfully put messages into a queue, but getting (consuming) messages always hangs. I’ve verified that messages exist in the queue, and I’ve granted full permissions to the user.
Tried MQOO_INPUT_SHARED
, MQOO_INPUT_AS_Q_DEF
and MQOO_BROWSE
— no difference.
function receiveFromMQ() {
return new Promise((resolve, reject) => {
const cno = new mq.MQCNO();
cno.Options = MQC.MQCNO_CLIENT_BINDING;
const cd = new mq.MQCD();
cd.ConnectionName = "localhost(1414)";
cd.ChannelName = "DEV.APP.SVRCONN";
cno.ClientConn = cd;
mq.Connx("QM1", cno, (connErr, hConn) => {
if (connErr) return reject(connErr);
const od = new mq.MQOD();
od.ObjectName = "MY.DEV.QUEUE.1";
od.ObjectType = MQC.MQOT_Q;
mq.Open(hConn, od, MQC.MQOO_INPUT_AS_Q_DEF, (openErr, hObj) => {
if (openErr) return reject(openErr);
const md = new mq.MQMD();
const gmo = new mq.MQGMO();
gmo.Options = MQC.MQGMO_NO_SYNCPOINT | MQC.MQGMO_WAIT;
gmo.WaitInterval = 3000;
mq.Get(hObj, md, gmo, (getErr, data) => {
mq.Close(hObj, 0, () => mq.Disc(hConn));
if (getErr) {
if (getErr.mqrc === MQC.MQRC_NO_MSG_AVAILABLE) {
resolve(null);
} else {
reject(getErr);
}
} else {
resolve(data.toString());
}
});
});
});
});
}
I've found a solution to the issue with the Get()
function hanging — switching to GetSync()
resolves the problem. I'm still unsure why Get()
hangs when retrieving the message.
const buf = Buffer.alloc(1024); // Read buffer
try {
const datalen = mq.GetSync(hObj, md, gmo, buf);
const message = Buffer.from(buf.subarray(0, datalen)).toString();
mq.Close(hObj, 0, (closeErr) => {
if (closeErr) console.error('Close error:', closeErr);
mq.Disc(hConn, (discErr) => {
if (discErr) console.error('Disconnect error:', discErr);
});
});
resolve(message);
} catch (getErr) {
mq.Close(hObj, 0, () => mq.Disc(hConn, () => {}));
if (getErr.mqrc === MQC.MQRC_NO_MSG_AVAILABLE) {
resolve(null); // No message to read
} else {
reject(getErr); // Actual error
}
}