06 rabbitmq之topics

在介绍了rabbitmq的Routing模式之后,这一节我们将阐述它的topics模式的用法。

1、前提约束

  • 已经完成rabbitmq的第一个简单的测试程序
    https://www.jianshu.com/p/77bfc4fe5a1a
  • 2、操作步骤
    我们马上要测试的rabbitmq的工作模式如下:

     
     
    16204070-6c2b15d4d545c77d.png
    topics
  • 在src/main/java文件夹下创建包net.wanho.rabbitmq.topics
  • 在net.wanho.rabbitmq.topics创建Producer.java
package net.wanho.rabbitmq.topics;

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

/**
 * 根据用户的通知设置去通知用户,设置接收Email的用户只接收Email,
 * 设置接收sms的用户只接收sms,设置两种 通知类型都接收的则两种通知都有效。
 */
public class producer {
    public static void main(String[] args) throws Exception {
        String QUEUE_INFORM_EMAIL = "queue_inform_email";
        String QUEUE_INFORM_SMS = "queue_inform_sms";
        String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";
        Connection connection = null;
        Channel channel = null;
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        connection = factory.newConnection();
        channel = connection.createChannel();
        channel.exchangeDeclare(EXCHANGE_TOPICS_INFORM, BuiltinExchangeType.TOPIC);
        channel.queueDeclare(QUEUE_INFORM_EMAIL, true, false, false, null);
        channel.queueDeclare(QUEUE_INFORM_SMS, true, false, false, null);
        for (int i = 0; i < 10; i++) {
            String message = "email inform to user" + i;
            channel.basicPublish(EXCHANGE_TOPICS_INFORM, "inform.email", null, message.getBytes());
            System.out.println("Send Message is:'" + message + "'");
        }
        for (int i = 0; i < 10; i++) {
            String message = "sms inform to user" + i;
            channel.basicPublish(EXCHANGE_TOPICS_INFORM, "inform.sms", null, message.getBytes());
            System.out.println("Send Message is:'" + message + "'");
        }
        for (int i = 0; i < 10; i++) {
            String message = "sms and email inform to user" + i;
            channel.basicPublish(EXCHANGE_TOPICS_INFORM, "inform.sms.email", null, message.getBytes());
            System.out.println("Send Message is:'" + message + "'");
        }
    }
}
  • 在net.wanho.rabbitmq.topics创建Consumer1.java
package net.wanho.rabbitmq.topics;

import com.rabbitmq.client.*;

import java.io.IOException;

class Consumer1 {

    public static void main(String[] args) throws Exception {
        String QUEUE_INFORM_EMAIL = "queue_inform_email";
        String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_INFORM_EMAIL, true, false, false, null);
        channel.exchangeDeclare(EXCHANGE_TOPICS_INFORM, BuiltinExchangeType.TOPIC);
        channel.queueBind(QUEUE_INFORM_EMAIL, EXCHANGE_TOPICS_INFORM, "inform.#");
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body) throws IOException {
                long deliveryTag = envelope.getDeliveryTag();
                String exchange = envelope.getExchange();
                String message = new String(body, "utf-8");
                System.out.println(message);

            }
        };
        channel.basicConsume(QUEUE_INFORM_EMAIL, true, defaultConsumer);
    }
}
  • 在net.wanho.rabbitmq.topics创建Consumer2.java
package net.wanho.rabbitmq.topics;

import com.rabbitmq.client.*;

import java.io.IOException;

public class Consumer2 {

    public static void main(String[] args) throws Exception {
        String QUEUE_INFORM_SMS = "queue_inform_sms";
        String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_INFORM_SMS, true, false, false, null);
        channel.exchangeDeclare(EXCHANGE_TOPICS_INFORM, BuiltinExchangeType.TOPIC);
        channel.queueBind(QUEUE_INFORM_SMS, EXCHANGE_TOPICS_INFORM, "inform.*.email");
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body) throws IOException {
                long deliveryTag = envelope.getDeliveryTag();
                String exchange = envelope.getExchange();
                String message = new String(body, "utf-8");
                System.out.println(message);

            }
        };
        channel.basicConsume(QUEUE_INFORM_SMS, true, defaultConsumer);
    }
}
  • 测试
    启动Consumer1,再启动Consumer2,然后启动Producer,我们会看到Consumer1消费了所有消息,因为它的通配符是"inform.#",Consumer2只消费了key值为inform.sms.email的消息,因为它的通配符是"inform.sms.email"。
    以上就是我们完成的rabbitmq的topics模式的测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值