rabbitmqspring-rabbitrabbitmq-exchangerabbitmq-shovel

How to get Messages by the consumer according to priority of the messages set by the publishers RabbitMQ


I have publish messages with some priority set for a single consumer(i.e single consumer that may receive messages according to message priority). What i want is to get that messages and print them according to the message priority on the consumer side. Hey guys Help me out in this !

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();
    }
}

In the above code I have use thread to pass the priority and message value and started the both the thread at the same time to publish messages with different priorities. I have set the priority value in the AMQ Builder.


Solution

  • The queue has to be configured to support priority.