RabbitMQ creating queues and bindings from command line

后端 未结 11 1590
时光取名叫无心
时光取名叫无心 2020-12-12 15:25

If I have RabbitMQ installed on my machine, is there a way to create a message queue from the command line and bind it to a certain exchange without using a client?

I

相关标签:
11条回答
  • 2020-12-12 16:12

    Walkthrough to Create and delete a queue in RabbitMQ:

    I couldn't find a commandline command to do it. Here is how I did it in code with java.

    Rabbitmq-server version 3.3.5 on Ubuntu.

    List the queues, no queues yet:

    sudo rabbitmqctl list_queues
    [sudo] password for eric:
    Listing queues ...
    ...done.
    

    Put this in CreateQueue.java

    import com.rabbitmq.client.ConnectionFactory;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.Channel;
    import java.util.*;
    public class CreateQueue {
      public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-message-ttl", 60000);
        channel.queueDeclare("kowalski", false, false, false, args);
        channel.close();
        connection.close();
      }
    }
    

    Supply the jar file that came with your rabbitmq installation:

    I'm using rabbitmq-client.jar version 0.9.1, use the one that comes with your version of rabbitmq.

    Compile and run:

    javac -cp .:rabbitmq-client.jar CreateQueue.java
    java -cp .:rabbitmq-client.jar CreateQueue
    

    It should finish without errors, check your queues now:

    sudo rabbitmqctl list_queues
    Listing queues ...
    kowalski        0
    ...done.
    

    the kowalski queue exists.

    0 讨论(0)
  • 2020-12-12 16:16

    Here is a more minimal Python example, taken from the RabbitMQ Python tutorial.

    First, install pika:

    sudo easy_install pika
    # (or use pip)
    

    This is all you need to send a message to localhost:

    import pika
    
    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
    channel = connection.channel()
    
    channel.queue_declare(queue='test-queue')
    channel.basic_publish(exchange='', routing_key='test-queue', body='Hello World!')
    
    0 讨论(0)
  • 2020-12-12 16:16

    helps to bind the exchange while you're at it:

    channel.queue_bind(queueName, exchange)
    

    C-;

    0 讨论(0)
  • 2020-12-12 16:17

    Maybe a little late to the party but I've done so using CURL.

    For queues:

    curl -i -u RABBITUSER:RABBITPASSWORD -H "content-type:application/json" \
    -XPUT -d'{"durable":true}' \
    http://192.168.99.100:15672/api/queues/%2f/QUEUENAME
    

    And for bindings

    curl -i -u RABBITUSER:RABBITPASSWORD -H "content-type:application/json" \
    -XPOST -d"{\"routing_key\":\"QUEUENAME\"}" \
    http://192.168.99.100:15672/api/bindings/%2f/e/EXCHANGENAME/q/QUEUENAME
    

    Note 192.168.99.100:15672 points to my RMQ Management

    0 讨论(0)
  • 2020-12-12 16:17

    rabbitmqctl, the provided command line interface, doesn't expose the ability to create a queue and bind it.

    It, however, is quite trivial to do it with a quick script though, and the RabbitMQ getting started guide shows several examples of it, both on the publisher as well as the consumer side.

    #do some work to connect
    #do some work to open a channel
    channel.queue_declare(queue='helloworld')
    

    I'm glossing over connecting, but it's a literal one liner to create a queue. The operation is also idempotent, meaning you can include the statement in a script and be safe, knowing that it won't keep recreating the queue or blowing out an existing one of the same name.

    0 讨论(0)
提交回复
热议问题