rabbitmq详解第二天(消息类型一Fanout 订阅模式)

不明白的可以参考 rabbitmq详解第一天(消息类型一回调模式)

fanout(订阅) 即 fanout类型的Exchange可以将producer 发送的消息绑定到所有订阅的队列中去. 即发布/订阅机制

配置文件 

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;

@Configuration
public class FanoutConfig {
    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }

    @Bean
    Binding bindingExchangeA(Queue AMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(AMessage).to(fanoutExchange);
    }

    @Bean
    Binding bindingExchangeB(Queue BMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(BMessage).to(fanoutExchange);
    }

    @Bean
    Binding bindingExchangeC(Queue CMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(CMessage).to(fanoutExchange);
    }

    @Bean
    public Queue AMessage() {
        return new Queue("fanout.A");
    }

    @Bean
    public Queue BMessage() {
        return new Queue("fanout.B");
    }

    @Bean
    public Queue CMessage() {
        return new Queue("fanout.C");
    }
}

生产者

/**
 * Fanout Exchange 生产者
 */
@Component
public class FanoutProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(){
//论证广播模式和routingkey参数无关
        rabbitTemplate.convertAndSend("fanoutExchange","abcd.ee","--------------");
        rabbitTemplate.convertAndSend("fanoutExchange","abcd.aa","==============");
        rabbitTemplate.convertAndSend("fanoutExchange","***************");
    }
}

 

消费者

/**
 * Fanout Exchange 消费者
 */
@Component
@RabbitListener(queues = "fanout.A")
public class FanoutCustomerA {

    @RabbitHandler
    public void process(String msg){
        System.out.println("FanoutReceiverA  : " + msg);
    }
}


/**
 * Fanout Exchange 消费者
 */
@Component
@RabbitListener(queues = "fanout.B")
public class FanoutCustomerB {

    @RabbitHandler
    public void process(String msg){
        System.out.println("FanoutReceiverB  : " + msg);
    }
}


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

/**
 * Fanout Exchange 消费者
 */
@Component
@RabbitListener(queues = "fanout.C")
public class FanoutCustomerC {

    @RabbitHandler
    public void process(String msg){
        System.out.println("FanoutReceiverC  : " + msg);
    }
}

测试方法 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FanoutController {
    @Autowired
    private FanoutProducer fanoutProducer;

    @RequestMapping("/fanoutsend")
    public void fanoutSend(){
        fanoutProducer.send();
    }
}

 

你可能感兴趣的:(mq)