RabbitMQ-动态路由模型

RabbitMQ-动态路由模型,第1张

RabbitMQ-动态路由模型 使用案例

统配符
* ( 匹配不多不少恰好1个词 )
# ( 匹配一个或多个词 )
如:
user.#   可以匹配user.info.name以及user.info等
user.*   只能匹配 user.info
编写动态路由模型生产者的代码
    // 动态路由模式生产者
    @Test
    void contextLoads5() throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setPassword("123");
        connectionFactory.setUsername("admin");
        connectionFactory.setHost("106.15.73.43");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/rabbitmq_zhj");
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare("topic_logs", "topic" );
        channel.basicPublish("topic_logs", "user.info" , MessageProperties.PERSISTENT_TEXT_PLAIN, "信息的routingkey是user.info".getBytes());
        channel.basicPublish("topic_logs", "user.info.log" , MessageProperties.PERSISTENT_TEXT_PLAIN, "信息的routingkey是user.info,log".getBytes());
        channel.close();
        connection.close();
    }
编写动态路由模型消费者的代码 消费者1
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setVirtualHost("/rabbitmq_zhj");
        connectionFactory.setHost( "106.15.73.43" );
        connectionFactory.setPort( 5672 );
        connectionFactory.setUsername( "admin" );
        connectionFactory.setPassword( "123" );
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare( "topic_logs" , "topic" );
        String tempQueue = channel.queueDeclare().getQueue();
        channel.queueBind( tempQueue, "topic_logs", "user.#" );
        channel.basicConsume( tempQueue, true, new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("user.#的拦截信息为: " + new String(body));
            }
        });
    }
消费者2
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setVirtualHost("/rabbitmq_zhj");
        connectionFactory.setHost( "106.15.73.43" );
        connectionFactory.setPort( 5672 );
        connectionFactory.setUsername( "admin" );
        connectionFactory.setPassword( "123" );
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare( "topic_logs" , "topic" );
        String tempQueue = channel.queueDeclare().getQueue();
        channel.queueBind( tempQueue, "topic_logs", "user.*" );
        channel.basicConsume( tempQueue, true, new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("user.*的拦截信息为: " + new String(body));
            }
        });
    }


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5638399.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存