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);
    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章