【问题标题】:How to get Messages by the consumer according to priority of the messages set by the publishers RabbitMQ消费者如何根据发布者设置的消息的优先级获取消息 RabbitMQ
【发布时间】:2018-01-29 06:24:36
【问题描述】:

我已经为单个消费者(即可能根据消息优先级接收消息的单个消费者)设置了一些优先级的消息发布。 我想要的是获取这些消息并根据消费者端的消息优先级打印它们。嘿伙计们帮帮我!

public class Send extends Thread {

   int priority; 
   String name = "";
   String app_type = "";
   private static final String EXCHANGE_NAME = "topic_exchange";

    public void run()
    {
        ConnectionFactory connFac = new ConnectionFactory();
        connFac.setHost("localhost");

        try {

                Connection conn = connFac.newConnection();
                Channel channel = conn.createChannel();
                channel.exchangeDeclare(EXCHANGE_NAME, 
                BuiltinExchangeType.TOPIC);
                for(int j=1; j<=200; j++)
                {
                    randomWait();

                    int random = (int)(Math.random() * 10 + 1);

                    String routingKey = j+"."+"update"+"."+app_type;
                    String msg = name;
                    channel.basicPublish(EXCHANGE_NAME, routingKey, new 
                    AMQP.BasicProperties.Builder()
                            .contentType("text/plain")
                            .deliveryMode(2)
                            .priority(priority)
                            .build(),
                            msg.getBytes("UTF-8"));
                    System.out.println("Sent " + routingKey + " : " + msg + 
                    " "+" Priority : "+priority);
                }

                channel.close();
                conn.close();

        } catch (IOException ex) {
            Logger.getLogger(Send.class.getName()).log(Level.SEVERE, null, 
             ex);
            System.out.println("Exception1 :--"+ex);

        } catch (TimeoutException ex) {
            Logger.getLogger(Send.class.getName()).log(Level.SEVERE, null, 
          ex);
            System.out.println("Exception 2:--"+ex);
        }
    }

     void randomWait()
    {
        try {
           Thread.currentThread().sleep((long)(200*Math.random()));
        } catch (InterruptedException x) {
           System.out.println("Interrupted!");
        }
    }

   public static void main(String[] args) {
        // TODO code application logic here

        Send test1 = new Send();
        test1.name = "Hello ANDROID";
        test1.app_type = "ANDROID";
        test1.priority = 10;

        Send test2 = new Send();
        test2.name = "Hello ANDROID";
        test2.app_type = "ANDROID";
        test2.priority = 5;

        test1.start();
        test2.start();
    }
}

在上面的代码中,我使用线程来传递优先级和消息值,并同时启动这两个线程来发布具有不同优先级的消息。我已经在 AMQ Builder 中设置了优先级值。

【问题讨论】:

    标签: rabbitmq spring-rabbit rabbitmq-exchange rabbitmq-shovel


    【解决方案1】:

    队列必须是configured to support priority

    【讨论】:

    • 消费者可以根据消息优先级从队列中取回消息。也就是说假设消费者各自的队列有消息(M1(5),M2(1),M3(3)............),现在消费者可以先检索M1(5),因为它具有更高的优先级。跨度>
    • 是的,当然,只要队列配置正确 - 请阅读我的答案中的文档链接。
    猜你喜欢
    • 2020-04-20
    • 2016-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多