RabbitMQ creating queues and bindings from command line

后端 未结 11 1588
时光取名叫无心
时光取名叫无心 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 args = new HashMap();
        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.

提交回复
热议问题