RabbitMQサンプルコード(5)

5.トピックモード

カスタム交換、トピックのタイプ、バインディング固有のキューおよびrouteKeyを使用したトピックモデル。

送信者->(カスタム交換、トピック)-(条件Aに対応)->キュー1->受信者1
                    |
                     -(条件Bに対応)->キュー2->受信者2
                              
(1)プロデューサー
インポートコム。 rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;

パブリッククラスEmitLogTopic {

    プライベート静的最終文字列EXCHANGE_NAME = "topic_logs";

    public static void main(String [] argv){         接続接続= null;         チャネルchannel = null;         {             ConnectionFactoryファクトリ= new ConnectionFactory();を試してください             factory.setHost( "localhost");             接続= factory.newConnection();             チャネル= connection.createChannel();             channel.exchangeDeclare(EXCHANGE_NAME、BuiltinExchangeType.TOPIC);             文字列routingKey = getRouting(argv);             文字列メッセージ= getMessage(argv);             channel.basicPublish(EXCHANGE_NAME、routingKey、null、message.getBytes( "UTF-8"));






            





            System.out.println( "[x]送信済み '" + routingKey + "': '" +メッセージ+ "'");

        } catch(Exception e){             e.printStackTrace();         }最後に{             if(connection!= null){                 try {                     connection.close();                 } catch(例外無視){                 }             }         }     }}









    プライベート静的文字列getRouting(String []文字列){         if(strings.length <1){             return "anonymous.info.wtf";         }         return string [0];     }




    private static String getMessage(String [] strings){         if(strings.length <2){             return "Hello World!";         }         return joinStrings(strings、 ""、1);     }




    プライベート静的文字列joinStrings(String []文字列、文字列区切り文字、int startIndex){         int length = strings.length;         if(length == 0){             return "";         }         if(length <startIndex){             return "";         }         StringBuilder words = new StringBuilder(strings [startIndex]);         for(int i = startIndex + 1; i <length; i ++){             words.append(delimiter).append(strings [i]);         }         return words.toString();     } }













    

(2)コンシューマー
インポートcom.rabbitmq.client。*;

インポートjava.io.IOException;

パブリッククラスReceiveLogsTopic {

    プライベート静的最終文字列EXCHANGE_NAME = "topic_logs";

    public static void main(String [] argv)は例外をスローします{         ConnectionFactory factory = new ConnectionFactory();         factory.setHost( "localhost");         接続connection = factory.newConnection();         チャネルchannel = connection.createChannel();         channel.exchangeDeclare(EXCHANGE_NAME、BuiltinExchangeType.TOPIC);         String queueName = channel.queueDeclare()。getQueue();         if(argv.length <1){             System.err.println( "使用法:ReceiveLogsTopic [binding_key] ...");             System.exit(1);         }



        






        for(String bindingKey:argv){             channel.queueBind(queueName、EXCHANGE_NAME、bindingKey);         }

        System.out.println( "[*]メッセージを待機しています。終了するには、CTRL + Cを押します");

        消費者の消費者は新しいDefaultConsumer(チャネル){=             @Override             公共ボイドhandleDelivery(文字列consumerTag、エンベロープエンベロープ、AMQP.BasicProperties特性、バイト[]本体)                     にIOException {スロー                 文字列メッセージ=新しい文字列(ボディを、 "UTF-8");                 System.out.println( "[x]受信済み '" + Envelope.getRoutingKey()+ "': '" +メッセージ+ "'");             }         };         channel.basicConsume(queueName、true、consumer);     } }









    

 

おすすめ

転載: blog.csdn.net/geekooler/article/details/100852717