RabbitMqConfig配置类
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import cn.ivg.framework.comp.rabbitmq.service.RabbitMqSend;
@Configuration
public class RabbitMqConfig {
@Autowired
private RabbitTemplate rabbitTemplate;
@Bean
public RabbitMqSend rabbitMqSend() {
return new RabbitMqSend(rabbitTemplate);
}
}
工具类RabbitMqSend
import java.util.UUID;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class RabbitMqSend implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {
private RabbitTemplate rabbitTemplate;
/************ Send_exchanges ***********************/
private static final String topic_exchange = "fitting_invengo_exchange";
private static final String cashierExchange = "cashier_invengo_exchange";
public RabbitMqSend(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
rabbitTemplate.setConfirmCallback(this);// 最后设置内容
}
public RabbitAdmin createAdmin() {
return new RabbitAdmin(this.rabbitTemplate.getConnectionFactory());
}
public void bindQueue(String queueName, String routingKey) {
RabbitAdmin admin = createAdmin();
Queue queue = new Queue(queueName);
admin.declareQueue(queue);
TopicExchange topicExchange = new TopicExchange(topic_exchange);
admin.declareExchange(topicExchange);
Binding binding = BindingBuilder.bind(queue).to(topicExchange).with(routingKey);
admin.declareBinding(binding);
log.info("创建队列名:{},创建routingKey:{}", queueName, routingKey);
// admin.setAutoStartup(true);
}
public boolean deleteQueue(String queueName) {
RabbitAdmin admin = createAdmin();
boolean result = admin.deleteQueue(queueName);
if (result) {
log.info("删除队列{}成功", queueName);
} else {
log.info("删除队列{}失败", queueName);
}
return result;
}
public void removeBinding(String queueName, String routingKey) {
RabbitAdmin admin = createAdmin();
Queue queue = new Queue(queueName);
TopicExchange topicExchange = new TopicExchange(topic_exchange);
Binding binding = BindingBuilder.bind(queue).to(topicExchange).with(routingKey);
admin.removeBinding(binding);
}
public void sendMsg(String routingKey, Object msg) {
this.sendMsgTemplate(topic_exchange, routingKey, msg);
}
public void sendMsgTemplate(String exchange, String routingKey, Object msg) {
rabbitTemplate.convertAndSend(exchange, routingKey, msg, new CorrelationData(UUID.randomUUID().toString()));
}
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
if (correlationData == null) {
return;
}
log.debug("send id:" + correlationData.getId());
if (ack) {// 调用成功
log.warn(correlationData.getId() + ":" + "发送成功.");
} else {
log.warn(correlationData.getId() + ":" + "发送失败.");
}
}
@Override
public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
log.info("send message failed:" + "{message:" + message + ",replyCode:" + replyCode + ",replyText:" + replyText
+ ",exchange" + exchange + ",routingKey" + routingKey + "}");
}
public void sendCashierMsg(String routingKey,String msg) {
sendMsgTemplate(cashierExchange, routingKey, msg);
}
}