Skip to content

Commit 1b91714

Browse files
committedDec 10, 2022
Update other examples
1 parent 4f9b013 commit 1b91714

File tree

5 files changed

+65
-60
lines changed

5 files changed

+65
-60
lines changed
 

‎examples/headers.js

+29-29
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,41 @@
22

33
// Example of using a headers exchange
44

5-
var amqp = require('../')
6-
7-
amqp.connect().then(function(conn) {
8-
return conn.createChannel().then(withChannel);
9-
}, console.error);
10-
11-
function withChannel(ch) {
12-
// NB the type of the exchange is 'headers'
13-
ch.assertExchange('matching exchange', 'headers').then(function(ex) {
14-
ch.assertQueue().then(function(q) {
15-
bindAndConsume(ch, ex, q).then(function() {
16-
send(ch, ex);
17-
});
18-
});
5+
const amqp = require('../');
6+
7+
(async () => {
8+
9+
const connection = await amqp.connect();
10+
const channel = await connection.createChannel();
11+
12+
process.once('SIGINT', async () => {
13+
await channel.close();
14+
await connection.close();
1915
});
20-
}
2116

22-
function bindAndConsume(ch, ex, q) {
17+
const { exchange } = await channel.assertExchange('matching exchange', 'headers');
18+
const { queue } = await channel.assertQueue();
19+
2320
// When using a headers exchange, the headers to be matched go in
2421
// the binding arguments. The routing key is ignore, so best left
2522
// empty.
2623

2724
// 'x-match' is 'all' or 'any', meaning "all fields must match" or
2825
// "at least one field must match", respectively. The values to be
2926
// matched go in subsequent fields.
30-
ch.bindQueue(q.queue, ex.exchange, '', {'x-match': 'any',
31-
'foo': 'bar',
32-
'baz': 'boo'});
33-
return ch.consume(q.queue, function(msg) {
34-
console.log(msg.content.toString());
35-
}, {noAck: true});
36-
}
37-
38-
function send(ch, ex) {
39-
// The headers for a message are given as an option to `publish`:
40-
ch.publish(ex.exchange, '', Buffer.from('hello'), {headers: {baz: 'boo'}});
41-
ch.publish(ex.exchange, '', Buffer.from('world'), {headers: {foo: 'bar'}});
42-
}
27+
await channel.bindQueue(queue, exchange, '', {
28+
'x-match': 'any',
29+
'foo': 'bar',
30+
'baz': 'boo'
31+
});
32+
33+
await channel.consume(queue, (message) => {
34+
console.log(message.content.toString());
35+
}, { noAck: true });
36+
37+
channel.publish(exchange, '', Buffer.from('hello'), { headers: { baz: 'boo' }});
38+
channel.publish(exchange, '', Buffer.from('hello'), { headers: { foo: 'bar' }});
39+
channel.publish(exchange, '', Buffer.from('lost'), { headers: { meh: 'nah' }});
40+
41+
console.log(' [x] To exit press CTRL+C.');
42+
})();

‎examples/ssl.js

+16-10
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
//
1919
// openssl s_client -connect localhost:5671
2020

21-
var amqp = require('../');
22-
var fs = require('fs');
21+
const amqp = require('../');
22+
const fs = require('fs');
2323

2424
// Assemble the SSL options; for verification we need at least
2525
// * a certificate to present to the server ('cert', in PEM format)
@@ -34,7 +34,7 @@ var fs = require('fs');
3434
// to use `rejectUnauthorized: false`.
3535

3636
// Options for full client and server verification:
37-
var opts = {
37+
const opts = {
3838
cert: fs.readFileSync('../etc/client/cert.pem'),
3939
key: fs.readFileSync('../etc/client/key.pem'),
4040
// cert and key or
@@ -49,16 +49,22 @@ var opts = {
4949
// {verify, verify_none},
5050
// {fail_if_no_peer_cert,false}
5151
//
52-
// var opts = { ca: [fs.readFileSync('../etc/testca/cacert.pem')] };
52+
// const opts = { ca: [fs.readFileSync('../etc/testca/cacert.pem')] };
5353

5454
// Option to use the SSL client certificate for authentication
5555
// opts.credentials = amqp.credentials.external();
5656

57-
var open = amqp.connect('amqps://localhost', opts);
57+
(async () => {
58+
const connection = await amqp.connect('amqp://localhost', opts);
59+
const channel = await connection.createChannel();
5860

59-
open.then(function(conn) {
60-
process.on('SIGINT', conn.close.bind(conn));
61-
return conn.createChannel().then(function(ch) {
62-
ch.sendToQueue('foo', Buffer.from('Hello World!'));
61+
process.on('SIGINT', async () => {
62+
await channel.close();
63+
await connection.close();
6364
});
64-
}).then(null, console.warn);
65+
66+
channel.sendToQueue('foo', Buffer.from('Hello World!'));
67+
68+
console.log(' [x] To exit press CTRL+C.');
69+
})();
70+

‎examples/tutorials/callback_api/rpc_server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ amqp.connect((err, connection) => {
2828
channel.ack(message);
2929
}, { noAck: false }, function(err) {
3030
if (err) return bail(err, conn);
31-
console.log(' [x] Awaiting RPC requests');
31+
console.log(' [x] Awaiting RPC requests. To exit press CTRL+C.');
3232
});
3333
});
3434
});

‎examples/tutorials/rpc_server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const queue = 'rpc_queue';
2727
channel.ack(message);
2828
});
2929

30-
console.log(' [x] Awaiting RPC requests');
30+
console.log(' [x] Awaiting RPC requests. To exit press CTRL+C.');
3131
}
3232
catch (err) {
3333
console.warn(err);

‎examples/waitForConfirms.js

+18-19
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
var amqp = require('../');
1+
const amqp = require('../');
22

3-
var NUM_MSGS = 20;
3+
(async () => {
4+
let connection;
5+
try {
6+
connection = await amqp.connect();
7+
const channel = await connection.createConfirmChannel();
48

5-
function mkCallback(i) {
6-
return (i % 2) === 0 ? function(err) {
7-
if (err !== null) { console.error('Message %d failed!', i); }
8-
else { console.log('Message %d confirmed', i); }
9-
} : null;
10-
}
9+
for (var i=0; i < 20; i++) {
10+
channel.publish('amq.topic', 'whatever', Buffer.from('blah'));
11+
};
1112

12-
amqp.connect().then(function(c) {
13-
c.createConfirmChannel().then(function(ch) {
14-
for (var i=0; i < NUM_MSGS; i++) {
15-
ch.publish('amq.topic', 'whatever', Buffer.from('blah'), {}, mkCallback(i));
16-
}
17-
ch.waitForConfirms().then(function() {
18-
console.log('All messages done');
19-
c.close();
20-
}).catch(console.error);
21-
});
22-
});
13+
await channel.waitForConfirms();
14+
console.log('All messages done');
15+
await channel.close();
16+
} catch (err) {
17+
console.warn(err);
18+
} finally {
19+
if (connection) await connection.close();
20+
}
21+
})();

0 commit comments

Comments
 (0)
Please sign in to comment.