rabbit TopicExchange

看一幅图, 很清楚的讲述了topic 

 

 

package top_exchange;

import com.rabbitmq.client.*;

public class ConsumerTopicExchange {



    public static void main(String[] args) throws Exception {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        connectionFactory.setVirtualHost("/");
        // 2通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();
        // 3通过connection 创建一个Channel
        Channel channel = connection.createChannel();

        // 4 声明
        String exchangeName = "test_topic_exchange";
        String exchangeType = "topic";
        String queueName = "test_topic_queue";
        // * 只匹配一个单词, user.name
        // String routingKey = "user.*";
        // # 匹配多个单词, user.name.address  ...
        String routingKey = "user.#";
        // 1. 声明交换机
        channel.exchangeDeclare(exchangeName, exchangeType, true, false, null);
        // 2,声名一个队列
        channel.queueDeclare(queueName, false, false, false, null);
        // 3、建立绑定关系
        channel.queueBind(queueName,exchangeName, routingKey);

        // 5.消息是否持久化
        QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
        // 6 设置channel
        channel.basicConsume(queueName, true, queueingConsumer);

        while (true) {
            //nextDelivery() 一个有参数,一个无参数 , 无参数会一直阻塞
            // 7 获取消息
            QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
            String msg = new String(delivery.getBody());
            System.out.println("消费端: " + msg);
            Envelope envelope = delivery.getEnvelope();
        }
    }
}

生产者

package top_exchange;

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

import java.io.IOException;
import java.util.concurrent.TimeoutException;


public class ProcuderTopicExchange {

    public static void main(String[] args) throws IOException, TimeoutException {
        // 创建一个ConnectionFactory 工厂, 并进行配置
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("localhost");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        connectionFactory.setVirtualHost("/");
        Connection connection = null;
        Channel channel = null;

        try {
            // 2通过连接工厂创建连接
            connection = connectionFactory.newConnection();
            // 3通过connection 创建一个Channel
            channel = connection.createChannel();
            //4、声名
            String exchangeName = "test_topic_exchange";
            String routingKey1 = "user.save";
            String routingKey2 = "user.update";
            String routingKey3 = "user.delete.abc";
            // 5 、发送
            String msg = "hello World RabbitMq 4 Topic Exchange Message ...";
            channel.basicPublish(exchangeName,routingKey1, null, msg.getBytes());
            channel.basicPublish(exchangeName,routingKey2, null, msg.getBytes());
            channel.basicPublish(exchangeName,routingKey3, null, msg.getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } finally {
//            记得关闭相关连接
            if (channel != null) {
                channel.close();
            }
            if (connection != null) {
                connection.close();
            }
        }


    }
}

如果有之前设置的路由为 #, 之后测试,* , 会发现,还可以接受三条信息,是因为之前的# 号绑定的,没有解绑,解绑一下就好了, 通过图形化管理界面也可以;

channel.queueUnbind(queueName, exchangeName, routingKey);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值