RabbitMQ实战篇:Fanout - 扇形交换机

前两篇我们已经初步学习了rabbitmq的使用,这一篇主要讲Fanout的使用,如果对Fanout扇形交换机还不太明白的话,建议看一下我的第一篇《RabbitMQ实战篇:开篇思维导图》里面有详细介绍各个交换机类型。

那么我们就直接上代码了:

我们配置了2个Queue ,绑定到同一个exchange中

package com.lwl.rabbitmq.config;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.lwl.rabbitmq.constant.Constants;

/**
 * 发送消息 配置发送消息的队列queue
 * @author lwl
 * @create 2018年8月10日 下午2:37:38
 * @version 1.0
 */
@Configuration
public class SendMessageConfig {

    @Bean
    public Queue fanoutQueue() {
    	return new Queue(Constants.FANOUT_QUEUE);
    }

    
    @Bean
    FanoutExchange exchange() {
        return new FanoutExchange(Constants.FANOUT_NAME);
    }
    
    /**
     * 使用扇型交换机,routekey 
     * @param queueMessage
     * @param exchange
     * @return
     * @author lwl
     * @create 2019年6月14日 上午10:51:21
     */
    @Bean
    Binding bindingExchangeMessage(Queue fanoutQueue, FanoutExchange  exchange) {
        return BindingBuilder.bind(fanoutQueue).to(exchange);
    }
    
    
    /**
     * 第二个队列
     * @return
     * @author lwl
     * @create 2019年6月14日 下午2:11:31
     */
    @Bean
    public Queue fanoutTwoQueue() {
    	return new Queue(Constants.FANOUT_QUEUE_TWO);
    }
    
    @Bean
    Binding bindingExchangeMessageTwo(Queue fanoutTwoQueue, FanoutExchange  exchange) {
    	return BindingBuilder.bind(fanoutTwoQueue).to(exchange);
    }
}

看一下我们的生产者:

package com.lwl.rabbitmq.producer;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lwl.rabbitmq.constant.Constants;

/**
 * 生成者
 * @author lwl
 * @create 2019年6月14日 上午10:56:41
 * @version 1.0
 */
@Component
public class Producer {

	@Autowired
    private AmqpTemplate template;

    /**
     * 使用扇型交换机
     * @param message
     * @author lwl
     * @create 2019年6月14日 上午10:54:54
     */
    public void send(Object message){
    	template.convertAndSend(Constants.FANOUT_NAME,null,message);
    }
    
	
}

再看一下我们的消费者,由于有2个队列,使用有2个消费者

package com.lwl.rabbitmq.consumer;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import com.lwl.rabbitmq.constant.Constants;

/**
 * 消费者
 * @author lwl
 * @create 2019年6月14日 上午10:57:11
 * @version 1.0
 */
@Component
@RabbitListener(queues = Constants.FANOUT_QUEUE)
public class FanoutConsumer {

	@RabbitHandler
    public void process(String hello) {
		System.out.println();
		System.out.println("-----------------------客户端  1  收到数据 -----------------------");
        System.out.println(Constants.FANOUT_QUEUE+ " --> Receiver1  : " + hello);
        System.out.println();
    }
	
}
package com.lwl.rabbitmq.consumer;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import com.lwl.rabbitmq.constant.Constants;

/**
 * 消费者
 * @author lwl
 * @create 2019年6月14日 上午10:57:11
 * @version 1.0
 */
@Component
@RabbitListener(queues = Constants.FANOUT_QUEUE_TWO)
public class FanoutConsumer4 {

	@RabbitHandler
    public void process(String hello) {
		System.out.println();
		System.out.println("-----------------------客户端  4  收到数据 -----------------------");
        System.out.println(Constants.FANOUT_QUEUE_TWO+ " --> Receiver4  : " + hello);
        System.out.println();
    }
	
}

接下来就是我们的测试用例了:

	/**
	 * 发送消息(此时把FanoutConsumer,FanoutConsumer2 ,FanoutConsumer3, FanoutConsumer4  注释掉)
	 * @author lwl
	 * @create 2019年6月14日 下午1:04:10
	 */
	@Test
	public void sendMessage() {
		String message = "我是Fanout 发送的消息22222";
		producer.send(message);
		System.out.println("--------------------------------发送完毕--------------------------------");
		System.out.println();
	}
	

我们看到fanout 2个队列都收到消息,因为这2个queue都绑定到exchange上。

	/**
	 * 接受消息(此时把FanoutConsumer,FanoutConsumer2 ,FanoutConsumer3, FanoutConsumer4  打开)
	 * @author lwl
	 * @create 2019年6月14日 下午1:04:10
	 */
	@Test
	public void getMessage() {
		System.out.println("-------------------------------接受数据--------------------------------");
	}

运行结果:

-----------------------客户端  3  收到数据 -----------------------
fanout_queue_2 --> Receiver3  : 我是Fanout 发送的消息22222


-----------------------客户端  2  收到数据 -----------------------
fanout_queue --> Receiver2  : 我是Fanout 发送的消息22222

我们看到,同一个queue 只能有一个人接收到,但是不同的queue可以相互不影响

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值