Skip to content

Commit fc8265e

Browse files
committed
Fix bug produce
1 parent b29637f commit fc8265e

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/context.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export function useContext(db: Db, conf: Conf): ApplicationContext {
6363
const handler = new Handler<User, string>(writer.write, validator.validate, retries, errorHandler.error, logger.error, logger.info);
6464

6565
const producer = new Producer<User>(conf.ibmmq, logger.error, logger.info);
66-
return { health, log, produce: producer.produce, consume: consumer.subscribe, handle: handler.handle };
66+
return { health, log, produce: producer.produce, consume: consumer.queue, handle: handler.handle };
6767
}
6868
export function writeUser(msg: User): Promise<number> {
6969
console.log('Error: ' + JSON.stringify(msg));

src/ibmmq/producer.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ export class Producer<T> {
4646
const qMgr = this.conf.mgr;
4747
const topicString = this.conf.topic;
4848
// Define some functions that will be used from the main flow
49-
const publishMessage = (hObj: any) => {
49+
const publishMessage = (hObj: any): boolean => {
50+
let succeed = true;
5051
const msg = JSON.stringify(data as any);
5152
const mqmd = new this.mq.MQMD(); // Defaults are fine.
5253
const pmo = new this.mq.MQPMO();
@@ -63,13 +64,15 @@ export class Producer<T> {
6364
if (err) {
6465
if (lgErr) {
6566
lgErr(formatErr(err));
67+
succeed = false;
6668
}
6769
} else {
6870
if (lg) {
6971
lg('MQPUT successful');
7072
}
7173
}
7274
});
75+
return succeed;
7376
};
7477
// The program really starts here.
7578
// Connect to the queue manager. If that works, the callback function
@@ -83,6 +86,7 @@ export class Producer<T> {
8386
if (err) {
8487
if (lgErr) {
8588
lgErr(formatErr(err));
89+
return reject(formatErr(err));
8690
}
8791
} else {
8892
if (lg) {
@@ -105,13 +109,16 @@ export class Producer<T> {
105109
if (err1) {
106110
if (lgErr) {
107111
lgErr(formatErr(err1));
112+
return reject(formatErr(err1));
108113
}
109114
cleanup(this.mq, hConn, hObj, lgErr, lg);
110115
} else {
111116
if (lg) {
112117
lg(`MQOPEN of ${topicString} successful`);
113118
}
114-
publishMessage(hObj);
119+
if (publishMessage(hObj)) {
120+
return resolve();
121+
}
115122
}
116123
});
117124
}
@@ -203,7 +210,8 @@ export class TopicProducer<T> {
203210
// The DEV.BASE.TOPIC object defines a tree starting at dev/
204211
const qMgr = this.conf.mgr;
205212
// Define some functions that will be used from the main flow
206-
const publishMessage = (hObj: any) => {
213+
const publishMessage = (hObj: any): boolean => {
214+
let ok = true;
207215
const msg = JSON.stringify(data as any);
208216
const mqmd = new this.mq.MQMD(); // Defaults are fine.
209217
const pmo = new this.mq.MQPMO();
@@ -220,13 +228,15 @@ export class TopicProducer<T> {
220228
if (err) {
221229
if (lgErr) {
222230
lgErr(formatErr(err));
231+
ok = false;
223232
}
224233
} else {
225234
if (lg) {
226235
lg('MQPUT successful');
227236
}
228237
}
229238
});
239+
return ok;
230240
};
231241
// The program really starts here.
232242
// Connect to the queue manager. If that works, the callback function
@@ -262,13 +272,16 @@ export class TopicProducer<T> {
262272
if (err1) {
263273
if (lgErr) {
264274
lgErr(formatErr(err1));
275+
return reject(formatErr(err1));
265276
}
266277
cleanup(this.mq, hConn, hObj, lgErr, lg);
267278
} else {
268279
if (lg) {
269280
lg(`MQOPEN of ${topic} successful`);
270281
}
271-
publishMessage(hObj);
282+
if (publishMessage(hObj)) {
283+
resolve();
284+
}
272285
}
273286
});
274287
}
@@ -342,11 +355,13 @@ export class QueueProducer<T> {
342355
.then(() => {
343356
if (lg) {
344357
lg('Done.');
358+
return resolve();
345359
}
346360
})
347361
.catch((err: { message: string; }) => {
348362
if (lgErr) {
349363
lgErr(formatErr(err));
364+
return reject(formatErr(err));
350365
}
351366
cleanup(this.mq, this.ghConn, this.ghObj, lgErr, lg);
352367
});

0 commit comments

Comments
 (0)