Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

receive message from IBM MQ in js app

Status
Not open for further replies.

pavloNP

Programmer
Oct 9, 2019
1
0
0
UA
Hi! I need to receive messages from IBM MQ in a js app.
The goal is:
1. Receive a message
2. Make several operations with date
3. When all operations are finished, make the same with the second message.

I face a problem. The await isn't worked. And I receive all messages and can't work in the "synchronic style" one after one.

const mq = require("ibmmq");
const { loadBaselineFile, dbStatus } = require("./dbLoader");
const dbl = require("./dbLoader");

const logger = require("../config/logerConfig");
const fs = require("fs");
const MQC = mq.MQC;

const StringDecoder = require("string_decoder").StringDecoder;
const decoder = new StringDecoder("utf8");

const qMgr = "QM1";
const qName = "TEST.QUEUE";
const msgId = null;

let connectionHandle;
let queueHandle;
let count = 0;

module.exports = { startListeningMQ };
function startListeningMQ() {
const cno = new mq.MQCNO();

const csp = new mq.MQCSP();
csp.UserId = "mqm";
csp.Password = "mqm";
cno.SecurityParms = csp;

cno_Options |= MQC.MQCNO_CLIENT_BINDING;
const cd = new mq.MQCD();
cd.ConnectionName = "localhost(1414)";
cd.ChannelName = "SVRCONN";
cno.ClientConn = cd;

mq.Conn(qMgr, function(err, hConn) {
if (err) {
console.log(formatErr(err));
} else {
console.log("MQCONN to %s successful ", qMgr);
connectionHandle = hConn;

const od = new mq.MQOD();
od.ObjectName = qName;
od.ObjectType = MQC.MQOT_Q;
const openOptions = MQC.MQOO_BROWSE;
mq.Open(hConn, od, openOptions, function(err, hObj) {
queueHandle = hObj;
if (err) {
console.log(formatErr(err));
} else {
console.log("MQOPEN of %s successful", qName);
getMessages();
}
});
}
});
}

function getMessages() {
const md = new mq.MQMD();
const gmo = new mq.MQGMO();

gmo_Options =
MQC.MQGMO_NO_SYNCPOINT |
MQC.MQGMO_WAIT |
MQC.MQGMO_CONVERT |
MQC.MQGMO_FAIL_IF_QUIESCING |
MQC.MQGMO_MQWI_UNLIMITED;
gmo_Options |= MQC.MQGMO_BROWSE_NEXT;

gmo.MatchOptions = MQC.MQMO_NONE;

if (msgId != null) {
console.log("Setting Match Option for MsgId");
gmo.MatchOptions = MQC.MQMO_MATCH_MSG_ID;
md.MsgId = hexToBytes(msgId);
}

mq.setTuningParameters({ getLoopPollTimeMs: 500 });
mq.Get(queueHandle, md, gmo, getCB);
}

function hexToBytes(hex) {
for (const bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return bytes;
}

async function getCB(err, hObj, gmo, md, buf, hConn) {
if (err) {
if (err.mqrc == MQC.MQRC_NO_MSG_AVAILABLE) {
console.log("No more messages available.");
} else {
console.log(formatErr(err));
exitCode = 1;
}
ok = false;
mq.GetSync(hObj);
} else {
if (md.Format == "MQSTR") {
console.log("message <%s>", decoder.write(buf));
count++;
try {
fs.writeFileSync(
"data_loader/dataForLoad/tryAgain" + count + ".json",
decoder.write(buf),
"utf8"
);
} catch (e) {
console.log("Cannot write file ", e);
}

const filePath =
`file:///home` +
`myfile.json`;
logger.serverLogger().info(`Start to upload BASELINE file ${filePath}`);


await MYFUNCTION(filePath);
gmo_Options &= ~MQC.MQGMO_BROWSE_FIRST;
gmo_Options |= MQC.MQGMO_BROWSE_NEXT;

} else {
console.log("binary message: " + buf);
}
}
}

function formatErr(err) {
if (err) {
ok = false;
return "MQ call failed at " + err.message;
} else {
return "MQ call successful";
}
}

function sleep {
setTimeout(function() {}, ms);
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top