RabbitMQ Java acquaintance a switch (fanout exchange)

RabbitMQ Java acquaintance a switch (fanout exchange)

First look at the AMQPagreement, RabbitMQthe structure will be more understanding.

In-depth understanding of AMQP protocol

Create a Mavenproject, import the appropriate package for your version of RabbitMQ server.
Here Insert Picture Description

		<dependency>
			<groupId>com.rabbitmq</groupId>
			<artifactId>amqp-client</artifactId>
			<version>3.6.5</version>
		</dependency>

Fan switch

Fan switch (funout exchange) routing the message queue to bind to all of its body, without regard to the routing key bindings. If N queues bound to a switch on the fan, when a message is sent to that fan switch, the switch will copy the message which are transmitted to all N queues.

Because the fan switches deliver a copy of the message to all queues bound to it, so it's very similar application cases are:

  • Massive multiplayer online (MMO) game can use it to deal with the global leaderboard updates and other events.
  • Sports news sites can use to near real-time score updates to mobile clients.
  • Distribution system use it to broadcast various status and configuration updates.
  • When a group chat, which is used to distribute messages to the user to participate in group chat. (AMQP no built-in concept of presence, XMPP and therefore may be a better choice).

Fan switch Legend:
Here Insert Picture Description
shown above, the producer (P) producing a message, the message 1 is pushed to the Exchange, since Exchange Type = fanout, this time will follow the routing rules fanout exchange of push message and tie it to all given Queue, Queue is two on the chart, and finally by the snoop Queue consumer spending.

Production end

routingKey = ""Because fanout exchange routing rules do not care routingKeyvalue (but not empty) of.

package com.kaven.rabbitmq.exchange.fanoutExchange;

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 FanoutProducer {

    // 自己服务器的IP
    private static String ip = "IP";
    // RabbitMQ启动的默认端口,也是应用程序进行连接RabbitMQ的端口
    private static int port = 5672;
    // RabbitMQ有一个 "/" 的虚拟主机
    private static String virtualHost = "/";

    // fanout exchange ,RabbitMQ提供的fanout exchange
    private static String exchangeName = "amq.fanout";
    // exchange type
    private static String exchangeType= "fanout";

    // 交换机路由的routingKey
    private static String routingKey = "";

    public static void main(String[] args) throws IOException, TimeoutException {
        // 1 创建一个连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(ip);
        connectionFactory.setPort(port);
        connectionFactory.setVirtualHost(virtualHost);

        // 2 创建连接
        Connection connection = connectionFactory.newConnection();

        // 3 创建Channel
        Channel channel = connection.createChannel();

        // 4 发送消息
        String msg = "RabbitMQ:Fanout Exchange 发送数据";
        channel.basicPublish(exchangeName ,routingKey ,null, msg.getBytes());

        // 5 关闭连接
        channel.close();
        connection.close();
    }
}

Consumer end

routingKey = "test", And the production side routingKeyis not the same, mainly to see whether fanout exchange routing rules as explained above, like, just create a consumer here, we can try the case of multiple consumers, in fact, is the same.

package com.kaven.rabbitmq.exchange.fanoutExchange;

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

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

public class FanoutConsumer {

    // 自己服务器的IP
    private static String ip = "IP";
    // RabbitMQ启动的默认端口,也是应用程序进行连接RabbitMQ的端口
    private static int port = 5672;
    // RabbitMQ有一个 "/" 的虚拟主机
    private static String virtualHost = "/";

    // fanout exchange ,RabbitMQ提供的fanout exchange
    private static String exchangeName = "amq.fanout";
    // exchange type
    private static String exchangeType= "fanout";
    // 队列名
    private static String queueName = "queue";
    // 队列与交换机绑定的routingKey
    private static String routingKey = "test";

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        // 1 创建一个连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(ip);
        connectionFactory.setPort(port);
        connectionFactory.setVirtualHost(virtualHost);

        // 2 创建连接
        Connection connection = connectionFactory.newConnection();

        // 3 创建Channel
        Channel channel = connection.createChannel();

        // 4 定义Queue ,将Queue绑定到direct exchange
        channel.queueDeclare(queueName,true , false , false , null);
        channel.queueBind(queueName , exchangeName , routingKey);

        // 5 创建消费者
        QueueingConsumer consumer = new QueueingConsumer(channel);

        // 6 设置
        channel.basicConsume(queueName , true , consumer);

        // 7 接收消息
        while(true){
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String msg = new String(delivery.getBody());
            System.out.println(msg);
        }
    }
}

test

As used here is RabbitMQ provided to us fanout exchange, so we do not need their own definition.
Because the switch has been defined, so either before starting the production side or end consumer, the consumer side can successfully receive the message.
Consumer end output is as follows:

RabbitMQ:Fanout Exchange 发送数据

Look RabbitMQ Management.
Here Insert Picture Description

Published 298 original articles · won praise 424 · views 710 000 +

Guess you like

Origin blog.csdn.net/qq_37960603/article/details/104265608