赞
踩
广播模式
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
@Bean
public Queue fanout_a(){
return new Queue("fanout.a");
}
@Bean
public Queue fanout_b(){
return new Queue("fanout.b");
}
@Bean
FanoutExchange fanoutExchange(){
return new FanoutExchange("fanoutExchange");
}
@Bean
Binding bindingFanoutExchangeA(Queue fanout_a,FanoutExchange fanoutExchange){
return BindingBuilder.bind(fanout_a).to(fanoutExchange);
}
@Bean
Binding bindingFanoutExchangeB(Queue fanout_b,FanoutExchange fanoutExchange){
return BindingBuilder.bind(fanout_b).to(fanoutExchange);
}
@Component
@RabbitListener(queues = "fanout.a")
public class FanoutReceiverA {
@RabbitHandler
public void process(String message){
System.out.println("接收者A:"+message);
}
}
@Component
@RabbitListener(queues = "fanout.b")
public class FanoutReceiverB {
@RabbitHandler
public void process(String message){
System.out.println("接收者B:"+message);
}
}
@Component
public class FanoutSender {
@Autowired
private AmqpTemplate amqpTemplate;
public void send(String exchange,String message){
amqpTemplate.convertAndSend(exchange,"",message);
}
}
fanoutSender.send("fanoutExchange","hello");
//运行结果: 接收者B:hello 接收者A:hello
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。