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

Topic 交换机

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

Topic 交换机

        发送消息时,需要指定路由key,消息到达交换机后,根据路由key匹配队列,队列在绑定交换机时指定了路由key,支持通配符,*代表1个词,#代表1个或多个词,交换机根据路由key把消息转发到匹配的队列中。 

package com.tech.rabbitmq.nospring.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;


public class Send {

    private final static String EXCHANGE_NAME = "exchange_topic";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.50.134");
        factory.setPort(5672);
        factory.setVirtualHost("/dev");
        factory.setUsername("tech");
        factory.setPassword("tech");
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            //绑定交换机
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
            String debugMsg = "这是订单服务的debug日志";
            String infoMsg = "这是订单服务的info日志";
            String errorMsg = "这是商品服务的error日志";
            channel.basicPublish(EXCHANGE_NAME, "order.log.debug", null, debugMsg.getBytes(StandardCharsets.UTF_8));
            channel.basicPublish(EXCHANGE_NAME, "order.log.info", null, infoMsg.getBytes(StandardCharsets.UTF_8));
            channel.basicPublish(EXCHANGE_NAME, "product.log.error", null, errorMsg.getBytes(StandardCharsets.UTF_8));
            System.out.println("消息发送成功");
        }
    }
}
package com.tech.rabbitmq.nospring.topic;

import com.rabbitmq.client.*;

import java.io.IOException;


public class Recv1 {
    private final static String EXCHANGE_NAME = "exchange_topic";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.50.134");
        factory.setPort(5672);
        factory.setVirtualHost("/dev");
        factory.setUsername("tech");
        factory.setPassword("tech");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        //绑定交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);

        String queueName = channel.queueDeclare().getQueue();
        //绑定交换机和队列
        channel.queueBind(queueName, EXCHANGE_NAME, "product.log.error");

        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("body=" + new String(body, "utf-8"));
                //手动确认消息,不是多条确认
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        };
        //false 关闭自动确认
        channel.basicConsume(queueName, false, consumer);
    }
}
package com.tech.rabbitmq.nospring.topic;

import com.rabbitmq.client.*;

import java.io.IOException;


public class Recv2 {
    private final static String EXCHANGE_NAME = "exchange_topic";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.50.134");
        factory.setPort(5672);
        factory.setVirtualHost("/dev");
        factory.setUsername("tech");
        factory.setPassword("tech");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        //绑定交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);

        String queueName = channel.queueDeclare().getQueue();
        //绑定交换机和队列
        channel.queueBind(queueName, EXCHANGE_NAME, "*.log.*");

        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("body=" + new String(body, "utf-8"));
                //手动确认消息,不是多条确认
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        };
        //false 关闭自动确认
        channel.basicConsume(queueName, false, consumer);
    }
}

Recv1日志

body=这是商品服务的error日志

Recv2日志

body=这是订单服务的debug日志
body=这是订单服务的info日志
body=这是商品服务的error日志

 

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

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

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