修正项目
This commit is contained in:
26
xlcs-parent/common/rabbit_util/pom.xml
Normal file
26
xlcs-parent/common/rabbit_util/pom.xml
Normal file
@@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.yovinchen</groupId>
|
||||
<artifactId>common</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>rabbit_util</artifactId>
|
||||
<dependencies>
|
||||
<!--rabbitmq消息队列-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
</project>
|
@@ -0,0 +1,22 @@
|
||||
package com.yovinchen.xlcs.mq.config;
|
||||
|
||||
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
|
||||
import org.springframework.amqp.support.converter.MessageConverter;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* ClassName: MQConfig
|
||||
* Package: com.yovinchen.xlcs.mq.config
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/9/16 23:32
|
||||
*/
|
||||
@Configuration
|
||||
public class MQConfig {
|
||||
|
||||
@Bean
|
||||
public MessageConverter messageConverter() {
|
||||
return new Jackson2JsonMessageConverter();
|
||||
}
|
||||
}
|
@@ -0,0 +1,66 @@
|
||||
package com.yovinchen.xlcs.mq.config;
|
||||
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.rabbit.connection.CorrelationData;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
/**
|
||||
* ClassName: MQProducerAckConfig
|
||||
* Package: com.yovinchen.xlcs.mq.config
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/9/16 23:33
|
||||
*/
|
||||
@Component
|
||||
public class MQProducerAckConfig implements RabbitTemplate.ReturnCallback, RabbitTemplate.ConfirmCallback {
|
||||
|
||||
// 我们发送消息使用的是 private RabbitTemplate rabbitTemplate; 对象
|
||||
// 如果不做设置的话 当前的rabbitTemplate 与当前的配置类没有任何关系!
|
||||
@Autowired
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
|
||||
// 设置 表示修饰一个非静态的void方法,在服务器加载Servlet的时候运行。并且只执行一次!
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
rabbitTemplate.setReturnCallback(this);
|
||||
rabbitTemplate.setConfirmCallback(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 表示消息是否正确发送到了交换机上
|
||||
*
|
||||
* @param correlationData 消息的载体
|
||||
* @param ack 判断是否发送到交换机上
|
||||
* @param cause 原因
|
||||
*/
|
||||
@Override
|
||||
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
|
||||
if (ack) {
|
||||
System.out.println("消息发送成功!");
|
||||
} else {
|
||||
System.out.println("消息发送失败!" + cause);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息如果没有正确发送到队列中,则会走这个方法!如果消息被正常处理,则这个方法不会走!
|
||||
*
|
||||
* @param message
|
||||
* @param replyCode
|
||||
* @param replyText
|
||||
* @param exchange
|
||||
* @param routingKey
|
||||
*/
|
||||
@Override
|
||||
public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
|
||||
System.out.println("消息主体: " + new String(message.getBody()));
|
||||
System.out.println("应答码: " + replyCode);
|
||||
System.out.println("描述:" + replyText);
|
||||
System.out.println("消息使用的交换器 exchange : " + exchange);
|
||||
System.out.println("消息使用的路由键 routing : " + routingKey);
|
||||
}
|
||||
}
|
@@ -0,0 +1,68 @@
|
||||
package com.yovinchen.xlcs.mq.constant;
|
||||
|
||||
/**
|
||||
* ClassName: MqConst
|
||||
* Package: com.yovinchen.xlcs.mq.constant
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/9/16 23:34
|
||||
*/
|
||||
public class MqConst {
|
||||
/**
|
||||
* 消息补偿
|
||||
*/
|
||||
public static final String MQ_KEY_PREFIX = "xlcs.mq:list";
|
||||
public static final int RETRY_COUNT = 3;
|
||||
|
||||
/**
|
||||
* 商品上下架
|
||||
*/
|
||||
public static final String EXCHANGE_GOODS_DIRECT = "xlcs.goods.direct";
|
||||
public static final String ROUTING_GOODS_UPPER = "xlcs.goods.upper";
|
||||
public static final String ROUTING_GOODS_LOWER = "xlcs.goods.lower";
|
||||
//队列
|
||||
public static final String QUEUE_GOODS_UPPER = "xlcs.goods.upper";
|
||||
public static final String QUEUE_GOODS_LOWER = "xlcs.goods.lower";
|
||||
|
||||
/**
|
||||
* 团长上下线
|
||||
*/
|
||||
public static final String EXCHANGE_LEADER_DIRECT = "xlcs.leader.direct";
|
||||
public static final String ROUTING_LEADER_UPPER = "xlcs.leader.upper";
|
||||
public static final String ROUTING_LEADER_LOWER = "xlcs.leader.lower";
|
||||
//队列
|
||||
public static final String QUEUE_LEADER_UPPER = "xlcs.leader.upper";
|
||||
public static final String QUEUE_LEADER_LOWER = "xlcs.leader.lower";
|
||||
|
||||
//订单
|
||||
public static final String EXCHANGE_ORDER_DIRECT = "xlcs.order.direct";
|
||||
public static final String ROUTING_ROLLBACK_STOCK = "xlcs.rollback.stock";
|
||||
public static final String ROUTING_MINUS_STOCK = "xlcs.minus.stock";
|
||||
|
||||
public static final String ROUTING_DELETE_CART = "xlcs.delete.cart";
|
||||
//解锁普通商品库存
|
||||
public static final String QUEUE_ROLLBACK_STOCK = "xlcs.rollback.stock";
|
||||
public static final String QUEUE_SECKILL_ROLLBACK_STOCK = "xlcs.seckill.rollback.stock";
|
||||
public static final String QUEUE_MINUS_STOCK = "xlcs.minus.stock";
|
||||
public static final String QUEUE_DELETE_CART = "xlcs.delete.cart";
|
||||
|
||||
//支付
|
||||
public static final String EXCHANGE_PAY_DIRECT = "xlcs.pay.direct";
|
||||
public static final String ROUTING_PAY_SUCCESS = "xlcs.pay.success";
|
||||
public static final String QUEUE_ORDER_PAY = "xlcs.order.pay";
|
||||
public static final String QUEUE_LEADER_BILL = "xlcs.leader.bill";
|
||||
|
||||
//取消订单
|
||||
public static final String EXCHANGE_CANCEL_ORDER_DIRECT = "xlcs.cancel.order.direct";
|
||||
public static final String ROUTING_CANCEL_ORDER = "xlcs.cancel.order";
|
||||
//延迟取消订单队列
|
||||
public static final String QUEUE_CANCEL_ORDER = "xlcs.cancel.order";
|
||||
|
||||
/**
|
||||
* 定时任务
|
||||
*/
|
||||
public static final String EXCHANGE_DIRECT_TASK = "xlcs.exchange.direct.task";
|
||||
public static final String ROUTING_TASK_23 = "xlcs.task.23";
|
||||
//队列
|
||||
public static final String QUEUE_TASK_23 = "xlcs.queue.task.23";
|
||||
}
|
@@ -0,0 +1,56 @@
|
||||
package com.yovinchen.xlcs.mq.service;
|
||||
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* ClassName: RabbitService
|
||||
* Package: com.yovinchen.xlcs.common.service
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/9/16 18:12
|
||||
*/
|
||||
@Service
|
||||
public class RabbitService {
|
||||
|
||||
// 引入操作rabbitmq 的模板
|
||||
@Autowired
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
*
|
||||
* @param exchange 交换机
|
||||
* @param routingKey 路由键
|
||||
* @param message 消息
|
||||
* @return
|
||||
*/
|
||||
public boolean sendMessage(String exchange, String routingKey, Object message) {
|
||||
// 调用发送数据的方法
|
||||
rabbitTemplate.convertAndSend(exchange, routingKey, message);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送延迟消息的方法
|
||||
*
|
||||
* @param exchange 交换机
|
||||
* @param routingKey 路由键
|
||||
* @param message 消息内容
|
||||
* @param delayTime 延迟时间
|
||||
* @return
|
||||
*/
|
||||
public boolean sendDelayMessage(String exchange, String routingKey, Object message, int delayTime) {
|
||||
|
||||
// 在发送消息的时候设置延迟时间
|
||||
rabbitTemplate.convertAndSend(exchange, routingKey, message, message1 -> {
|
||||
// 设置一个延迟时间
|
||||
message1
|
||||
.getMessageProperties()
|
||||
.setDelay(delayTime * 1000);
|
||||
return message1;
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user