【问题标题】:Adding "Constant" Queues to a Topic Exchange in RabbitMQ在 RabbitMQ 中将“恒定”队列添加到主题交换
【发布时间】:2014-05-22 18:03:08
【问题描述】:

我最近学习了 RabbitMQ,希望能在我的工作流程中实现它。 (我将在 Java 中实现它)我刚刚完成了所有教程并且很好奇我将如何实现一个“常量”队列而不是一个“临时”队列。或者至少允许消费者获取交易所发送的消息。例如,如果我发送“kern.overflow”主题但消费者离线,只要我的消费者上线,只要它正在侦听与“kern.#”或“#.overflow”相关的内容,我就想要它接收未收到的消息。

【问题讨论】:

  • 作为常数你的意思是.. 持久队列?
  • @Gas 是的,我的意思是持久队列。

标签: java rabbitmq message-queue


【解决方案1】:

代码如下:

  1. 创建一个持久队列
  2. "kern.#"作为routing-key将队列绑定到主题:

代码:

            String myPersistentQueue = "myPersistentQueue";
            boolean isDurable = true;
            boolean isExclusive = false;
            boolean isAutoDelete = false;
            channel.queueDeclare(myPersistentQueue, isDurable, isExclusive, isAutoDelete, null);
            channel.queueBind(myPersistentQueue, "myTopic", "kern.#");
            final QueueingConsumer consumer = new QueueingConsumer(channel);
            boolean autoAck = true;
            String tag1 = channel.basicConsume(myPersistentQueue, autoAck, consumer);
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        Delivery delivery;
                        try {
                            delivery = consumer.nextDelivery();
                            String message = new String(delivery.getBody());
                            System.out.println("Received: " + message);
                        } catch (Exception ex) {
                            Logger.getLogger(TestMng.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            });
            System.out.println("Consumers Ready");

当您使用kern.overflow 作为路由键将消息发布到myTopic 时,消息将存储到myPersistentQueue 队列中。客户端可以离线,当客户端在线时可以收到消息。

【讨论】:

    猜你喜欢
    • 2011-12-29
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-31
    • 1970-01-01
    • 2014-05-21
    相关资源
    最近更新 更多