6.Rabbitmq Topics

前言

主题模式下,消费者能够接收到符合某一主题的的消息,消息的匹配规则如下:

* (star) 匹配一个字符;
# (hash)匹配0~n个字符;

架构如下:

image.png

代码

  • build.gradle
plugins {
    id 'java'
}

group 'com.nick'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile group: 'com.rabbitmq', name: 'amqp-client', version: '5.2.0'
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
    compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'
    compile group: 'org.projectlombok', name: 'lombok', version: '1.18.0'
}
  • EmitLogTopic.java
package com.nick.topics;

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

public class EmitLogTopic {

    private static final String EXCHANGE_NAME = "topic_logs";

    public static void main(String[] argv)
            throws Exception {

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.5.136");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "topic");

        //* (star) can substitute for exactly one word.
        //# (hash) can substitute for zero or more words.
        String routingKey = "com.nick.rabbit";
        String message = "hello topic";

        channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
        System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");

        connection.close();
    }
}
  • ReceiveLogsTopic.java
package com.nick.topics;


import com.rabbitmq.client.*;

import java.io.IOException;

public class ReceiveLogsTopic {
    private static final String EXCHANGE_NAME = "topic_logs";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.5.136");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "topic");
        String queueName = channel.queueDeclare().getQueue();

        //* (star) can substitute for exactly one word.
        //# (hash) can substitute for zero or more words.

        //channel.queueBind(queueName, EXCHANGE_NAME, "*.*.rabbit");
        channel.queueBind(queueName, EXCHANGE_NAME, "#.rabbit");
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
                System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + message + "'");
            }
        };
        channel.basicConsume(queueName, true, consumer);
    }
}

测试

  • 执行ReceiveLogsTopic,再执行EmitLogTopic,看接收窗口结果;
  • 改变匹配规则,#.rabbit匹配末尾是rabbit的主题,*.*.rabbit匹配三个字母并且末尾是rabbit的主题。

完整代码

https://github.com/qiujiahong/rabbitmq_study

你可能感兴趣的:(6.Rabbitmq Topics)