|
1 |
| -const q = 'tasks'; |
2 |
| - |
3 | 1 | const { execSync } = require("child_process");
|
4 |
| -const amqplib = require("amqplib"); |
| 2 | +const amqp = require("amqplib"); |
5 | 3 |
|
6 |
| -async function createConnection() { |
7 |
| - const id = Math.round(Math.random() * 1000); |
8 |
| - console.log(`* connect ${id}`); |
9 |
| - const pconn = amqplib.connect("amqp://root:[email protected]:5672/").then((conn) => { |
10 |
| - conn.on("close", (error) => { |
11 |
| - console.log(`* connection closed ${id}:`, error); |
12 |
| - }); |
13 |
| - conn.on("error", (error) => { |
14 |
| - console.log(`* connection error ${id}:`, error); |
15 |
| - }); |
16 |
| - return conn; |
| 4 | +async function sleep(delay) { |
| 5 | + return new Promise((resolve, reject) => { |
| 6 | + setTimeout(resolve, delay); |
17 | 7 | });
|
18 |
| - console.log(`* connected ${id}`); |
19 |
| - return pconn; |
20 |
| -} |
21 |
| - |
22 |
| -async function createChannel(conn) { |
23 |
| - const id = Math.round(Math.random() * 1000); |
24 |
| - console.log(`* create channel ${id}`); |
25 |
| - const channel = conn.createChannel({durable: false}); |
26 |
| - console.log(`* channel created ${id}`); |
27 |
| - return channel; |
28 | 8 | }
|
29 | 9 |
|
30 |
| -async function createConnectionAndChannel() { |
31 |
| - const conn = await createConnection(); |
32 |
| - const channel = await createChannel(conn); |
33 |
| - return channel; |
34 |
| -} |
35 |
| - |
36 |
| -async function main(channels) { |
37 |
| - |
38 |
| - if (!channels) { |
39 |
| - channels = await Promise.all([createConnectionAndChannel(), createConnectionAndChannel()]); |
| 10 | +async function createChannel(config) { |
| 11 | + const { url, publishers, listeners } = Object.assign({url: "", publishers: {}, listeners: {}}, config); |
| 12 | + try { |
| 13 | + // create connection |
| 14 | + const connection = await amqp.connect(url); |
| 15 | + let channel = null; |
| 16 | + connection._channels = []; |
| 17 | + connection.on("error", (error) => { |
| 18 | + console.error("Connection error : ", config, error); |
| 19 | + }); |
| 20 | + connection.on("close", async (error) => { |
| 21 | + if (channel) { |
| 22 | + channel.close(); |
| 23 | + } |
| 24 | + console.error("Connection close : ", config, error); |
| 25 | + await sleep(1000); |
| 26 | + createChannel(config); |
| 27 | + }); |
| 28 | + // create channel |
| 29 | + channel = await connection.createConfirmChannel(); |
| 30 | + channel.on("error", (error) => { |
| 31 | + console.error("Channel error : ", config, error); |
| 32 | + }); |
| 33 | + channel.on("close", (error) => { |
| 34 | + console.error("Channel close : ", config, error); |
| 35 | + }); |
| 36 | + // register listeners |
| 37 | + for (queue in listeners) { |
| 38 | + const callback = listeners[queue]; |
| 39 | + channel.assertQueue(queue, { durable: false }); |
| 40 | + channel.consume(queue, callback); |
| 41 | + } |
| 42 | + // publish |
| 43 | + for (queue in publishers) { |
| 44 | + const message = publishers[queue]; |
| 45 | + channel.assertQueue(queue, { durable: false }); |
| 46 | + channel.sendToQueue(queue, message); |
| 47 | + } |
| 48 | + return channel; |
| 49 | + } catch (error) { |
| 50 | + console.error("Create connection error : ", error); |
| 51 | + await sleep(1000); |
| 52 | + createChannel(config); |
40 | 53 | }
|
41 |
| - const [publisherChannel, consumerChannel] = channels; |
| 54 | +} |
42 | 55 |
|
43 |
| - // publisher |
44 |
| - console.log("publish"); |
45 |
| - await publisherChannel.assertQueue(q).then(function(ok) { |
46 |
| - console.log("published"); |
47 |
| - return publisherChannel.sendToQueue(q, Buffer.from("something to do")); |
| 56 | +async function main() { |
| 57 | + const channelPublish = await createChannel({ |
| 58 | + url: "amqp://root:[email protected]:5672", |
| 59 | + publishers: { |
| 60 | + "queue": Buffer.from("hello"), |
| 61 | + } |
48 | 62 | });
|
49 | 63 |
|
50 |
| - // consumer |
51 |
| - console.log("consume"); |
52 |
| - await consumerChannel.assertQueue(q).then(function(ok) { |
53 |
| - console.log("consumed"); |
54 |
| - return consumerChannel.consume(q, function(msg) { |
55 |
| - if (msg !== null) { |
56 |
| - console.log(msg.content.toString()); |
57 |
| - consumerChannel.ack(msg); |
58 |
| - } |
59 |
| - }); |
60 |
| - }); |
| 64 | + execSync("docker stop rabbitmq"); |
| 65 | + execSync("docker start rabbitmq"); |
61 | 66 |
|
62 |
| - await new Promise((resolve, reject) => { |
63 |
| - setTimeout(resolve, 1000); |
| 67 | + const channelConsume = await createChannel({ |
| 68 | + url: "amqp://root:[email protected]:5672", |
| 69 | + listeners: { |
| 70 | + "queue": (message) => { |
| 71 | + console.log("Receive message ", message.content.toString()); |
| 72 | + }, |
| 73 | + } |
64 | 74 | });
|
65 | 75 |
|
66 |
| - await main(channels); |
| 76 | + return true; |
67 | 77 | }
|
68 | 78 |
|
69 |
| -main(); |
| 79 | +main().catch((error) => console.error(error)); |
0 commit comments