【问题标题】:Moving messages between queues rabbitMQ在队列之间移动消息 rabbitMQ
【发布时间】:2014-03-25 20:24:56
【问题描述】:

我希望能够在 Rabbit 的队列之间(手动)移动消息。

例如:

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

我希望能够将例如消息“a”从第一个队列移动到第二个队列。这可以是手动操作。两个队列都在同一个代理上,我不想通过任何交换发送它们。有没有办法做到这一点?我一直在玩rabbitmqctl,但似乎无法让它工作。我对任何其他可以让我完成此任务的工具持开放态度。最终,我希望有某种消息选择器(例如,将带有某些标头字段 = X 的所有消息从第一队列移动到第二队列)。

我还是 rabbitmq 和 amqp 的新手,但一直找不到有关如何执行此操作的文档(如果可能的话)。

谢谢。

【问题讨论】:

    标签: rabbitmq message messaging amqp


    【解决方案1】:

    @Dax - 我刚刚在这里回答了同样的问题:Is it possible to move / merge messages between RabbitMQ queues?

    我在那里有很长的描述。为避免重复内容,我不想复制/粘贴。

    听起来您正在寻找的是 rabbitmq 铲子插件。

    它内置在核心中,只需启用它:

    rabbitmq-plugins enable rabbitmq_shovel
    rabbitmq-plugins enable rabbitmq_shovel_management
    

    从 GUI 的“管理”部分,您会发现创建铲子的简单界面。

    看我的其他帖子!

    【讨论】:

    • 我以管理员身份在队列中看到“移动消息”选项,但用户看不到此选项。我们如何让他们通过队列以及配置、写入和读取来访问此选项?
    【解决方案2】:

    它没有被记录的事实是因为它远离消息传递模型。

    将消息发送到特定队列很容易 - 例如,请参阅 tutorial #1 - 但读取消息的唯一方法是使用它们,按照代理发送给客户端的顺序。

    不允许像使用 SQL 那样从队列中选择消息。

    您可以做的是让客户端(或最终是插件,但这是一个高级主题)使用队列中的消息,并根据某些规则重新将它们发布到后续队列或另一个队列。

    【讨论】:

      【解决方案3】:

      这是一个简单的 java 代码,用于将所有内容从一个队列移动到另一个队列:

      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】:

        你可以用这个 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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-06-09
          • 2013-08-09
          • 1970-01-01
          • 1970-01-01
          • 2018-07-28
          • 2015-11-05
          • 1970-01-01
          相关资源
          最近更新 更多