栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

RabitMQ-路由模型

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

RabitMQ-路由模型

使用案例


路由模型在生产者端在向交换器发送消息时指定消息的routingkey,不同的消息类型对应不同的routingkey,然后在消费者端声明临时的队列后,在绑定队列时指定routingkey(可以绑定多个)。通过交换机的路由模型,将相同routingkey对应起来就可以了。

编写路由模型的生产者代码
    // 路由模式生产者
    @Test
    void contextLoads4() throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setPassword( "123" );
        connectionFactory.setPort( 5672 );
        connectionFactory.setVirtualHost( "/rabbitmq_zhj" );
        connectionFactory.setUsername( "admin" );
        connectionFactory.setHost( "106.15.73.43" );
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare( "direct_logs", "direct" );
        channel.basicPublish( "direct_logs", "error", MessageProperties.PERSISTENT_TEXT_PLAIN, "产生了Error的错误信息".getBytes() );
        channel.basicPublish( "direct_logs", "waring", MessageProperties.PERSISTENT_TEXT_PLAIN, "产生了Waring的错误信息".getBytes() );
        channel.close();
        connection.close();
    }
编写路由模型的消费者代码并运行 消费者1
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setVirtualHost( "/rabbitmq_zhj" );
        connectionFactory.setPort( 5672 );
        connectionFactory.setHost( "106.15.73.43" );
        connectionFactory.setUsername( "admin" );
        connectionFactory.setPassword( "123" );
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare( "direct_logs", "direct" );
        String tempQueue = channel.queueDeclare().getQueue();
        channel.queueBind( tempQueue, "direct_logs", "error" );
        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("Error打印:" + new String(body));
            }
        });
    }
消费者2
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setVirtualHost( "/rabbitmq_zhj" );
        connectionFactory.setPort( 5672 );
        connectionFactory.setHost( "106.15.73.43" );
        connectionFactory.setUsername( "admin" );
        connectionFactory.setPassword( "123" );
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare( "direct_logs", "direct" );
        String tempQueue = channel.queueDeclare().getQueue();
        channel.queueBind( tempQueue, "direct_logs", "error" );
        channel.queueBind( tempQueue, "direct_logs", "waring" );
        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("Error和Waring打印:" + new String(body));
            }
        });
    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/632521.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号