primer cambio
This commit is contained in:
22
node_modules/amqplib/examples/direct_reply_to_client.js
generated
vendored
Normal file
22
node_modules/amqplib/examples/direct_reply_to_client.js
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('../');
|
||||
|
||||
const queue = 'rpc_queue';
|
||||
|
||||
(async () => {
|
||||
const connection = await amqp.connect();
|
||||
const channel = await connection.createChannel();
|
||||
|
||||
await channel.consume('amq.rabbitmq.reply-to', async (message) => {
|
||||
console.log(message.content.toString());
|
||||
await channel.close();
|
||||
await connection.close();
|
||||
}, { noAck: true });
|
||||
|
||||
await channel.assertQueue(queue, { durable: false });
|
||||
|
||||
channel.sendToQueue(queue, Buffer.from(' [X] ping'), {
|
||||
replyTo: 'amq.rabbitmq.reply-to',
|
||||
});
|
||||
})();
|
||||
25
node_modules/amqplib/examples/direct_reply_to_server.js
generated
vendored
Normal file
25
node_modules/amqplib/examples/direct_reply_to_server.js
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('../');
|
||||
const { v4: uuid } = require('uuid');
|
||||
|
||||
const queue = 'rpc_queue';
|
||||
|
||||
(async () => {
|
||||
const connection = await amqp.connect();
|
||||
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(message.content.toString());
|
||||
channel.sendToQueue(message.properties.replyTo, Buffer.from(' [.] pong'));
|
||||
}, { noAck: true });
|
||||
|
||||
console.log(' [x] To exit press CTRL+C.');
|
||||
|
||||
})();
|
||||
40
node_modules/amqplib/examples/headers.js
generated
vendored
Normal file
40
node_modules/amqplib/examples/headers.js
generated
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const amqp = require('../');
|
||||
|
||||
(async () => {
|
||||
|
||||
const connection = await amqp.connect();
|
||||
const channel = await connection.createChannel();
|
||||
|
||||
process.once('SIGINT', async () => {
|
||||
await channel.close();
|
||||
await connection.close();
|
||||
});
|
||||
|
||||
const { exchange } = await channel.assertExchange('matching exchange', 'headers');
|
||||
const { queue } = await channel.assertQueue();
|
||||
|
||||
// When using a headers exchange, the headers to be matched go in
|
||||
// the binding arguments. The routing key is ignore, so best left
|
||||
// empty.
|
||||
|
||||
// 'x-match' is 'all' or 'any', meaning "all fields must match" or
|
||||
// "at least one field must match", respectively. The values to be
|
||||
// matched go in subsequent fields.
|
||||
await channel.bindQueue(queue, exchange, '', {
|
||||
'x-match': 'any',
|
||||
'foo': 'bar',
|
||||
'baz': 'boo'
|
||||
});
|
||||
|
||||
await channel.consume(queue, (message) => {
|
||||
console.log(message.content.toString());
|
||||
}, { noAck: true });
|
||||
|
||||
channel.publish(exchange, '', Buffer.from('hello'), { headers: { baz: 'boo' }});
|
||||
channel.publish(exchange, '', Buffer.from('hello'), { headers: { foo: 'bar' }});
|
||||
channel.publish(exchange, '', Buffer.from('lost'), { headers: { meh: 'nah' }});
|
||||
|
||||
console.log(' [x] To exit press CTRL+C.');
|
||||
})();
|
||||
39
node_modules/amqplib/examples/receive_generator.js
generated
vendored
Normal file
39
node_modules/amqplib/examples/receive_generator.js
generated
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
'use strict';
|
||||
const co = require('co');
|
||||
const amqp = require('amqplib');
|
||||
const readline = require('readline');
|
||||
|
||||
co(function* () {
|
||||
const myConsumer = (msg) => {
|
||||
if (msg !== null) {
|
||||
console.log('consuming message %s in generator', JSON.stringify(msg.content.toString()));
|
||||
}
|
||||
};
|
||||
const conn = yield amqp.connect('amqp://localhost');
|
||||
try {
|
||||
// create a message to consume
|
||||
const q = 'hello';
|
||||
const msg = 'Hello World!';
|
||||
const channel = yield conn.createChannel();
|
||||
yield channel.assertQueue(q);
|
||||
channel.sendToQueue(q, Buffer.from(msg));
|
||||
console.log(" [x] Sent '%s'", msg);
|
||||
// consume the message
|
||||
yield channel.consume(q, myConsumer, { noAck: true });
|
||||
}
|
||||
catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}).catch(err => {
|
||||
console.warn('Error:', err);
|
||||
});
|
||||
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
|
||||
// pend until message is consumed
|
||||
rl.question('newline to exit', () => process.exit());
|
||||
42
node_modules/amqplib/examples/send_generators.js
generated
vendored
Normal file
42
node_modules/amqplib/examples/send_generators.js
generated
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
'use strict';
|
||||
|
||||
// NB this requires the module 'co':
|
||||
// npm install co
|
||||
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, Buffer.from(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);
|
||||
});
|
||||
70
node_modules/amqplib/examples/ssl.js
generated
vendored
Normal file
70
node_modules/amqplib/examples/ssl.js
generated
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
// Example of using a TLS/SSL connection. Note that the server must be
|
||||
// configured to accept SSL connections; see, for example,
|
||||
// http://www.rabbitmq.com/ssl.html.
|
||||
//
|
||||
// When trying this out, I followed the RabbitMQ SSL guide above,
|
||||
// almost verbatim. I set the CN of the server certificate to
|
||||
// 'localhost' rather than $(hostname) (since on my MBP hostname ends
|
||||
// up being "<blah>.local", which is just weird). My client
|
||||
// certificates etc., are in `../etc/client/`. My testca certificate
|
||||
// is in `../etc/testca` and server certs etc., in `../etc/server`,
|
||||
// and I've made a `rabbitmq.config` file, with which I start
|
||||
// RabbitMQ:
|
||||
//
|
||||
// RABBITMQ_CONFIG_FILE=`pwd`/../etc/server/rabbitmq \
|
||||
// /usr/local/sbin/rabbitmq-server &
|
||||
//
|
||||
// A way to check RabbitMQ's running with SSL OK is to use
|
||||
//
|
||||
// openssl s_client -connect localhost:5671
|
||||
|
||||
const amqp = require('../');
|
||||
const fs = require('fs');
|
||||
|
||||
// Assemble the SSL options; for verification we need at least
|
||||
// * a certificate to present to the server ('cert', in PEM format)
|
||||
// * the private key for the certificate ('key', in PEM format)
|
||||
// * (possibly) a passphrase for the private key
|
||||
//
|
||||
// The first two may be replaced with a PKCS12 file ('pfx', in pkcs12
|
||||
// format)
|
||||
|
||||
// We will also want to list the CA certificates that we will trust,
|
||||
// since we're using a self-signed certificate. It is NOT recommended
|
||||
// to use `rejectUnauthorized: false`.
|
||||
|
||||
// Options for full client and server verification:
|
||||
const opts = {
|
||||
cert: fs.readFileSync('../etc/client/cert.pem'),
|
||||
key: fs.readFileSync('../etc/client/key.pem'),
|
||||
// cert and key or
|
||||
// pfx: fs.readFileSync('../etc/client/keycert.p12'),
|
||||
passphrase: 'MySecretPassword',
|
||||
ca: [fs.readFileSync('../etc/testca/cacert.pem')]
|
||||
};
|
||||
|
||||
// Options for just confidentiality. This requires RabbitMQ's SSL
|
||||
// configuration to include the items
|
||||
//
|
||||
// {verify, verify_none},
|
||||
// {fail_if_no_peer_cert,false}
|
||||
//
|
||||
// const opts = { ca: [fs.readFileSync('../etc/testca/cacert.pem')] };
|
||||
|
||||
// Option to use the SSL client certificate for authentication
|
||||
// opts.credentials = amqp.credentials.external();
|
||||
|
||||
(async () => {
|
||||
const connection = await amqp.connect('amqp://localhost', opts);
|
||||
const channel = await connection.createChannel();
|
||||
|
||||
process.on('SIGINT', async () => {
|
||||
await channel.close();
|
||||
await connection.close();
|
||||
});
|
||||
|
||||
channel.sendToQueue('foo', Buffer.from('Hello World!'));
|
||||
|
||||
console.log(' [x] To exit press CTRL+C.');
|
||||
})();
|
||||
|
||||
40
node_modules/amqplib/examples/stream_queues/README.md
generated
vendored
Normal file
40
node_modules/amqplib/examples/stream_queues/README.md
generated
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
RabbitMQ Stream Examples
|
||||
---
|
||||
The [stream queues](https://www.rabbitmq.com/streams.html) are available starting from RabbitMQ 3.9
|
||||
|
||||
These examples show how to use stream queues with the lib.
|
||||
|
||||
Send a message to a stream queue
|
||||
```
|
||||
node send_stream.js
|
||||
```
|
||||
|
||||
Receive all the messages from stream queue:
|
||||
```
|
||||
node receive_stream.js
|
||||
```
|
||||
|
||||
Consumers can be configured to receive messages using an offset via the `x-stream-offset` argument. e.g.
|
||||
|
||||
```js
|
||||
channel.consume(queue, onMessage, {
|
||||
noAck: false,
|
||||
arguments: {
|
||||
'x-stream-offset': 'first'
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
RabbitMQ supports six different types of offset, however specifying them can be
|
||||
|
||||
| Offset Type | Example Value | Notes |
|
||||
|-----------|----------------------------------------------------------|-------|
|
||||
| First | `{'x-stream-offset':'first'}` | Start from the first message in the log |
|
||||
| Last | `{'x-stream-offset':'last'}` | Start from the last "chunk" of messages (could be multiple messages) |
|
||||
| Next | `{'x-stream-offset':'next'}` | Start from the next message (the default) |
|
||||
| Offset | `{'x-stream-offset':5}` | a numerical value specifying an exact offset to attach to the log at |
|
||||
| Timestamp | `{'x-stream-offset':{'!':'timestamp',value:1686519750}}` | a timestamp value specifying the point in time to attach to the log at. The timestamp must be the number of seconds since 00:00:00 UTC, 1970-01-01. Consumers can receive messages published a bit before the specified timestamp. |
|
||||
| Interval | `{'x-stream-offset':'1h'}` | the time interval relative to current time to attach the log at. Valid units are Y, M, D, h, m and s |
|
||||
|
||||
|
||||
See https://www.rabbitmq.com/streams.html#consuming for more details
|
||||
14
node_modules/amqplib/examples/stream_queues/package.json
generated
vendored
Normal file
14
node_modules/amqplib/examples/stream_queues/package.json
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "stream_queues",
|
||||
"version": "1.0.0",
|
||||
"description": "An example demonstrating use of stream queues",
|
||||
"main": "n",
|
||||
"dependencies": {
|
||||
"amqplib": "^0.10.3"
|
||||
},
|
||||
"devDependencies": {},
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"license": "ISC"
|
||||
}
|
||||
55
node_modules/amqplib/examples/stream_queues/receive_stream.js
generated
vendored
Normal file
55
node_modules/amqplib/examples/stream_queues/receive_stream.js
generated
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
const amqp = require('amqplib');
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const connection = await amqp.connect('amqp://localhost');
|
||||
process.once('SIGINT', connection.close);
|
||||
|
||||
const channel = await connection.createChannel();
|
||||
const queue = 'my_first_stream';
|
||||
|
||||
// Define the queue stream
|
||||
// Mandatory: exclusive: false, durable: true autoDelete: false
|
||||
await channel.assertQueue(queue, {
|
||||
exclusive: false,
|
||||
durable: true,
|
||||
autoDelete: false,
|
||||
arguments: {
|
||||
'x-queue-type': 'stream', // Mandatory to define stream queue
|
||||
'x-max-length-bytes': 2_000_000_000 // Set the queue retention to 2GB else the stream doesn't have any limit
|
||||
}
|
||||
});
|
||||
|
||||
channel.qos(100); // This is mandatory
|
||||
|
||||
channel.consume(queue, (msg) => {
|
||||
console.log(" [x] Received '%s'", msg.content.toString());
|
||||
channel.ack(msg); // Mandatory
|
||||
}, {
|
||||
noAck: false,
|
||||
arguments: {
|
||||
/*
|
||||
Here you can specify the offset: : first, last, next, offset, timestamp and interval, i.e.
|
||||
|
||||
'x-stream-offset': 'first'
|
||||
'x-stream-offset': 'last'
|
||||
'x-stream-offset': 'next'
|
||||
'x-stream-offset': 5
|
||||
'x-stream-offset': { '!': 'timestamp', value: 1686519750 }
|
||||
'x-stream-offset': '1h'
|
||||
|
||||
The timestamp must be the desired number of seconds since 00:00:00 UTC, 1970-01-01
|
||||
The interval units can be Y, M, D, h, m, s
|
||||
|
||||
*/
|
||||
'x-stream-offset': 'first'
|
||||
}
|
||||
});
|
||||
|
||||
console.log(' [*] Waiting for messages. To exit press CTRL+C');
|
||||
}
|
||||
// Catch and display any errors in the console
|
||||
catch(e) {
|
||||
console.log(e)
|
||||
}
|
||||
})();
|
||||
38
node_modules/amqplib/examples/stream_queues/send_stream.js
generated
vendored
Normal file
38
node_modules/amqplib/examples/stream_queues/send_stream.js
generated
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
const amqp = require('amqplib');
|
||||
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const connection = await amqp.connect('amqp://localhost');
|
||||
process.once('SIGINT', connection.close);
|
||||
|
||||
const channel = await connection.createChannel();
|
||||
const queue = 'my_first_stream';
|
||||
const msg = `Hello World! ${Date.now()}`;
|
||||
|
||||
// Define the queue stream
|
||||
// Mandatory: exclusive: false, durable: true autoDelete: false
|
||||
await channel.assertQueue(queue, {
|
||||
exclusive: false,
|
||||
durable: true,
|
||||
autoDelete: false,
|
||||
arguments: {
|
||||
'x-queue-type': 'stream', // Mandatory to define stream queue
|
||||
'x-max-length-bytes': 2_000_000_000 // Set the queue retention to 2GB else the stream doesn't have any limit
|
||||
}
|
||||
});
|
||||
|
||||
// Send the message to the stream queue
|
||||
await channel.sendToQueue(queue, Buffer.from(msg));
|
||||
console.log(" [x] Sent '%s'", msg);
|
||||
await channel.close();
|
||||
|
||||
// Close connection
|
||||
connection.close();
|
||||
}
|
||||
// Catch and display any errors in the console
|
||||
catch(e) {
|
||||
console.log(e)
|
||||
}
|
||||
})();
|
||||
|
||||
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);
|
||||
}
|
||||
})();
|
||||
21
node_modules/amqplib/examples/waitForConfirms.js
generated
vendored
Normal file
21
node_modules/amqplib/examples/waitForConfirms.js
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
const amqp = require('../');
|
||||
|
||||
(async () => {
|
||||
let connection;
|
||||
try {
|
||||
connection = await amqp.connect();
|
||||
const channel = await connection.createConfirmChannel();
|
||||
|
||||
for (var i=0; i < 20; i++) {
|
||||
channel.publish('amq.topic', 'whatever', Buffer.from('blah'));
|
||||
};
|
||||
|
||||
await channel.waitForConfirms();
|
||||
console.log('All messages done');
|
||||
await channel.close();
|
||||
} catch (err) {
|
||||
console.warn(err);
|
||||
} finally {
|
||||
if (connection) await connection.close();
|
||||
}
|
||||
})();
|
||||
Reference in New Issue
Block a user