0

単一のコンシューマ(つまり、メッセージの優先順位に従ってメッセージを受信する可能性のある単一のコンシューマ)に対して優先順位を設定してパブリッシュします。 私が望むのは、そのメッセージを入手し、消費者側のメッセージ優先度に従って印刷することです。ねえみんなこれで助けて!上記のコードでパブリッシャーによって設定されたメッセージの優先順位に従って、消費者がメッセージを取得する方法RabbitMQ

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

Iは、優先順位とメッセージの値を渡すために使用スレッドを有し、異なる優先度のメッセージを発行すると同時に、スレッドの両方を開始しました。 AMQ Builderで優先度の値を設定しました。

答えて

1

キューはconfigured to support priorityである必要があります。

+0

コンシューマは、メッセージ優先度に従ってキューからメッセージを取得できます。これは、消費者のそれぞれの待ち行列がメッセージ(M1(5)、M2(1)、M3(3)........)を有すると仮定し、より優先度が高いので、今度は消費者がM1(5) –

+0

もちろん、キューが正しく構成されている限り、私の答えのドキュメントのリンクを読んでください。 –

関連する問題