简体   繁体   English

在RabbitMQ中将“恒定”队列添加到主题交换

[英]Adding “Constant” Queues to a Topic Exchange in RabbitMQ

I've recently learned RabbitMQ with hopes of implementing it in my work flow. 我最近学习了RabbitMQ,希望在我的工作流程中实现它。 (I will be implementing it in Java) I just finished all the tutorials and was curious how I would implement a "constant" queue instead of a "temporary" queue. (我将用Java实现它)我刚刚完成了所有教程,并且很好奇我将如何实现“恒定”队列而不是“临时”队列。 Or at least allow the consumer to get the message that the exchange sent. 或者至少允许消费者获得交易所发送的消息。 For example if I send a topic of "kern.overflow" but a consumer is offline, as soon as my consumer comes online as long as it is listening for something related to "kern.#" or "#.overflow" I want it to receive un-received messages. 例如,如果我发送“ kern.overflow”主题,但某个消费者离线,那么只要我的消费者上线,只要它正在收听与“ kern。#”或“#.overflow”相关的内容,我都希望它接收未收到的消息。

Here is the code to: 这是代码:

  1. create a persistent queue 创建一个持久队列
  2. bind the queue to the topic with "kern.#" as routing-key: 使用"kern.#"作为路由键将队列绑定到主题:

code: 码:

            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");

When you publish a message to myTopic using kern.overflow as routing-key the message is stored to the myPersistentQueue queue. 当您发布一条消息, myTopic使用kern.overflow作为路由密钥信息存储到myPersistentQueue队列。 The client can be off-line, when the client is on-line can get the messages. 客户端可以离线,当客户端在线时可以获取消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM