rabbitmq-Fanout exchange(扇形交换器)

Fanout exchange

扇形交换器,以广播的形式发送,不管消费者是否有相匹配的绑定键,都会收到消息。就像你买了商品A,人家还硬要把商品B都送给你,买一送一。

我们具体看下实例:

生产者

发送消息:

Send apple:Hello,RabbitMq1
Send orange:Hello,RabbitMq2
Send pear:Hello,RabbitMq3

package com.enjoy.testrabbitmq;

import com.enjoy.common.RabbitmqConnection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

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

/**

 *类说明:fanout类型交换器的生产者
 */
public class TestProducerFanout {

    public final static String EXCHANGE_NAME = "fanout_exchange";

    public static void main(String[] args)
            throws IOException, TimeoutException {
        /* 创建连接,连接到RabbitMQ*/
        Connection connection = RabbitmqConnection.getConnection();

        /*创建信道*/
        Channel channel = connection.createChannel();
        /*创建fanout交换器*/
        channel.exchangeDeclare(EXCHANGE_NAME,"fanout");

        /*日志消息级别,作为路由键使用*/
        String[] routekeys = {"apple","orange","pear"};
        for(int i=0;i<3;i++){
            String routekey = routekeys[i%3];
            String msg = "Hello,RabbitMq"+(i+1);
            /*发布消息,需要参数:交换器,路由键,其中以日志消息级别为路由键*/
            channel.basicPublish(EXCHANGE_NAME,routekey,null,
                    msg.getBytes());
            System.out.println("Send "+routekey+":"+msg);

        }
        channel.close();
        connection.close();

    }

}

消费者:

绑定键other,但是确收到了生产者发送的全部消息

waiting for message........
Received[apple]Hello,RabbitMq1
Received[orange]Hello,RabbitMq2
Received[pear]Hello,RabbitMq3

package com.enjoy.testrabbitmq;

import com.enjoy.common.RabbitmqConnection;
import com.rabbitmq.client.*;

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

/**

 *类说明:普通的消费者
 */
public class TestConsumerFanout {

    public static void main(String[] argv)
            throws IOException, TimeoutException {
        /* 创建连接,连接到RabbitMQ*/
        Connection connection = RabbitmqConnection.getConnection();

        final Channel channel = connection.createChannel();
        channel.exchangeDeclare(TestProducerFanout.EXCHANGE_NAME,
                "fanout");

        /*声明一个队列*/
        String queueName = channel.queueDeclare().getQueue().substring(4);
        channel.queueDeclare(queueName,false,false,
                false,null);

        /*绑定,将队列和交换器通过路由键进行绑定*/
        String routekey = "other";
        channel.queueBind(queueName,TestProducerFanout.EXCHANGE_NAME,routekey);

        System.out.println("waiting for message........");

        /*声明了一个消费者*/
        final 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("Received["+envelope.getRoutingKey()
                        +"]"+message);
            }
        };
        /*消费者正式开始在指定队列上消费消息*/
        channel.basicConsume(queueName,true,consumer);
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值