-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example send with generators version
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env node | ||
|
||
'use strict'; | ||
|
||
const co = require('co'); | ||
const amqp = require('amqplib'); | ||
|
||
co(function* () { | ||
// connection errors are handled in the co .catch handler | ||
const conn = yield amqp.connect('amqp://localhost'); | ||
|
||
// try catch will throw any errors from the yielding the following promises to the co .catch handler | ||
try { | ||
const q = 'hello'; | ||
const msg = 'Hello World!'; | ||
|
||
// use a confirm channel so we can check the message is sent OK. | ||
const channel = yield conn.createConfirmChannel(); | ||
|
||
yield channel.assertQueue(q); | ||
|
||
channel.sendToQueue(q, new Buffer(msg)); | ||
|
||
// if message has been nacked, this will result in an error (rejected promise); | ||
yield channel.waitForConfirms(); | ||
|
||
console.log(" [x] Sent '%s'", msg); | ||
|
||
channel.close(); | ||
} | ||
catch (e) { | ||
throw e; | ||
} | ||
finally { | ||
conn.close(); | ||
} | ||
|
||
}).catch(err => { | ||
console.warn('Error:', err); | ||
}); |