1. 程式人生 > >rabbitmq 在springboot框架中新增多個exchange的方法

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  就會報錯

Tut5Sender.java