bashdockerrabbitmqqueuerabbitmq-exchange

RabbitMQ bind exchange to exchange in bash


I would like to bind an exchange to another exchange via a bash script (what I plan to use within a Dockerfile).

As simple code (like JS) it is fine, working perfectly, but I would like to use plain bash script for it, if it is possible.

The part of the JS code, what I would like to have but in bash:

// ...
await ch1.assertExchange('test-exchange', 'headers');
await ch1.assertExchange('another-exchange', 'headers');
await ch1.bindExchange('test-exchange', 'another-exchange','',{
      'x-match':'all',
      target: 'pay-flow'
});
// ...

When I run the JS code, then it is fine. I got the following results in RabbitMQ:

bash-5.1# rabbitmqadmin -u guest -p guest list bindings
+-----------------+-----------------------+-----------------------+
|     source      |      destination      |      routing_key      |
+-----------------+-----------------------+-----------------------+
|                 | test-queue            | test-queue            |
| test-exchange   | test-queue            |                       |
| test-exchange   | another-exchange      |                       |
+-----------------+-----------------------+-----------------------+

What I tried in bash:

#!/bin/bash
rabbitmqadmin -u guest -p guest declare binding source=test-exchange destination=another-exchange

Then I got the message of:

** Not found: /api/bindings/%2F/e/test-exchange/q/another-exchange

By the CLI/rabbitmqadmin documentation it seems, I am supposed to (or able only to) bind an exchange with a queue.

Anyone has any idea, how to solve it? (Maybe write the binder code in python and run it from the bash script?) Are there any kind of cli tool what capable to do it?


Solution

  • Please see the command's help:

    $ rabbitmqadmin help subcommands | grep -F 'declare binding'
      declare binding source=... destination=... [destination_type=... routing_key=... arguments=...]
    

    This is the correct set of arguments:

    rabbitmqadmin -u guest -p guest declare binding source=test-exchange destination=another-exchange destination_type=exchange
    

    Of course, before you run the above command the two exchanges must exist.

    Tested as follows:

    $ rabbitmqadmin declare exchange name=test-exchange type=direct
    exchange declared
    
    $ rabbitmqadmin declare exchange name=test-exchange-2 type=direct
    exchange declared
    
    $ rabbitmqadmin declare binding source=test-exchange destination=test-exchange-2 destination_type=exchange
    binding declared