rabbitmq 在springboot框架中添加多个exchange的方法
Tut5Config.java文件.
import org.springframework.amqp.core.AnonymousQueue; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.TopicExchange; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class Tut5Config { @Bean public TopicExchange vobcTopic() { return new TopicExchange("topic.cu2ats"); } @Bean public TopicExchange clientTopic() { return new TopicExchange("topic.serv2cli"); } @Bean public TopicExchange ats2cuTopic() { return new TopicExchange("topic.ats2cu"); } /*@Profile("receiver")*/ private static class ReceiverConfig { @Bean public Tut5Receiver receiver() { return new Tut5Receiver(); } @Bean public Queue autoDeleteQueue1() { return new AnonymousQueue(); } @Bean public Binding binding1a(@Qualifier("vobcTopic") TopicExchange topic, Queue autoDeleteQueue1) { return BindingBuilder.bind(autoDeleteQueue1).to(topic).with("cu2ats.vobc.*.status"); } } @Bean public Tut5Sender sender() { return new Tut5Sender(); } / }
Tut5Sender.java中
public class Tut5Sender { @Autowired private TrainIDService trainIDService; @Autowired private RabbitTemplate template; @Autowired() @Qualifier("ats2cuTopic") private TopicExchange ats2cuTopic; @Autowired() @Qualifier("clientTopic") private TopicExchange clientTopic; private int index; private final String[] keys = {"ats2cu.vobc.command", "serv2cli.traintrace.trainIdentifyId"}; @Scheduled(fixedDelay = 1000, initialDelay = 500) public void send() throws JsonProcessingException { StringBuilder builder = new StringBuilder(); if (++this.index == keys.length) { this.index = 0; } String key = keys[this.index]; //builder.append(key).append( ); String json=null; if(key.equals("ats2cu.vobc.command")){ json = trainIDService.Ats2vbc(); if(!json.equals("")&&json!=null){ builder.append(json).append( ); String message = builder.toString(); template.convertAndSend(ats2cuTopic.getName(), key, message); System.out.println(" [x] ats2cuTopicSent " + message + ""); } } else if(key.equals("serv2cli.traintrace.trainIdentifyId")){ json = trainIDService.getTrainTrack(); if(!json.equals("")&&json!=null){ builder.append(json).append( ); String message = builder.toString(); template.convertAndSend(clientTopic.getName(), key, message); //System.out.println(" [x] clientTopicSent " + message + ""); } } } }
@Qualifier注解,qualifier的意思是合格者,通过这个标示,表明了哪个bean才是我们所需要的
Tut5Sender.java中的@Qualifier("ats2cuTopic")对应Tu5Config.java中的
Tut5Sender.java @Bean public TopicExchange ats2cuTopic() { return new TopicExchange("topic.ats2cu"); }
如果不加@Qualifier("ats2cuTopic"),在程序运行会报有两个bean的错
同理,在接收的时候
Tut5Sender.java @Bean public Binding binding1a(@Qualifier("vobcTopic")TopicExchange topic, Queue autoDeleteQueue1) { return BindingBuilder.bind(autoDeleteQueue1).to(topic).with("cu2ats.vobc.*.status"); }
这里必须标明是哪个topicExchange 否则程序无法自己找到是山歌exchange中的哪个exchange 就会报错