File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ #!/usr/bin/env node
2+
3+ 'use strict' ;
4+
5+ const co = require ( 'co' ) ;
6+ const amqp = require ( 'amqplib' ) ;
7+
8+ co ( function * ( ) {
9+ // connection errors are handled in the co .catch handler
10+ const conn = yield amqp . connect ( 'amqp://localhost' ) ;
11+
12+ // try catch will throw any errors from the yielding the following promises to the co .catch handler
13+ try {
14+ const q = 'hello' ;
15+ const msg = 'Hello World!' ;
16+
17+ // use a confirm channel so we can check the message is sent OK.
18+ const channel = yield conn . createConfirmChannel ( ) ;
19+
20+ yield channel . assertQueue ( q ) ;
21+
22+ channel . sendToQueue ( q , new Buffer ( msg ) ) ;
23+
24+ // if message has been nacked, this will result in an error (rejected promise);
25+ yield channel . waitForConfirms ( ) ;
26+
27+ console . log ( " [x] Sent '%s'" , msg ) ;
28+
29+ channel . close ( ) ;
30+ }
31+ catch ( e ) {
32+ throw e ;
33+ }
34+ finally {
35+ conn . close ( ) ;
36+ }
37+
38+ } ) . catch ( err => {
39+ console . warn ( 'Error:' , err ) ;
40+ } ) ;
You can’t perform that action at this time.
0 commit comments