RabbitMQ之Exchanges(交换机)

RabbitMQ 分为 生产者, 交换机, 消费者
交换机类型: direct, topic, headers, fanout.

生产者, 向交换机发送消息, 再由交换机, 向所有订阅这个交换机的消费者广播消息

生产者

package com.yuchen.demo.exchange;

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

public class Send {

    // 队列名称
    private static final String EXCHANGE_NAME = "lsy_exchange";

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

        // 创建连接连接到MabbitMQ
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("10.13.144.24");
        factory.setVirtualHost("/");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
        // 新建连接
        Connection connection = factory.newConnection();
        // 建立通道
        Channel channel = connection.createChannel();

        // 创建交换机
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");

        String message = "Hello Exchange";
        channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
        System.out.println("发送成功: '" + message + "'");

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


    }
}

消费者A

package com.yuchen.demo.exchange;

import java.io.IOException;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class MessageConsumerA {

    // 队列名称
    private static final String EXCHANGE_NAME = "lsy_exchange";

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

        // 创建连接连接到MabbitMQ
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("10.13.144.24");
        factory.setVirtualHost("/");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
        // 新建连接
        Connection connection = factory.newConnection();
        // 建立通道
        Channel channel = connection.createChannel();

        // 声明队列, 可能在发送方之前启动接收方, 确保队列存在。
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        String queueName = channel.queueDeclare().getQueue();
        channel.queueBind(queueName, EXCHANGE_NAME, "");
        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("[A] 接收到远端消息: '" + message + "'");
            }
        };
        channel.basicConsume(queueName, true, consumer);
    }

}

消费者B

package com.yuchen.demo.exchange;

import java.io.IOException;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class MessageConsumerB {

    // 队列名称
    private static final String EXCHANGE_NAME = "lsy_exchange";

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

        // 创建连接连接到MabbitMQ
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("10.13.144.24");
        factory.setVirtualHost("/");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
        // 新建连接
        Connection connection = factory.newConnection();
        // 建立通道
        Channel channel = connection.createChannel();

        // 声明队列, 可能在发送方之前启动接收方, 确保队列存在。
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        String queueName = channel.queueDeclare().getQueue();
        // 绑定交换机
        channel.queueBind(queueName, EXCHANGE_NAME, "");

        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("[B] 接收到远端消息: '" + message + "'");
            }
        };
        channel.basicConsume(queueName, true, consumer);
    }

}

你可能感兴趣的:(RabbitMQ)