Java笔记-使用RabbitMQ的Java接口实现topic(主题模式)

目录

 

 

基本概念

代码与实例


 

基本概念

实现的就是官方给出的这个模型:

Java笔记-使用RabbitMQ的Java接口实现topic(主题模式)_第1张图片

Topic exchange:将路由和某模式匹配

其中

#:匹配一个或多个

*:匹配一个

比如下面要举得这个例子

交换机设置为topic模式,生产者生成的消息的路由键值为goods.XXXX

其中XXXX,可能为add、delete、update、modify等

队列一绑定的是goods.add

队列二绑定的是goods.#

这样话,如果生产者生产一个路由键值为goods.add的消息,辣么2个队列都将会收到。

如果生产者生成一个路由键值为goods.delete的消息,辣么只有1个队列将会收到。

 

代码与实例

当生产者和消费者跑起来后,对应的RabbitMQ交换机如下:

Java笔记-使用RabbitMQ的Java接口实现topic(主题模式)_第2张图片

可见有2个队列,一个绑定的路由键为goods.add

一个绑定的路由键为goods.#

当生产者发送的键值为goods.add时:

两个消费者都可以收到:

Java笔记-使用RabbitMQ的Java接口实现topic(主题模式)_第3张图片

Java笔记-使用RabbitMQ的Java接口实现topic(主题模式)_第4张图片

当生产者发送的键值为goods.delete时:

只有消费者二可以收到,消费者一和以前一样

Java笔记-使用RabbitMQ的Java接口实现topic(主题模式)_第5张图片

Java笔记-使用RabbitMQ的Java接口实现topic(主题模式)_第6张图片

源码如下:

Recv1.java

package topic;

import com.rabbitmq.client.*;
import util.ConnectionUtils;

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

public class Recv1 {

    private static final String EXCHANGE_NAME = "test_exchange_topic";
    private static final String QUEUE_NAME = "test_queue_topic_1";

    public static void main(String[] args) throws IOException, TimeoutException {

        Connection connection = ConnectionUtils.getConnection();
        final Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "goods.add");

        channel.basicQos(1);

        Consumer consumer = new DefaultConsumer(channel){

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                String msg = new String(body, "utf-8");
                System.out.println("[1] Recv msg : " + msg);


                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    System.out.println("[1] done");
                    channel.basicAck(envelope.getDeliveryTag(), false);
                }
            }
        };

        boolean autoAck = false;
        channel.basicConsume(QUEUE_NAME, autoAck, consumer);
    }
}

Recv2.java

package topic;

import com.rabbitmq.client.*;
import util.ConnectionUtils;

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

public class Recv2 {

    private static final String EXCHANGE_NAME = "test_exchange_topic";
    private static final String QUEUE_NAME = "test_queue_topic_2";

    public static void main(String[] args) throws IOException, TimeoutException {

        Connection connection = ConnectionUtils.getConnection();
        final Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "goods.#");

        channel.basicQos(1);

        Consumer consumer = new DefaultConsumer(channel){

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                String msg = new String(body, "utf-8");
                System.out.println("[2] Recv msg : " + msg);


                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    System.out.println("[2] done");
                    channel.basicAck(envelope.getDeliveryTag(), false);
                }
            }
        };

        boolean autoAck = false;
        channel.basicConsume(QUEUE_NAME, autoAck, consumer);
    }
}

Send.java

package topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import util.ConnectionUtils;

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

public class Send {

    private static final String EXCHANGE_NAME = "test_exchange_topic";

    public static void main(String[] args) throws IOException, TimeoutException {

        Connection connection = ConnectionUtils.getConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "topic");

        String msgString = "goods ... ... ...";
        channel.basicPublish(EXCHANGE_NAME, "goods.delete", null, msgString.getBytes());
        System.out.println("send msg :" + msgString);
        channel.close();
        connection.close();
    }
}

ConnectionUtils.java

package util;

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

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

public class ConnectionUtils {

    public static Connection getConnection() throws IOException, TimeoutException {

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setVirtualHost("/vhost_cff");
        factory.setUsername("cff");
        factory.setPassword("123");

        return factory.newConnection();
    }
}

源码打包下载地址:

https://github.com/fengfanchen/Java/tree/master/TopicModel

你可能感兴趣的:(Java,RabbitMQ)