primer cambio
This commit is contained in:
93
node_modules/amqplib/examples/tutorials/README.md
generated
vendored
Normal file
93
node_modules/amqplib/examples/tutorials/README.md
generated
vendored
Normal file
@@ -0,0 +1,93 @@
|
||||
# RabbitMQ tutorials
|
||||
|
||||
This directory contains the [RabbitMQ tutorials][rabbitmq-tutes],
|
||||
ported to amqplib. The sub-directory `callback_api` has translations
|
||||
of the tutorial programs to the callback-oriented API.
|
||||
|
||||
## Preparation
|
||||
|
||||
To run the tutorial code, you need amqplib installed. Assuming you are
|
||||
in a clone of the amqplib repository, from the tutorials directory:
|
||||
|
||||
npm install
|
||||
|
||||
or to use the latest released version,
|
||||
|
||||
npm install amqplib
|
||||
|
||||
Then just run each file as a script, e.g., in bash
|
||||
|
||||
./send.js
|
||||
|
||||
or
|
||||
|
||||
node send.js
|
||||
|
||||
or
|
||||
|
||||
nave use 0.8 node send.js
|
||||
|
||||
## [Tutorial one: Hello World!][tute-one]
|
||||
|
||||
A "Hello World" example, with one script sending a message to a queue,
|
||||
and another receiving messages from the same queue.
|
||||
|
||||
* [send.js](send.js)
|
||||
* [receive.js](receive.js)
|
||||
|
||||
## [Tutorial two: Work queues][tute-two]
|
||||
|
||||
Using RabbitMQ as a work queue; `new_task` creates a task, and
|
||||
`worker` processes tasks. Multiple `worker` process will share the
|
||||
tasks among them. Long-running tasks are simulated by supplying a
|
||||
string with dots, e.g., '...' to `new_task`. Each dot makes the worker
|
||||
"work" for a second.
|
||||
|
||||
* [new_task.js](new_task.js)
|
||||
* [worker.js](worker.js)
|
||||
|
||||
## [Tutorial three: Publish/Subscribe][tute-three]
|
||||
|
||||
Using RabbitMQ as a broadcast mechanism. `emit_log` sends a "log"
|
||||
message to a fanout exchange, and all `receive_logs` processes receive
|
||||
log messages.
|
||||
|
||||
* [emit_log.js](emit_log.js)
|
||||
* [receive_logs.js](receive_logs.js)
|
||||
|
||||
## [Tutorial four: Routing][tute-four]
|
||||
|
||||
Using RabbitMQ as a routing ('somecast') mechanism. `emit_log_direct`
|
||||
sends a log message with a severity, and all `receive_logs_direct`
|
||||
processes receive log messages for the severities on which they are
|
||||
listening.
|
||||
|
||||
* [emit_log_direct.js](emit_log_direct.js)
|
||||
* [receive_logs_direct.js](receive_logs_direct.js)
|
||||
|
||||
## [Tutorial five: Topics][tute-five]
|
||||
|
||||
Extends the previous tutorial to routing with wildcarded patterns.
|
||||
|
||||
* [emit_log_topic.js](emit_log_topic.js)
|
||||
* [receive_logs_topic.js](receive_logs_topic.js)
|
||||
|
||||
## [Tutorial six: RPC][tute-six]
|
||||
|
||||
Using RabbitMQ as an RPC intermediary, queueing requests for servers
|
||||
and routing replies back to clients.
|
||||
|
||||
* [rpc_server.js](rpc_server.js)
|
||||
* [rpc_client.js](rpc_client.js)
|
||||
|
||||
I depart slightly from the original tutorial code, which I think has
|
||||
some needless object-orientation (in the Python code; you don't get a
|
||||
choice about needless object-orientation in Java).
|
||||
|
||||
[rabbitmq-tutes]: http://github.com/rabbitmq/rabbitmq-tutorials
|
||||
[tute-one]: http://www.rabbitmq.com/tutorials/tutorial-one-javascript.html
|
||||
[tute-two]: http://www.rabbitmq.com/tutorials/tutorial-two-javascript.html
|
||||
[tute-three]: http://www.rabbitmq.com/tutorials/tutorial-three-javascript.html
|
||||
[tute-four]: http://www.rabbitmq.com/tutorials/tutorial-four-javascript.html
|
||||
[tute-five]: http://www.rabbitmq.com/tutorials/tutorial-five-javascript.html
|
||||
[tute-six]: http://www.rabbitmq.com/tutorials/tutorial-six-javascript.html
|
||||
28
node_modules/amqplib/examples/tutorials/callback_api/emit_log.js
generated
vendored
Normal file
28
node_modules/amqplib/examples/tutorials/callback_api/emit_log.js
generated
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('amqplib/callback_api');
|
||||
|
||||
const exchange = 'logs';
|
||||
const text = process.argv.slice(2).join(' ') || 'info: Hello World!';
|
||||
|
||||
amqp.connect((err, connection) => {
|
||||
if (err) return bail(err);
|
||||
connection.createChannel((err, channel) => {
|
||||
if (err) return bail(err, connection);
|
||||
channel.assertExchange(exchange, 'fanout', { durable: false }, (err) => {
|
||||
if (err) return bail(err, connection);
|
||||
channel.publish(exchange, '', Buffer.from(text));
|
||||
console.log(" [x] Sent '%s'", text);
|
||||
channel.close(() => {
|
||||
connection.close();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function bail(err, connection) {
|
||||
console.error(err);
|
||||
if (connection) connection.close(() => {
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
30
node_modules/amqplib/examples/tutorials/callback_api/emit_log_direct.js
generated
vendored
Normal file
30
node_modules/amqplib/examples/tutorials/callback_api/emit_log_direct.js
generated
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('amqplib/callback_api');
|
||||
|
||||
const exchange = 'direct_logs';
|
||||
const args = process.argv.slice(2);
|
||||
const routingKey = (args.length > 0) ? args[0] : 'info';
|
||||
const text = args.slice(1).join(' ') || 'Hello World!';
|
||||
|
||||
amqp.connect((err, connection) => {
|
||||
if (err) return bail(err);
|
||||
connection.createChannel((err, channel) => {
|
||||
if (err) return bail(err, connection);
|
||||
channel.assertExchange(exchange, 'direct', { durable: false }, (err) => {
|
||||
if (err) return bail(err, connection);
|
||||
channel.publish(exchange, routingKey, Buffer.from(text));
|
||||
console.log(" [x] Sent '%s'", text);
|
||||
channel.close(() => {
|
||||
connection.close();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function bail(err, connection) {
|
||||
console.error(err);
|
||||
if (connection) connection.close(() => {
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
30
node_modules/amqplib/examples/tutorials/callback_api/emit_log_topic.js
generated
vendored
Normal file
30
node_modules/amqplib/examples/tutorials/callback_api/emit_log_topic.js
generated
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('amqplib/callback_api');
|
||||
|
||||
const exchange = 'topic_logs';
|
||||
const args = process.argv.slice(2);
|
||||
const routingKey = (args.length > 0) ? args[0] : 'info';
|
||||
const text = args.slice(1).join(' ') || 'Hello World!';
|
||||
|
||||
amqp.connect((err, connection) => {
|
||||
if (err) return bail(err);
|
||||
connection.createChannel((err, channel) => {
|
||||
if (err) return bail(err, connection);
|
||||
channel.assertExchange(exchange, 'topic', { durable: false }, (err) => {
|
||||
if (err) return bail(err, connection);
|
||||
channel.publish(exchange, routingKey, Buffer.from(text));
|
||||
console.log(" [x] Sent '%s'", text);
|
||||
channel.close(() => {
|
||||
connection.close();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function bail(err, connection) {
|
||||
console.error(err);
|
||||
if (connection) connection.close(() => {
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
28
node_modules/amqplib/examples/tutorials/callback_api/new_task.js
generated
vendored
Normal file
28
node_modules/amqplib/examples/tutorials/callback_api/new_task.js
generated
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('amqplib/callback_api');
|
||||
|
||||
const queue = 'task_queue';
|
||||
const text = process.argv.slice(2).join(' ') || "Hello World!";
|
||||
|
||||
amqp.connect((err, connection) => {
|
||||
if (err) return bail(err);
|
||||
connection.createChannel((err, channel) => {
|
||||
if (err) return bail(err, connection);
|
||||
channel.assertQueue(queue, { durable: true }, (err) => {
|
||||
if (err) return bails(err, connection);
|
||||
channel.sendToQueue(queue, Buffer.from(text), { persistent: true });
|
||||
console.log(" [x] Sent '%s'", text);
|
||||
channel.close(() => {
|
||||
connection.close();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function bail(err, connection) {
|
||||
console.error(err);
|
||||
if (connection) connection.close(() => {
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
36
node_modules/amqplib/examples/tutorials/callback_api/receive.js
generated
vendored
Normal file
36
node_modules/amqplib/examples/tutorials/callback_api/receive.js
generated
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('amqplib/callback_api');
|
||||
|
||||
const queue = 'hello';
|
||||
|
||||
amqp.connect((err, connection) => {
|
||||
if (err) return bail(err);
|
||||
connection.createChannel((err, channel) => {
|
||||
if (err) return bail(err, connection);
|
||||
|
||||
process.once('SIGINT', () => {
|
||||
channel.close(() => {
|
||||
connection.close();
|
||||
});
|
||||
});
|
||||
|
||||
channel.assertQueue(queue, { durable: false }, (err) => {
|
||||
if (err) return bail(err, connection);
|
||||
channel.consume(queue, (message) => {
|
||||
if (message) console.log(" [x] Received '%s'", message.content.toString());
|
||||
else console.warn(' [x] Consumer cancelled');
|
||||
}, { noAck: true }, (err) => {
|
||||
if (err) return bail(err, connection);
|
||||
console.log(" [*] Waiting for logs. To exit press CTRL+C.");
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function bail(err, connection) {
|
||||
console.error(err);
|
||||
if (connection) connection.close(() => {
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
42
node_modules/amqplib/examples/tutorials/callback_api/receive_logs.js
generated
vendored
Normal file
42
node_modules/amqplib/examples/tutorials/callback_api/receive_logs.js
generated
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('amqplib/callback_api');
|
||||
|
||||
const exchange = 'logs';
|
||||
|
||||
amqp.connect((err, connection) => {
|
||||
if (err) return bail(err);
|
||||
connection.createChannel((err, channel) => {
|
||||
if (err) return bail(err, connection);
|
||||
|
||||
process.once('SIGINT', () => {
|
||||
channel.close(() => {
|
||||
connection.close();
|
||||
});
|
||||
});
|
||||
|
||||
channel.assertExchange(exchange, 'fanout', { durable: false }, (err, { queue }) => {
|
||||
if (err) return bail(err, connection);
|
||||
channel.assertQueue('', { exclusive: true }, (err, { queue }) => {
|
||||
if (err) return bail(err, connection);
|
||||
channel.bindQueue(queue, exchange, '', {}, (err) => {
|
||||
if (err) return bail(err, connection);
|
||||
channel.consume(queue, (message) => {
|
||||
if (message) console.log(" [x] '%s'", message.content.toString());
|
||||
else console.warn(' [x] Consumer cancelled');
|
||||
}, { noAck: true }, (err) => {
|
||||
if (err) return bail(err, connection);
|
||||
console.log(" [*] Waiting for logs. To exit press CTRL+C.");
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function bail(err, connection) {
|
||||
console.error(err);
|
||||
if (connection) connection.close(() => {
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
57
node_modules/amqplib/examples/tutorials/callback_api/receive_logs_direct.js
generated
vendored
Normal file
57
node_modules/amqplib/examples/tutorials/callback_api/receive_logs_direct.js
generated
vendored
Normal file
@@ -0,0 +1,57 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('amqplib/callback_api');
|
||||
const { basename } = require('path');
|
||||
|
||||
const exchange = 'direct_logs';
|
||||
const severities = process.argv.slice(2);
|
||||
if (severities.length < 1) {
|
||||
console.log('Usage %s [info] [warning] [error]', basename(process.argv[1]));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
amqp.connect((err, connection) => {
|
||||
if (err) return bail(err);
|
||||
connection.createChannel((err, channel) => {
|
||||
if (err) return bail(err, connection);
|
||||
|
||||
process.once('SIGINT', () => {
|
||||
channel.close(() => {
|
||||
connection.close();
|
||||
});
|
||||
});
|
||||
|
||||
channel.assertExchange(exchange, 'direct', { durable: false }, (err) => {
|
||||
if (err) return bail(err, connection);
|
||||
channel.assertQueue('', { exclusive: true }, (err, { queue }) => {
|
||||
if (err) return bail(err, connection);
|
||||
channel.consume(queue, (message) => {
|
||||
if (message) console.log(" [x] %s:'%s'", message.fields.routingKey, message.content.toString());
|
||||
else console.warn(' [x] Consumer cancelled');
|
||||
}, {noAck: true}, function(err) {
|
||||
if (err) return bail(err, connection);
|
||||
console.log(' [*] Waiting for logs. To exit press CTRL+C.');
|
||||
subscribeAll(channel, queue, severities, (err) => {
|
||||
if (err) return bail(err, connection);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function subscribeAll(channel, queue, bindingKeys, cb) {
|
||||
if (bindingKeys.length === 0) return cb();
|
||||
const bindingKey = bindingKeys.shift();
|
||||
channel.bindQueue(queue, exchange, bindingKey, {}, (err) => {
|
||||
if (err) return cb(err);
|
||||
subscribeAll(channel, queue, bindingKeys, cb);
|
||||
});
|
||||
}
|
||||
|
||||
function bail(err, connection) {
|
||||
console.error(err);
|
||||
if (connection) connection.close(() => {
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
58
node_modules/amqplib/examples/tutorials/callback_api/receive_logs_topic.js
generated
vendored
Normal file
58
node_modules/amqplib/examples/tutorials/callback_api/receive_logs_topic.js
generated
vendored
Normal file
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('amqplib/callback_api');
|
||||
const { basename } = require('path');
|
||||
|
||||
const exchange = 'topic_logs';
|
||||
const severities = process.argv.slice(2);
|
||||
if (severities.length < 1) {
|
||||
console.log('Usage %s [info] [warning] [error]', basename(process.argv[1]));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
amqp.connect((err, connection) => {
|
||||
if (err) return bail(err);
|
||||
connection.createChannel((err, channel) => {
|
||||
if (err) return bail(err, connection);
|
||||
|
||||
process.once('SIGINT', () => {
|
||||
channel.close(() => {
|
||||
connection.close();
|
||||
});
|
||||
});
|
||||
|
||||
channel.assertExchange(exchange, 'topic', { durable: false }, (err) => {
|
||||
if (err) return bail(err, connection);
|
||||
channel.assertQueue('', { exclusive: true }, (err, { queue }) => {
|
||||
if (err) return bail(err, connection);
|
||||
channel.consume(queue, (message) => {
|
||||
if (message) console.log(" [x] %s:'%s'", message.fields.routingKey, message.content.toString());
|
||||
else console.warn(' [x] Consumer cancelled');
|
||||
}, {noAck: true}, function(err) {
|
||||
if (err) return bail(err, connection);
|
||||
console.log(' [*] Waiting for logs. To exit press CTRL+C.');
|
||||
subscribeAll(channel, queue, severities, (err) => {
|
||||
if (err) return bail(err, connection);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function subscribeAll(channel, queue, bindingKeys, cb) {
|
||||
if (bindingKeys.length === 0) return cb();
|
||||
const bindingKey = bindingKeys.shift();
|
||||
channel.bindQueue(queue, exchange, bindingKey, {}, (err) => {
|
||||
if (err) return cb(err);
|
||||
subscribeAll(channel, queue, bindingKeys, cb);
|
||||
});
|
||||
}
|
||||
|
||||
function bail(err, connection) {
|
||||
console.error(err);
|
||||
if (connection) connection.close(() => {
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
51
node_modules/amqplib/examples/tutorials/callback_api/rpc_client.js
generated
vendored
Normal file
51
node_modules/amqplib/examples/tutorials/callback_api/rpc_client.js
generated
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('amqplib/callback_api');
|
||||
const { basename } = require('path');
|
||||
const { v4: uuid } = require('uuid');
|
||||
|
||||
const queue = 'rpc_queue';
|
||||
|
||||
const n = parseInt(process.argv[2], 10);
|
||||
if (isNaN(n)) {
|
||||
console.warn('Usage: %s number', basename(process.argv[1]));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
amqp.connect((err, connection) => {
|
||||
if (err) return bail(err);
|
||||
connection.createChannel((err, channel) => {
|
||||
if (err) return bail(err, connection);
|
||||
channel.assertQueue('', { exclusive: true }, (err, { queue: replyTo }) => {
|
||||
if (err) return bail(err, connection);
|
||||
|
||||
const correlationId = uuid();
|
||||
channel.consume(replyTo, (message) => {
|
||||
if (!message) console.warn(' [x] Consumer cancelled');
|
||||
else if (message.properties.correlationId === correlationId) {
|
||||
console.log(' [.] Got %d', message.content.toString());
|
||||
channel.close(() => {
|
||||
connection.close();
|
||||
})
|
||||
}
|
||||
}, { noAck: true });
|
||||
|
||||
channel.assertQueue(queue, { durable: false }, (err) => {
|
||||
if (err) return bail(err, connection);
|
||||
console.log(' [x] Requesting fib(%d)', n);
|
||||
channel.sendToQueue(queue, Buffer.from(n.toString()), {
|
||||
correlationId,
|
||||
replyTo
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function bail(err, connection) {
|
||||
console.error(err);
|
||||
if (connection) connection.close(() => {
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
55
node_modules/amqplib/examples/tutorials/callback_api/rpc_server.js
generated
vendored
Normal file
55
node_modules/amqplib/examples/tutorials/callback_api/rpc_server.js
generated
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('amqplib/callback_api');
|
||||
|
||||
const queue = 'rpc_queue';
|
||||
|
||||
amqp.connect((err, connection) => {
|
||||
if (err) return bail(err);
|
||||
connection.createChannel((err, channel) => {
|
||||
if (err) return bail(err, connection);
|
||||
|
||||
process.once('SIGINT', () => {
|
||||
channel.close(() => {
|
||||
connection.close();
|
||||
});
|
||||
});
|
||||
|
||||
channel.assertQueue(queue, { durable: false }, (err) => {
|
||||
if (err) return bail(err, connection);
|
||||
channel.prefetch(1);
|
||||
channel.consume(queue, (message) => {
|
||||
const n = parseInt(message.content.toString(), 10);
|
||||
console.log(' [.] fib(%d)', n);
|
||||
const response = fib(n);
|
||||
channel.sendToQueue(message.properties.replyTo, Buffer.from(response.toString()), {
|
||||
correlationId: message.properties.correlationId
|
||||
});
|
||||
channel.ack(message);
|
||||
}, { noAck: false }, function(err) {
|
||||
if (err) return bail(err, conn);
|
||||
console.log(' [x] Awaiting RPC requests. To exit press CTRL+C.');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function fib(n) {
|
||||
// Do it the ridiculous, but not most ridiculous, way. For better,
|
||||
// see http://nayuki.eigenstate.org/page/fast-fibonacci-algorithms
|
||||
let a = 0, b = 1;
|
||||
for (let i=0; i < n; i++) {
|
||||
let c = a + b;
|
||||
a = b; b = c;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
|
||||
function bail(err, connection) {
|
||||
console.error(err);
|
||||
if (connection) connection.close(() => {
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
28
node_modules/amqplib/examples/tutorials/callback_api/send.js
generated
vendored
Normal file
28
node_modules/amqplib/examples/tutorials/callback_api/send.js
generated
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('amqplib/callback_api');
|
||||
|
||||
const queue = 'hello';
|
||||
const text = 'Hello World!';
|
||||
|
||||
amqp.connect((err, connection) => {
|
||||
if (err) return bail(err);
|
||||
connection.createChannel((err, channel) => {
|
||||
if (err) return bail(err, connection);
|
||||
channel.assertQueue(queue, { durable: false }, (err) => {
|
||||
if (err) return bail(err, connection);
|
||||
channel.sendToQueue(queue, Buffer.from(text));
|
||||
console.log(" [x] Sent '%s'", text);
|
||||
channel.close(() => {
|
||||
connection.close();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function bail(err, connection) {
|
||||
console.error(err);
|
||||
if (connection) connection.close(() => {
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
39
node_modules/amqplib/examples/tutorials/callback_api/worker.js
generated
vendored
Normal file
39
node_modules/amqplib/examples/tutorials/callback_api/worker.js
generated
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('amqplib/callback_api');
|
||||
|
||||
const queue = 'task_queue';
|
||||
|
||||
amqp.connect((err, connection) => {
|
||||
if (err) return bail(err);
|
||||
connection.createChannel((err, channel) => {
|
||||
if (err) return bail(err, connection);
|
||||
|
||||
process.once('SIGINT', () => {
|
||||
channel.close(() => {
|
||||
connection.close();
|
||||
});
|
||||
});
|
||||
|
||||
channel.assertQueue(queue, { durable: true }, (err, { queue }) => {
|
||||
if (err) return bail(err, connection);
|
||||
channel.consume(queue, (message) => {
|
||||
const text = message.content.toString();
|
||||
console.log(" [x] Received '%s'", text);
|
||||
const seconds = text.split('.').length - 1;
|
||||
setTimeout(() => {
|
||||
console.log(" [x] Done");
|
||||
channel.ack(message);
|
||||
}, seconds * 1000);
|
||||
}, { noAck: false });
|
||||
console.log(" [*] Waiting for messages. To exit press CTRL+C");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function bail(err, connection) {
|
||||
console.error(err);
|
||||
if (connection) connection.close(() => {
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
24
node_modules/amqplib/examples/tutorials/emit_log.js
generated
vendored
Normal file
24
node_modules/amqplib/examples/tutorials/emit_log.js
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('amqplib');
|
||||
|
||||
const exchange = 'logs';
|
||||
const text = process.argv.slice(2).join(' ') || 'info: Hello World!';
|
||||
|
||||
(async () => {
|
||||
let connection;
|
||||
try {
|
||||
connection = await amqp.connect('amqp://localhost');
|
||||
const channel = await connection.createChannel();
|
||||
await channel.assertExchange(exchange, 'fanout', { durable: false });
|
||||
channel.publish(exchange, '', Buffer.from(text));
|
||||
console.log(" [x] Sent '%s'", text);
|
||||
await channel.close();
|
||||
}
|
||||
catch (err) {
|
||||
console.warn(err);
|
||||
}
|
||||
finally {
|
||||
if (connection) await connection.close();
|
||||
};
|
||||
})();
|
||||
26
node_modules/amqplib/examples/tutorials/emit_log_direct.js
generated
vendored
Normal file
26
node_modules/amqplib/examples/tutorials/emit_log_direct.js
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('amqplib');
|
||||
|
||||
const exchange = 'direct_logs';
|
||||
const args = process.argv.slice(2);
|
||||
const routingKey = (args.length > 0) ? args[0] : 'info';
|
||||
const text = args.slice(1).join(' ') || 'Hello World!';
|
||||
|
||||
(async () => {
|
||||
let connection;
|
||||
try {
|
||||
connection = await amqp.connect('amqp://localhost');
|
||||
const channel = await connection.createChannel();
|
||||
await channel.assertExchange(exchange, 'direct', { durable: false });
|
||||
channel.publish(exchange, routingKey, Buffer.from(text));
|
||||
console.log(" [x] Sent %s:'%s'", routingKey, text);
|
||||
await channel.close();
|
||||
}
|
||||
catch (err) {
|
||||
console.warn(err);
|
||||
}
|
||||
finally {
|
||||
if (connection) await connection.close();
|
||||
};
|
||||
})();
|
||||
26
node_modules/amqplib/examples/tutorials/emit_log_topic.js
generated
vendored
Normal file
26
node_modules/amqplib/examples/tutorials/emit_log_topic.js
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('amqplib');
|
||||
|
||||
const exchange = 'topic_logs';
|
||||
const args = process.argv.slice(2);
|
||||
const routingKeys = (args.length > 0) ? args[0] : 'info';
|
||||
const text = args.slice(1).join(' ') || 'Hello World!';
|
||||
|
||||
(async () => {
|
||||
let connection;
|
||||
try {
|
||||
connection = await amqp.connect('amqp://localhost');
|
||||
const channel = await connection.createChannel();
|
||||
await channel.assertExchange(exchange, 'topic', { durable: false });
|
||||
channel.publish(exchange, routingKeys, Buffer.from(text));
|
||||
console.log(" [x] Sent %s:'%s'", routingKeys, text);
|
||||
await channel.close();
|
||||
}
|
||||
catch (err) {
|
||||
console.warn(err);
|
||||
}
|
||||
finally {
|
||||
if (connection) await connection.close();
|
||||
};
|
||||
})();
|
||||
25
node_modules/amqplib/examples/tutorials/new_task.js
generated
vendored
Normal file
25
node_modules/amqplib/examples/tutorials/new_task.js
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env node
|
||||
// Post a new task to the work queue
|
||||
|
||||
const amqp = require('amqplib');
|
||||
|
||||
const queue = 'task_queue';
|
||||
const text = process.argv.slice(2).join(' ') || "Hello World!";
|
||||
|
||||
(async () => {
|
||||
let connection;
|
||||
try {
|
||||
connection = await amqp.connect('amqp://localhost');
|
||||
const channel = await connection.createChannel();
|
||||
await channel.assertQueue(queue, { durable: true });
|
||||
channel.sendToQueue(queue, Buffer.from(text), { persistent: true });
|
||||
console.log(" [x] Sent '%s'", text);
|
||||
await channel.close();
|
||||
}
|
||||
catch (err) {
|
||||
console.warn(err);
|
||||
}
|
||||
finally {
|
||||
await connection.close();
|
||||
};
|
||||
})();
|
||||
16
node_modules/amqplib/examples/tutorials/package.json
generated
vendored
Normal file
16
node_modules/amqplib/examples/tutorials/package.json
generated
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "amqplib-tutorials",
|
||||
"version": "0.0.1",
|
||||
"description": "The RabbitMQ tutorials, ported to amqplib",
|
||||
"main": "send.js",
|
||||
"dependencies": {
|
||||
"amqplib": "../..",
|
||||
"uuid": "*"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": "",
|
||||
"author": "Michael Bridgen <mikeb@squaremobius.net>",
|
||||
"license": "MPL 2.0"
|
||||
}
|
||||
26
node_modules/amqplib/examples/tutorials/receive.js
generated
vendored
Normal file
26
node_modules/amqplib/examples/tutorials/receive.js
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('amqplib');
|
||||
|
||||
const queue = 'hello';
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const connection = await amqp.connect('amqp://localhost');
|
||||
const channel = await connection.createChannel();
|
||||
|
||||
process.once('SIGINT', async () => {
|
||||
await channel.close();
|
||||
await connection.close();
|
||||
});
|
||||
|
||||
await channel.assertQueue(queue, { durable: false });
|
||||
await channel.consume(queue, (message) => {
|
||||
console.log(" [x] Received '%s'", message.content.toString());
|
||||
}, { noAck: true });
|
||||
|
||||
console.log(' [*] Waiting for messages. To exit press CTRL+C');
|
||||
} catch (err) {
|
||||
console.warn(err);
|
||||
}
|
||||
})();
|
||||
30
node_modules/amqplib/examples/tutorials/receive_logs.js
generated
vendored
Normal file
30
node_modules/amqplib/examples/tutorials/receive_logs.js
generated
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('amqplib');
|
||||
|
||||
const exchange = 'logs';
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const connection = await amqp.connect('amqp://localhost');
|
||||
const channel = await connection.createChannel();
|
||||
|
||||
process.once('SIGINT', async () => {
|
||||
await channel.close();
|
||||
await connection.close();
|
||||
});
|
||||
|
||||
await channel.assertExchange(exchange, 'fanout', { durable: false });
|
||||
const { queue } = await channel.assertQueue('', { exclusive: true });
|
||||
await channel.bindQueue(queue, exchange, '')
|
||||
|
||||
await channel.consume(queue, (message) => {
|
||||
if (message) console.log(" [x] '%s'", message.content.toString());
|
||||
else console.warn(' [x] Consumer cancelled');
|
||||
}, { noAck: true });
|
||||
|
||||
console.log(' [*] Waiting for logs. To exit press CTRL+C');
|
||||
} catch (err) {
|
||||
console.warn(err);
|
||||
}
|
||||
})();
|
||||
38
node_modules/amqplib/examples/tutorials/receive_logs_direct.js
generated
vendored
Normal file
38
node_modules/amqplib/examples/tutorials/receive_logs_direct.js
generated
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('../..');
|
||||
const { basename } = require('path');
|
||||
|
||||
const exchange = 'direct_logs';
|
||||
const bindingKeys = process.argv.slice(2);
|
||||
if (bindingKeys.length < 1) {
|
||||
console.warn('Usage: %s [info] [warning] [error]', basename(process.argv[1]));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const connection = await amqp.connect('amqp://localhost');
|
||||
const channel = await connection.createChannel();
|
||||
|
||||
process.once('SIGINT', async () => {
|
||||
await channel.close();
|
||||
await connection.close();
|
||||
});
|
||||
|
||||
await channel.assertExchange(exchange, 'direct', { durable: false });
|
||||
const { queue } = await channel.assertQueue('', { exclusive: true });
|
||||
await Promise.all(bindingKeys.map(async (bindingKey) => {
|
||||
await channel.bindQueue(queue, exchange, bindingKey);
|
||||
}));
|
||||
|
||||
await channel.consume(queue, (message) => {
|
||||
if (message) console.log(" [x] %s:'%s'", message.fields.routingKey, message.content.toString());
|
||||
else console.warn(' [x] Consumer cancelled');
|
||||
}, { noAck: true });
|
||||
|
||||
console.log(' [*] Waiting for logs. To exit press CTRL+C.');
|
||||
} catch(err) {
|
||||
console.warn(err);
|
||||
}
|
||||
})();
|
||||
39
node_modules/amqplib/examples/tutorials/receive_logs_topic.js
generated
vendored
Normal file
39
node_modules/amqplib/examples/tutorials/receive_logs_topic.js
generated
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('../..');
|
||||
const { basename } = require('path');
|
||||
|
||||
const exchange = 'topic_logs';
|
||||
const bindingKeys = process.argv.slice(2);
|
||||
if (bindingKeys.length < 1) {
|
||||
console.log('Usage: %s pattern [pattern...]', basename(process.argv[1]));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const connection = await amqp.connect('amqp://localhost');
|
||||
const channel = await connection.createChannel();
|
||||
|
||||
process.once('SIGINT', async () => {
|
||||
await channel.close();
|
||||
await connection.close();
|
||||
});
|
||||
|
||||
await channel.assertExchange(exchange, 'topic', { durable: false });
|
||||
const { queue } = await channel.assertQueue('', { exclusive: true });
|
||||
await Promise.all(bindingKeys.map(async (bindingKey) => {
|
||||
await channel.bindQueue(queue, exchange, bindingKey);
|
||||
}));
|
||||
|
||||
await channel.consume(queue, (message) => {
|
||||
if (message) console.log(" [x] %s:'%s'", message.fields.routingKey, message.content.toString());
|
||||
else console.warn(' [x] Consumer cancelled');
|
||||
}, { noAck: true });
|
||||
|
||||
console.log(' [*] Waiting for logs. To exit press CTRL+C.');
|
||||
}
|
||||
catch (err) {
|
||||
console.warn(err);
|
||||
}
|
||||
})();
|
||||
49
node_modules/amqplib/examples/tutorials/rpc_client.js
generated
vendored
Normal file
49
node_modules/amqplib/examples/tutorials/rpc_client.js
generated
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('amqplib');
|
||||
const { basename } = require('path');
|
||||
const { v4: uuid } = require('uuid');
|
||||
|
||||
const queue = 'rpc_queue';
|
||||
|
||||
const n = parseInt(process.argv[2], 10);
|
||||
if (isNaN(n)) {
|
||||
console.warn('Usage: %s number', basename(process.argv[1]));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
(async () => {
|
||||
let connection;
|
||||
try {
|
||||
connection = await amqp.connect('amqp://localhost');
|
||||
const channel = await connection.createChannel();
|
||||
const correlationId = uuid();
|
||||
|
||||
const requestFib = new Promise(async (resolve) => {
|
||||
const { queue: replyTo } = await channel.assertQueue('', { exclusive: true });
|
||||
|
||||
await channel.consume(replyTo, (message) => {
|
||||
if (!message) console.warn(' [x] Consumer cancelled');
|
||||
else if (message.properties.correlationId === correlationId) {
|
||||
resolve(message.content.toString());
|
||||
}
|
||||
}, { noAck: true });
|
||||
|
||||
await channel.assertQueue(queue, { durable: false });
|
||||
console.log(' [x] Requesting fib(%d)', n);
|
||||
channel.sendToQueue(queue, Buffer.from(n.toString()), {
|
||||
correlationId,
|
||||
replyTo,
|
||||
});
|
||||
});
|
||||
|
||||
const fibN = await requestFib;
|
||||
console.log(' [.] Got %d', fibN);
|
||||
}
|
||||
catch (err) {
|
||||
console.warn(err);
|
||||
}
|
||||
finally {
|
||||
if (connection) await connection.close();
|
||||
};
|
||||
})();
|
||||
46
node_modules/amqplib/examples/tutorials/rpc_server.js
generated
vendored
Normal file
46
node_modules/amqplib/examples/tutorials/rpc_server.js
generated
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('amqplib');
|
||||
|
||||
const queue = 'rpc_queue';
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const connection = await amqp.connect('amqp://localhost');
|
||||
const channel = await connection.createChannel();
|
||||
|
||||
process.once('SIGINT', async () => {
|
||||
await channel.close();
|
||||
await connection.close();
|
||||
});
|
||||
|
||||
await channel.assertQueue(queue, { durable: false });
|
||||
|
||||
channel.prefetch(1);
|
||||
await channel.consume(queue, (message) => {
|
||||
const n = parseInt(message.content.toString(), 10);
|
||||
console.log(' [.] fib(%d)', n);
|
||||
const response = fib(n);
|
||||
channel.sendToQueue(message.properties.replyTo, Buffer.from(response.toString()), {
|
||||
correlationId: message.properties.correlationId
|
||||
});
|
||||
channel.ack(message);
|
||||
});
|
||||
|
||||
console.log(' [x] Awaiting RPC requests. To exit press CTRL+C.');
|
||||
}
|
||||
catch (err) {
|
||||
console.warn(err);
|
||||
}
|
||||
})();
|
||||
|
||||
function fib(n) {
|
||||
// Do it the ridiculous, but not most ridiculous, way. For better,
|
||||
// see http://nayuki.eigenstate.org/page/fast-fibonacci-algorithms
|
||||
let a = 0, b = 1;
|
||||
for (let i=0; i < n; i++) {
|
||||
let c = a + b;
|
||||
a = b; b = c;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
31
node_modules/amqplib/examples/tutorials/send.js
generated
vendored
Normal file
31
node_modules/amqplib/examples/tutorials/send.js
generated
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('amqplib');
|
||||
|
||||
const queue = 'hello';
|
||||
const text = 'Hello World!';
|
||||
|
||||
(async () => {
|
||||
let connection;
|
||||
try {
|
||||
connection = await amqp.connect('amqp://localhost');
|
||||
const channel = await connection.createChannel();
|
||||
|
||||
await channel.assertQueue(queue, { durable: false });
|
||||
|
||||
// NB: `sentToQueue` and `publish` both return a boolean
|
||||
// indicating whether it's OK to send again straight away, or
|
||||
// (when `false`) that you should wait for the event `'drain'`
|
||||
// to fire before writing again. We're just doing the one write,
|
||||
// so we'll ignore it.
|
||||
channel.sendToQueue(queue, Buffer.from(text));
|
||||
console.log(" [x] Sent '%s'", text);
|
||||
await channel.close();
|
||||
}
|
||||
catch (err) {
|
||||
console.warn(err);
|
||||
}
|
||||
finally {
|
||||
if (connection) await connection.close();
|
||||
};
|
||||
})();
|
||||
34
node_modules/amqplib/examples/tutorials/worker.js
generated
vendored
Normal file
34
node_modules/amqplib/examples/tutorials/worker.js
generated
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env node
|
||||
// Process tasks from the work queue
|
||||
|
||||
const amqp = require('amqplib');
|
||||
|
||||
const queue = 'task_queue';
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const connection = await amqp.connect('amqp://localhost');
|
||||
process.once('SIGINT', async () => {
|
||||
await connection.close();
|
||||
});
|
||||
|
||||
const channel = await connection.createChannel();
|
||||
await channel.assertQueue(queue, { durable: true });
|
||||
|
||||
channel.prefetch(1);
|
||||
await channel.consume(queue, (message) => {
|
||||
const text = message.content.toString();
|
||||
console.log(" [x] Received '%s'", text);
|
||||
const seconds = text.split('.').length - 1;
|
||||
setTimeout(() => {
|
||||
console.log(" [x] Done");
|
||||
channel.ack(message);
|
||||
}, seconds * 1000);
|
||||
}, { noAck: false });
|
||||
|
||||
console.log(" [*] Waiting for messages. To exit press CTRL+C");
|
||||
}
|
||||
catch (err) {
|
||||
console.warn(err);
|
||||
}
|
||||
})();
|
||||
Reference in New Issue
Block a user