赞
踩
SpringAMQP提供的Exchange接口
- package com.itheima.consumer.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;
-
- @Configuration
- public class FanoutConfiguration {
-
- //生成一个fanoutExchange交互机(hmall,fanout2)
- @Bean
- public FanoutExchange fanoutExchange(){
- // ExchangeBuilder.fanoutExchange("").build();
- return new FanoutExchange("hmall.fanout2");
- }
-
- //生成一个新的队列fanout.queue3
- @Bean
- public Queue fanoutQueue3(){
- // QueueBuilder.durable("ff").build();
- return new Queue("fanout.queue3");
- }
-
- //将队列fanoutQueue3绑定到fanoutExchange交互机
- @Bean
- public Binding fanoutBinding3(Queue fanoutQueue3, FanoutExchange fanoutExchange){
- return BindingBuilder.bind(fanoutQueue3).to(fanoutExchange);
- }
-
- //生成一个新的队列fanout.queue4
- @Bean
- public Queue fanoutQueue4(){
- return new Queue("fanout.queue4");
- }
-
- //将队列fanoutQueue4绑定到fanoutExchange交互机
- @Bean
- public Binding fanoutBinding4(){
- return BindingBuilder.bind(fanoutQueue4()).to(fanoutExchange());
- }
- }
同理于Fanout交换机
- package com.itheima.consumer.config;
-
- import org.springframework.amqp.core.Binding;
- import org.springframework.amqp.core.BindingBuilder;
- import org.springframework.amqp.core.DirectExchange;
- import org.springframework.amqp.core.Queue;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- @Configuration
- public class DirectConfiguration {
-
- @Bean
- public DirectExchange directExchange(){
- return new DirectExchange("hmall.direct");
- }
-
- @Bean
- public Queue directQueue1(){
- return new Queue("direct.queue1");
- }
-
- @Bean
- public Binding directQueue1BindingRed(Queue directQueue1, DirectExchange directExchange){
- return BindingBuilder.bind(directQueue1).to(directExchange).with("red");
- }
-
- @Bean
- public Binding directQueue1BindingBlue(Queue directQueue1, DirectExchange directExchange){
- return BindingBuilder.bind(directQueue1).to(directExchange).with("blue");
- }
-
- @Bean
- public Queue directQueue2(){
- return new Queue("direct.queue2");
- }
-
- @Bean
- public Binding directQueue2BindingRed(Queue directQueue2, DirectExchange directExchange){
- return BindingBuilder.bind(directQueue2).to(directExchange).with("red");
- }
-
- @Bean
- public Binding directQueue2BindingBlue(Queue directQueue2, DirectExchange directExchange){
- return BindingBuilder.bind(directQueue2).to(directExchange).with("yellow");
- }
-
- }
- @RabbitListener(bindings = @QueueBinding(
- value = @Queue(name = "direct.queue1", durable = "true"),
- exchange = @Exchange(name = "hmall.direct", type = ExchangeTypes.DIRECT),
- key = {"red", "blue"}
- ))
- public void listenDirectQueue1(String msg) throws InterruptedException {
- System.out.println("消费者1 收到了 direct.queue1的消息:【" + msg +"】");
- }
-
- @RabbitListener(bindings = @QueueBinding(
- value = @Queue(name = "direct.queue2", durable = "true"),
- exchange = @Exchange(name = "hmall.direct", type = ExchangeTypes.DIRECT),
- key = {"red", "yellow"}
- ))
- public void listenDirectQueue2(String msg) throws InterruptedException {
- System.out.println("消费者2 收到了 direct.queue2的消息:【" + msg +"】");
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。