forked from ibm-messaging/mq-dev-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasicrequest.js
327 lines (273 loc) · 9.41 KB
/
basicrequest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/**
* Copyright 2018, 2019 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
// This is a demonstration showing a request operations onto a MQ Queue
// Using the MQI Node.js interface. The application creates a
// dynamic queue for the responder to reply to, posts the request
// and waits for a response.
//
// This application is based on the samples
// https://github.com/ibm-messaging/mq-mqi-nodejs/blob/master/samples/amqsconn.js
// https://github.com/ibm-messaging/mq-mqi-nodejs/blob/master/samples/amqsput.js
// and
// https://github.com/ibm-messaging/mq-mqi-nodejs/blob/master/samples/amqsget.js
//
// Values for Queue Manager, Queue, Host, Port and Channel are
// passed in as envrionment variables.
const fs = require('fs');
// Import the MQ package
const mq = require('ibmmq');
var StringDecoder = require('string_decoder').StringDecoder;
var decoder = new StringDecoder('utf8');
// Load up missing envrionment variables from the env.json file
var env = require('../env.json');
var MQC = mq.MQC;
var waitInterval = 5;
// Set up debug logging options
var debug_info = require('debug')('amqsreq:info');
var debug_warn = require('debug')('amqsreq:warn');
// Set up Constants
const CCDT = "MQCCDTURL";
const FILEPREFIX = "file://";
// Load the MQ Endpoint details either from the envrionment or from the
// env.json file. The envrionment takes precedence. The json file allows for
// mulitple endpoints ala a cluster.
// The Connection string is built using HOST(PORT) settings for all
// the endpoints.
var MQDetails = {};
['QMGR', 'QUEUE_NAME',
'MODEL_QUEUE_NAME', 'DYNAMIC_QUEUE_PREFIX',
'HOST', 'PORT',
'CHANNEL', 'KEY_REPOSITORY', 'CIPHER'].forEach(function(f) {
MQDetails[f] = process.env[f] || env.MQ_ENDPOINTS[0][f]
});
var credentials = {
USER: process.env.APP_USER || env.MQ_ENDPOINTS[0].APP_USER,
PASSWORD: process.env.APP_PASSWORD || env.MQ_ENDPOINTS[0].APP_PASSWORD
}
// Global variables
var ok = true;
function toHexString(byteArray) {
return byteArray.reduce((output, elem) =>
(output + ('0' + elem.toString(16)).slice(-2)),
'');
}
function hexToBytes(hex) {
for (var bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return bytes;
}
function getConnection() {
let points = [];
env.MQ_ENDPOINTS.forEach((p) => {
if (p['HOST'] && p['PORT']) {
points.push(`${p.HOST}(${p.PORT})`)
}
});
return points.join(',');
}
// Define some functions that will be used from the main flow
function putMessage(hObj, hObjDynamic, cb) {
// var msg = "Hello from Node at " + new Date();
var msgObject = {
'Greeting': "Hello from Node at " + new Date(),
'value': Math.floor(Math.random() * 100)
}
var msg = JSON.stringify(msgObject);
var mqmd = new mq.MQMD(); // Defaults are fine.
mqmd.ReplyToQ = hObjDynamic._name;
mqmd.MsgType = MQC.MQMT_REQUEST;
var pmo = new mq.MQPMO();
// Describe how the Put should behave
pmo.Options = MQC.MQPMO_NO_SYNCPOINT |
MQC.MQPMO_NEW_MSG_ID |
MQC.MQPMO_NEW_CORREL_ID;
mq.Put(hObj, mqmd, pmo, msg, function(err) {
if (err) {
debug_warn('Error Detected in Put operation', err);
cb(err, null);
} else {
var msgId = toHexString(mqmd.MsgId);
debug_info('MsgId: ', msgId);
debug_info("MQPUT successful");
cb(null, msgId);
}
});
}
function awaitResponse(hObjDynamic, msgId, cb) {
debug_info('Setting up timer');
setTimeout(getResponse, waitInterval * 1000, hObjDynamic, msgId, cb);
//getResponse(hObjDynamic, msgId);
}
function getResponse(hObjDynamic, msgId, cb) {
debug_info("Waiting for response to request for msgId ", msgId);
var buf = Buffer.alloc(1024);
var mqmd = new mq.MQMD();
var gmo = new mq.MQGMO();
gmo.Options = MQC.MQGMO_NO_SYNCPOINT |
MQC.MQGMO_NO_WAIT |
MQC.MQGMO_CONVERT |
MQC.MQGMO_FAIL_IF_QUIESCING;
gmo.MatchOptions = MQC.MQMO_MATCH_MSG_ID;
mqmd.MsgId = hexToBytes(msgId);
// gmo.MatchOptions = MQC.MQMO_MATCH_CORREL_ID
// mqmd.CorrelId = hexToBytes(msgId);
mq.GetSync(hObjDynamic, mqmd, gmo, buf, function(err, len) {
if (err) {
if (err.mqrc == MQC.MQRC_NO_MSG_AVAILABLE) {
debug_info("no more messages");
if (ok) {
awaitResponse(hObjDynamic, msgId, cb);
}
} else {
debug_warn('Error retrieving message', err);
cb(err, null);
ok = false;
}
} else if (mqmd.Format == "MQSTR") {
var msgObject = null;
try {
msgObject = JSON.parse(buf);
debug_info("Message Object found", msgObject);
cb(msgObject, null);
} catch (err) {
debug_info("message <%s>", decoder.write(buf));
cb(err, null);
}
} else {
debug_info("binary message: " + buf);
cb(buf, null);
}
});
}
// When we're done, close queues and connections
function cleanup(hConn, hObj, hObjDynamic) {
mq.Close(hObjDynamic, 0, function(err) {
if (err) {
debug_warn('Error Detected in Close operation of Dynamic queue', err);
} else {
debug_info("MQCLOSE successful of dynamic queue");
}
mq.Close(hObj, 0, function(err) {
if (err) {
debug_warn('Error Detected in Close operation', err);
} else {
debug_info("MQCLOSE successful");
}
mq.Disc(hConn, function(err) {
if (err) {
debug_warn('Error Detected in Disconnect operation', err);
} else {
debug_info("MQDISC successful");
}
});
});
});
}
function ccdtCheck () {
if (CCDT in process.env) {
let ccdtFile = process.env[CCDT].replace(FILEPREFIX, '');
debug_info(ccdtFile);
if (fs.existsSync(ccdtFile)) {
debug_info("CCDT File found at ", ccdtFile);
return true;
}
}
return false;
}
debug_info('Running on ', process.platform);
debug_info('Starting up Application');
var cno = new mq.MQCNO();
// use MQCNO_CLIENT_BINDING to connect as client
// cno.Options = MQC.MQCNO_NONE;
cno.Options = MQC.MQCNO_CLIENT_BINDING;
// For no authentication, disable this block
if (credentials.USER) {
var csp = new mq.MQCSP();
csp.UserId = credentials.USER;
csp.Password = credentials.PASSWORD;
cno.SecurityParms = csp;
}
if (! ccdtCheck()) {
debug_info('CCDT URL export is not set, will be using json envrionment client connections settings');
// And then fill in relevant fields for the MQCD
var cd = new mq.MQCD();
cd.ChannelName = MQDetails.CHANNEL;
cd.ConnectionName = getConnection();
debug_info('Connections string is ', cd.ConnectionName);
if (MQDetails.KEY_REPOSITORY) {
debug_info('Will be running in TLS Mode');
cd.SSLCipherSpec = MQDetails.CIPHER;
cd.SSLClientAuth = MQC.MQSCA_OPTIONAL;
}
// Make the MQCNO refer to the MQCD
cno.ClientConn = cd;
}
// The location of the KeyRepository is not specified in the CCDT, so regardless
// of whether a CCDT is being used, need to specify the KeyRepository location
// if it has been provided in the environment json settings.
if (MQDetails.KEY_REPOSITORY) {
debug_info('Key Repository has been specified');
// *** For TLS ***
var sco = new mq.MQSCO();
sco.KeyRepository = MQDetails.KEY_REPOSITORY;
// And make the CNO refer to the SSL Connection Options
cno.SSLConfig = sco;
}
debug_info('Attempting Connection to MQ Server');
mq.Connx(MQDetails.QMGR, cno, function(err, hConn) {
debug_info('Inside Connection Callback function');
if (err) {
debug_warn('Error Detected making Connection', err);
} else {
debug_info("MQCONN to %s successful ", MQDetails.QMGR);
// Open up the Dynamic Response queue
var odDynamic = new mq.MQOD();
odDynamic.ObjectName = MQDetails.MODEL_QUEUE_NAME;
odDynamic.DynamicQName = MQDetails.DYNAMIC_QUEUE_PREFIX;
var openDynamicOptions = MQC.MQOO_INPUT_EXCLUSIVE;
//var openDynamicOptions = null;
mq.Open(hConn, odDynamic, openDynamicOptions, function(err, hObjDynamic) {
debug_info('Inside MQ Open Dynamic Queue Callback function');
if (err) {
debug_warn('Error Detected Opening MQ Connection', err);
} else {
debug_info("MQOPEN of Dynamic Queue %s successful", hObjDynamic._name);
debug_info(hObjDynamic);
// Define what we want to open, and how we want to open it.
var od = new mq.MQOD();
od.ObjectName = MQDetails.QUEUE_NAME;
od.ObjectType = MQC.MQOT_Q;
var openOptions = MQC.MQOO_OUTPUT;
mq.Open(hConn, od, openOptions, function(err, hObj) {
debug_info('Inside MQ Open Callback function');
if (err) {
debug_warn('Error Detected Opening MQ Connection', err);
} else {
debug_info("MQOPEN of %s successful", MQDetails.QUEUE_NAME);
putMessage(hObj, hObjDynamic, (err, msgId) => {
if (msgId) {
awaitResponse(hObjDynamic, msgId, (err, msg) => {
cleanup(hConn, hObj, hObjDynamic);
});
}
});
}
});
}
});
}
});
debug_info('Application Start Completed');