RabbitMQ使用--Topics通配符模式

在这里插入图片描述
在这里插入图片描述
图解:

红色Queue:绑定的是usa.# ,因此凡是以 usa.开头的routing key 都会被匹配到
黄色Queue:绑定的是#.news ,因此凡是以 .news结尾的 routing key 都会被匹配
代码:
producer

public class Producer {
    
    
    public static void main(String[] args) throws Exception{
    
    
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("139.196.37.163");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");
        connectionFactory.setVirtualHost("my_vhost");
        //创建连接
        Connection connection = connectionFactory.newConnection();
        //创建频道
        Channel channel = connection.createChannel();
        //创建交换机
        /**
         * String exchange,
         * BuiltinExchangeType type,
         * boolean durable,
         * boolean autoDelete,
         * boolean internal,
         * Map<String, Object> arguments
         */
        String exchangeName = "test_topic";
        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.TOPIC,true,false,false,null);
        //创建队列
        String queue1Name = "test_topic_queue1";
        String queue2Name = "test_topic_queue2";
        channel.queueDeclare(queue1Name,true,false,false,null);
        channel.queueDeclare(queue2Name,true,false,false,null);
        //绑定队列跟交换机
        channel.queueBind(queue1Name,exchangeName,"#.error");
        channel.queueBind(queue1Name,exchangeName,"order.*");
        channel.queueBind(queue2Name,exchangeName,"*.*");
        channel.basicPublish(exchangeName,"order.info",null,"body".getBytes());
        //9. 释放资源
        channel.close();
        connection.close();
    }
}

Consumer代码:

public class Consumer1 {
    
    
    public static void main(String[] args) throws Exception{
    
    

    ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("139.196.37.163");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");
        connectionFactory.setVirtualHost("my_vhost");
    //创建连接
    Connection connection = connectionFactory.newConnection();
    //创建频道
    Channel channel = connection.createChannel();
    //创建队列,如果有就不会创建
    /**
     * String queue,
     * boolean durable,
     * boolean exclusive,
     * boolean autoDelete,
     * Map<String, Object> arguments
     */
        channel.queueDeclare("test_topic_queue1", true, false, false, null);
    /**
     * String queue,
     * boolean autoAck,
     * Map<String, Object> arguments,
     * Consumer callback
     */
    DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
    
    
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
    
    

            System.out.println(new String(body));
        }
    };
        channel.basicConsume("test_topic_queue1", true, defaultConsumer);

}
}

猜你喜欢

转载自blog.csdn.net/weixin_44363766/article/details/114040178
今日推荐