Moving messages between queues rabbitMQ

房东的猫 提交于 2019-12-22 05:43:10

问题


I am looking to be able to move messages between queues (manually) in Rabbit.

For example:

first-queue has messages ['a','b','c','d','e','f']
second-queue has messages ['x','y']

I want to be able to move for example message 'a' to second-queue from first-queue. This can be a manual operation. Both queues are on the same broker, and I do not want to send them through any exchange. Is there anyway to do this? I have been playing with rabbitmqctl but can't seem to get it to work. I am open to any other tools that would allow me to accomplish this. Eventually I am hoping to have some sort of message selector (for example move all messages with some header field = X from first-queue to second-queue).

I am still new to rabbitmq and amqp but have been unable to find documentation on how to do this (if it is even possible).

Thanks.


回答1:


@Dax - I just answered this same question here: Is it possible to move / merge messages between RabbitMQ queues?

I have a long description there. To avoid duplicate content I don't want to copy/paste.

It sounds like what you are looking for is the rabbitmq shovel plugin.

It is built into the core, simply enable it:

rabbitmq-plugins enable rabbitmq_shovel
rabbitmq-plugins enable rabbitmq_shovel_management

From the Admin section in the GUI you'll find an easy interface to create shovels.

See other post for me deets!




回答2:


The fact that it's not documented is because it's far away from messaging model.

It's easy to send a message to a specific queue - see tutorial #1 for example - but the only way to read messages is to consume them, in the order the broker send to the clients.

It's not allowed to select messages from a queue as you can do with SQL.

What you can do is to let a client (or eventually, a plugin, but this is an advanced topic) consume messages from a queue, and based on some rule you re-publish them to a subsequent queue or to another one.




回答3:


Here is a simple java code to move everything from one queue to another:

public void moveMessages(
            final String sourceQueueName,
            final String targetQueueName,
            final String rabbitmqHost,
            final String rabbitmqUsername,
            final String rabbitmqPassword,
            final String rabbitmqVirtualHost
) throws IOException {

        // Initialize the consuming and publishing channel
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(rabbitmqHost);
        factory.setUsername(rabbitmqUsername);
        factory.setPassword(rabbitmqPassword);
        factory.setVirtualHost(rabbitmqVirtualHost);
        Connection connection = factory.newConnection();

        Channel consumingChannel = connection.createChannel();
        Channel publishingChannel = connection.createChannel();


        while (true) {
            // Get the first message in the queue (auto ack = false)
            GetResponse response = consumingChannel.basicGet(sourceQueueName, false);

            if (response == null) {
                return;
            }

            BasicProperties properties = response.getProps();

            // Publish the message to the origin queue
            publishingChannel.txSelect();
            publishingChannel.basicPublish("", targetQueueName, (AMQP.BasicProperties) properties, response.getBody());
            publishingChannel.txCommit();

            // Acknowledge the message in the dead letter queue
            consumingChannel.basicAck(response.getEnvelope().getDeliveryTag(), false);
        }
    }



回答4:


You can create a shovel using this curl :

curl 
-u  "user:password" 
-vvv 'http://localhost:15672/api/parameters/shovel/%2Foms/Move%20from%20sourceQueue' 
-X PUT 
-H 'content-type: application/json' 
--data-binary '
{
    "component": "shovel",
    "vhost": "/vhost",
    "name": "Move from sourceQueue",
    "value": {
        "src-uri": "amqp:///%2Fvhost",
        "src-queue": "sourceQueue",
        "src-protocol": "amqp091",
        "src-prefetch-count": 1000,
        "src-delete-after": "queue-length",
        "dest-protocol": "amqp091",
        "dest-uri": "amqp:///%2Fvhost",
        "dest-add-forward-headers": false,
        "ack-mode": "on-confirm",
        "dest-queue": "destQueue"
    }
}
' --compressed


来源:https://stackoverflow.com/questions/22645517/moving-messages-between-queues-rabbitmq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!