forked from openiap/opencore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamqp_consumer.ts
More file actions
84 lines (75 loc) · 3.63 KB
/
Copy pathamqp_consumer.ts
File metadata and controls
84 lines (75 loc) · 3.63 KB
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
import * as winston from "winston";
import * as amqplib from "amqplib";
import * as url from "url";
// tslint:disable-next-line: class-name
export class amqp_consumer {
conn: amqplib.Connection;
channel: amqplib.Channel; // channel: amqplib.ConfirmChannel;
queue: string;
private _logger: winston.Logger;
private _ok: amqplib.Replies.AssertQueue;
private connectionstring: string;
constructor(logger: winston.Logger, connectionstring: string, queue: string) {
this._logger = logger;
this.queue = queue;
this.connectionstring = connectionstring;
}
async connect(autoack: boolean, autoDelete: boolean): Promise<void> {
var me: amqp_consumer = this;
this.conn = await amqplib.connect(this.connectionstring);
this.conn.on("error", () => null);
this.channel = await this.conn.createChannel();
this._ok = await this.channel.assertQueue(this.queue, { durable: false, autoDelete: autoDelete });
await this.channel.consume(this.queue, (msg) => { this.OnMessage(me, msg); }, { noAck: autoack });
this._logger.info("Connected to " + new URL(this.connectionstring).hostname);
}
async close(): Promise<void> {
if (this.channel != null && this.channel != undefined) { await this.channel.close(); this.channel = null; }
if (this.conn != null && this.conn != undefined) { await this.conn.close(); this.conn = null; }
}
OnMessage(sender: amqp_consumer, msg: amqplib.ConsumeMessage): void {
sender._logger.info("OnMessage " + msg.content.toString());
}
sendToQueue(queue: string, correlationId: string, data: any): void {
this.sendToQueueWithReply(queue, this._ok.queue, correlationId, data);
}
sendToQueueWithReply(queue: string, replyto: string, correlationId: string, data: any): void {
if (typeof data !== 'string' && !(data instanceof String)) {
data = JSON.stringify(data);
}
this._logger.info("SendMessage " + data);
//this.channel.publish( this.exchange, "", Buffer.from(msg));
this.channel.sendToQueue(queue, Buffer.from(data), { correlationId: correlationId, replyTo: replyto });
}
}
type RPCCallback = (msg: string) => string;
// tslint:disable-next-line: class-name
export class amqp_rpc_consumer {
conn: amqplib.Connection;
channel: amqplib.Channel; // channel: amqplib.ConfirmChannel;
queue: string;
callback: RPCCallback;
private _logger: winston.Logger;
private _ok: amqplib.Replies.AssertQueue;
private connectionstring: string;
constructor(logger: winston.Logger, connectionstring: string, queue: string, callback: RPCCallback) {
this._logger = logger;
this.queue = queue;
this.callback = callback;
this.connectionstring = connectionstring;
}
async connect(): Promise<void> {
var me: amqp_rpc_consumer = this;
this.conn = await amqplib.connect(this.connectionstring);
this.channel = await this.conn.createChannel();
this._ok = await this.channel.assertQueue(this.queue, { durable: false });
await this.channel.consume(this.queue, (msg) => { this.OnMessage(me, msg); }, { noAck: false });
this._logger.info("Connected to " + new URL(this.connectionstring).hostname);
}
OnMessage(sender: amqp_rpc_consumer, msg: amqplib.ConsumeMessage): void {
sender._logger.info("OnMessage " + msg.content.toString());
var result: string = this.callback(msg.content.toString());
this.channel.sendToQueue(msg.properties.replyTo, Buffer.from(result), { correlationId: msg.properties.correlationId });
this.channel.ack(msg);
}
}