Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I dynamically create RabbitMQ shovel from my NodeJS app?

RabbitMQ shovel plugin provides an HTTP API to create & configure shovels. Assuming that I have enabled shovel & shovel management plugin for my RabbitMQ server instance, can I dynamically create shovels from my NodeJS app?

I currently use amqplib to connect to an exchange. https://www.squaremobius.net/amqp.node/channel_api.html

However I don't see any API to dynamically create a shovel. Is this achievable or are there any other libraries that support this?

like image 543
wraith Avatar asked Nov 15 '25 20:11

wraith


1 Answers

amqplib does not provide any specific method to create dynamically shovels, but you can do that using the Management HTTP-based API as specified in the docs: https://www.rabbitmq.com/shovel-dynamic.html#tutorial

So, in pure Node.js, this is and example of HTTP request (PUT method) to create a shovel programmatically:

var http = require('http');

var rabbit_host = "myrabbitmq.com";
var token = Buffer.from("admin:admin_pwd").toString('base64');
var shovel_name = "my_shovel";

var payload = {
    "component": "shovel",
    "vhost": "/",
    "name": "my_shovel",
    "value": {
        "src-uri": "amqp://user1:[email protected]",
        "src-exchange": "test",
        "dest-uri": "amqp://user2:[email protected]",
        "dest-exchange-key": "test2",
        "add-forward-headers": false,
        "ack-mode": "on-confirm",
        "delete-after": "never"
    }
};

var options = {
  "host": rabbit_host,
  "port": 15672,
  "path": "/api/parameters/shovel/%2F/" + shovel_name,
  "method": "PUT",
  "headers": { 
    "Authorization" : "Basic " + token,
    "Content-Type" : "application/json",
  }
}

var callback = function(response) {
    var str = ''
    response.on('data', function(chunk){
        str += chunk;
    });

    response.on('end', function(){
        console.log("end: response="+str);
    });
}

var body = JSON.stringify(payload);
http.request(options, callback).end(body).on('error', function(e) {
    console.log("error: " + e.message);
});

Obviously host "myrabbitmq.com" must be accessible from Node.js script on port 15672.

The example above creates a shovel from "myrabbitmq.com" to "anotherbroker.com", specifying exchanges for routing messages.

like image 200
beaver Avatar answered Nov 17 '25 10:11

beaver