|
| 1 | +const rabbit = require("rabbitmq-amqp-js-client") |
| 2 | +const { randomUUID } = require("crypto") |
| 3 | + |
| 4 | +const rabbitUser = process.env.RABBITMQ_USER || "rabbit" |
| 5 | +const rabbitPassword = process.env.RABBITMQ_PASSWORD || "rabbit" |
| 6 | + |
| 7 | +async function main() { |
| 8 | + const testExchange = `test-exchange-${randomUUID()}` |
| 9 | + const testQueue = `test-queue-${randomUUID()}` |
| 10 | + const routingKey = `test-key-${randomUUID()}` |
| 11 | + |
| 12 | + console.log("Creating the environment...") |
| 13 | + const environment = rabbit.createEnvironment({ |
| 14 | + host: "localhost", |
| 15 | + port: 5672, |
| 16 | + username: rabbitUser, |
| 17 | + password: rabbitPassword, |
| 18 | + }) |
| 19 | + |
| 20 | + console.log("Opening a connection...") |
| 21 | + const connection = await environment.createConnection() |
| 22 | + const management = connection.management() |
| 23 | + |
| 24 | + console.log("Creating a queue and an exchange...") |
| 25 | + const queue = await management.declareQueue(testQueue) |
| 26 | + const exchange = await management.declareExchange(testExchange) |
| 27 | + |
| 28 | + console.log("Binding exchange to queue...") |
| 29 | + await management.bind(routingKey, { source: exchange, destination: queue }) |
| 30 | + |
| 31 | + console.log("Opening a publisher and publishing 10 messages...") |
| 32 | + const publisher = await connection.createPublisher({ exchange: { name: testExchange, routingKey: routingKey } }) |
| 33 | + for (const i of Array(10).keys()) { |
| 34 | + const publishResult = await publisher.publish(rabbit.createAmqpMessage({ body: `Hello - ${i} - ` })) |
| 35 | + switch (publishResult.outcome) { |
| 36 | + case rabbit.OutcomeState.ACCEPTED: |
| 37 | + console.log("Message Accepted") |
| 38 | + break |
| 39 | + case rabbit.OutcomeState.RELEASED: |
| 40 | + console.log("Message Released") |
| 41 | + break |
| 42 | + case rabbit.OutcomeState.REJECTED: |
| 43 | + console.log("Message Rejected") |
| 44 | + break |
| 45 | + default: |
| 46 | + break |
| 47 | + } |
| 48 | + } |
| 49 | + publisher.close() |
| 50 | + |
| 51 | + console.log("Opening a consumer and consuming messages...") |
| 52 | + const consumer = await connection.createConsumer(testQueue, { |
| 53 | + messageHandler: (msg) => console.log(`MessageId: ${msg.message_id}; Payload: ${msg.body}`), |
| 54 | + }) |
| 55 | + consumer.start() |
| 56 | + await sleep(5000) |
| 57 | + |
| 58 | + console.log("Cleaning up...") |
| 59 | + consumer.close() |
| 60 | + await management.unbind(routingKey, { source: exchange, destination: queue }) |
| 61 | + await management.deleteExchange(testExchange) |
| 62 | + await management.deleteQueue(testQueue) |
| 63 | + management.close() |
| 64 | + await connection.close() |
| 65 | + await environment.close() |
| 66 | +} |
| 67 | + |
| 68 | +main() |
| 69 | + .then(() => console.log("done!")) |
| 70 | + .catch((res) => { |
| 71 | + console.log("ERROR ", res) |
| 72 | + process.exit(-1) |
| 73 | + }) |
| 74 | +const sleep = (ms) => new Promise((r) => setTimeout(r, ms)) |
0 commit comments