nodejs rabbitmq使用

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
var amqp = require('amqp')
 
1. 一个发送者, 多个消费者。 exchange发送3条消息, 每个队列接收3条消息相同
 
connection.on('ready', function () {
 
    var exchange = connection.exchange('exchange_name', {
        type: 'fanout',
        autoDelete: false
    });
 
 
    connection.queue("queue_name", {
        autoDelete: false
    }, function (queue) {
 
        queue.bind('exchange_name', 'queue_name', function () {
            exchange.publish('queue_name', 'this is message is testing ......');
            exchange.publish('queue_name', 'this is message is testing3 ......')
 
            setTimeout(function () {
                console.log("Single queue bind callback succeeded");
                //exchange.destroy();
                //queue.destroy();node
                connection.end();
                connection.destroy();
            }, 5000);
 
        })
        //同一个消息多次订阅同一个队列,只会收到一次消息
        /*
        ack:true,确认收到一条消息后,后续对列中的消息接收不到
        prefetchCount:每个消费者接收的消息平均,
        */
        // queue.subscribe({ack:true,prefetchCount:1},function (message) {
        // console.log('2At 5 second recieved message is:'+ message.data);
        // });
        // queue.subscribe({ack:true,prefetchCount:1},function (message) {
        // console.log('At 5 second recieved message is:'+ message.data);
        // });
 
        queue.subscribe(function (message) {
            console.log('2At 5 second recieved message is:' + message.data);
        });
        // queue.subscribe(function (message) {
        // console.log('1 5 second recieved message is:'+ message.data);
        // });
 
    })
    connection.queue("queue_name1", {
        autoDelete: false
    }, function (queue) {
        queue.bind('exchange_name', 'queue_name1', function () {
            exchange.publish('queue_name1', 'this is queue_name1 is testing4 ......')
        })
 
        queue.subscribe(function (message) {
            console.log('queue_name1 5 second recieved message is:' + message.data);
        });
    })
})
 
打印:
 
queue_name1 5 second recieved message is: this is message is testing......
    2 At 5 second recieved message is: this is message is testing......
    2 At 5 second recieved message is: this is message is testing3......
    queue_name1 5 second recieved message is: this is message is testing3......
    2 At 5 second recieved message is: this is queue_name1 is testing4......
    queue_name1 5 second recieved message is: this is queue_name1 is testing4......
 
    2. exchange的type为direct, 每个个队列, 只能收到指定的队列消息
 
connection.on('ready', function () {
 
    var exchange = connection.exchange('exchange_name2', {
        type: 'direct',
        autoDelete: false
    });
    connection.queue("queue_name", {
        autoDelete: false
    }, function (queue) {
        queue.bind('exchange_name2', 'queue_name', function () {
            exchange.publish('queue_name', 'this is message is testing ......');
            exchange.publish('queue_name', 'this is message is testing2222 ......');
 
            setTimeout(function () {
                console.log("Single queue bind callback succeeded");
                //exchange.destroy();
                //queue.destroy();
                connection.end();
                connection.destroy();
            }, 5000)
 
        })
        queue.subscribe(function (message) {
            console.log('At 2:' + message.data);
        })
        queue.subscribe(function (message) {
            console.log('At 1:' + message.data);
        })
    })
 
    connection.queue('q2', {
        autoDelete: false
    }, function (q) {
        q.bind('exchange_name2', 'q2', function () {
            exchange.publish('q2', 'is form q2')
        })
        q.subscribe(function (msg) {
            console.log("q2: " + msg.data)
        })
    })
 
 
})
 
打印:
 
At 2: this is message is testing......
    At 1: this is message is testing2222......
    q2: is form q2
 
3. 参数尝试, echange, queue, publish持久化
 
const amqp = require('amqp');
var connection = amqp.createConnection({
    host: '127.0.0.1',
    port: 5672,
    login: 'test',
    password: '123',
    vhost: '/test'
});
connection.on('error', function (err) {
    console.log('Connection error', err);
});
connection.on('ready', function () {
    // Options
    // - type 'fanout', 'direct', or 'topic' (default)
    // - passive (boolean)
    // - durable (boolean)
    // - autoDelete (boolean, default true)
    var exchange = connection.exchange('exchange_name', {
        type: 'fanout',
        autoDelete: false
    });
    // - passive (boolean)
    // - durable (boolean)持久化
    // - exclusive (boolean)
    // - autoDelete (boolean, default true)
    //'arguments': {'x-expires': 3600000}
    connection.queue("queue_name8", {
        autoDelete: false,
        durable: true
    }, function (queue) {
        queue.bind('exchange_name', 'queue_name8', function () {
            // the third argument can specify additional options
            // - mandatory (boolean, default false)
            // - immediate (boolean, default false)
            // - contentType (default 'application/octet-stream')
            // - contentEncoding
            // - headers
            // - deliveryMode
            // - priority (0-9)
            // - correlationId
            // - replyTo
            // - expiration
            // - messageId
            // - timestamp
            // - userId
            // - appId
            // - clusterId
            //
            exchange.publish('queue_name8', 'this is message is testing ......', {
                deliveryMode: 2
            });
            exchange.publish('queue_name8', 'this is message is testing3 ......', {
                deliveryMode: 2
            })
 
            setTimeout(function () {
                console.log("Single queue bind callback succeeded");
                //exchange.destroy();
                //queue.destroy();node
                connection.end();
                connection.destroy();
            }, 5000);
 
        })
        //同一个消息多次订阅同一个队列,只会收到一次消息
        /*
        ack:true,确认收到一条消息后,后续对列中的消息接收不到,如果需要下条消息,调用q.shift()
        prefetchCount:每个消费者接收的消息平均,
        */
        // queue.subscribe({ack:true,prefetchCount:1},function (message) {
        // console.log('2At 5 second recieved message is:'+ message.data);
        // });
        // queue.subscribe({ack:true,prefetchCount:1},function (message) {
        // console.log('At 5 second recieved message is:'+ message.data);
        // });
 
        queue.subscribe(function (message) {
            console.log('2At 5 second recieved message is:' + message.data);
        });
        // queue.subscribe(function (message) {
        // console.log('1 5 second recieved message is:'+ message.data);
        // });
    })
    connection.queue("queue_name9", {
        autoDelete: false,
        durable: true
    }, function (queue) {
        queue.bind('exchange_name', 'queue_name9', function () {
            exchange.publish('queue_name9', 'this is queue_name1 is testing4 ......', {
                deliveryMode: 2
            })
        })
        queue.subscribe(function (message) {
            console.log('queue_name1 7 second recieved message is:' + message.data);
        });
    })
})
 
4. 确认收到消息, 继续接收消息
 
var queName = 'test_q3',
    ex = 'test_ex';
connection.on('ready', function () {
    var ex = connection.exchange(`${ex}`, {
        type: 'fanout',
        durable: true,
        autoDelete: false
    });
    connection.queue(`${queName}`, {
        durable: true,
        autoDelete: false
    }, function (q) {
 
        q.bind(ex, queName, function () {
            ex.publish(queName, 'test msg1', {
                deliveryMode: 2
            })
            ex.publish(queName, 'test msg2 msg2', {
                deliveryMode: 2
            })
        })
        setTimeout(function () {
            console.log("Single queue bind callback succeeded");
            //exchange.destroy();
            //queue.destroy();node
            connection.end();
            connection.destroy();
        }, 1000);
        q.subscribe({
            ack: true,
            prefetchCount: 1
        }, function (msg) {
            q.shift()
            console.log('receive msg : ' + msg.data);
        })
    })
})
 
注意: 同一个exhcange中的queuename, exhcange, type方式一旦在程序中确定, 就不能改变

  

posted @   江山一族  阅读(3321)  评论(1编辑  收藏  举报
编辑推荐:
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· 程序员常用高效实用工具推荐,办公效率提升利器!
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)
点击右上角即可分享
微信分享提示