Rabbit MQ加上@Configuration配置,在mq上没有队列和exhange的时候,自动创建队列和exchange
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@Configuration
public class MQReceiver {
private static final String EXCHANGE_ADMIN_NOTIFY = "adminNotify2";
private static final String QUEURE_ADMIN_MESSAGE = "admin-message2";
@Bean
public Queue queueMessage() {
return new Queue(QUEURE_ADMIN_MESSAGE, false);
}
@Bean
public DirectExchange exchange() {
return new DirectExchange(EXCHANGE_ADMIN_NOTIFY, false, false);
}
@Bean
public Binding bindingExchangeMessage() {
return BindingBuilder.bind(queueMessage()).to(exchange()).with("");
}
@Autowired
private NotifyService notifyService;
@RabbitHandler
@RabbitListener(queues = QUEURE_ADMIN_MESSAGE)
public void process(Message msg) {
try {
log.debug("===>>> MQ listen message {} ", msg.toString());
} catch (Exception e) {
log.error("MQReceiver ====> ", e);
}
}
}
网友评论