yshop3.0-RC2版本
This commit is contained in:
@ -5,12 +5,25 @@
|
||||
<parent>
|
||||
<artifactId>yshop</artifactId>
|
||||
<groupId>co.yixiang</groupId>
|
||||
<version>3.0-alpha</version>
|
||||
<version>3.0-RC2</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>yshop-message</artifactId>
|
||||
<name>消息队列模块</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>co.yixiang</groupId>
|
||||
<artifactId>yshop-mall</artifactId>
|
||||
<version>3.0-RC2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.rocketmq</groupId>
|
||||
<artifactId>rocketmq-spring-boot-starter</artifactId>
|
||||
<version>2.0.4</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright (C) 2018-2020
|
||||
* All rights reserved, Designed By www.yixiang.co
|
||||
* 注意:
|
||||
* 本软件为www.yixiang.co开发研制,未经购买不得使用
|
||||
* 购买后可获得全部源代码(禁止转卖、分享、上传到码云、github等开源平台)
|
||||
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
|
||||
*/
|
||||
package co.yixiang.message.redis.config;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
|
||||
/**
|
||||
* reids相关配置
|
||||
* @author hupeng
|
||||
* @since 2020-02-27
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "spring.redis")
|
||||
public class RedisConfigProperties {
|
||||
|
||||
private String host = "host";
|
||||
private String port = "port";
|
||||
private String password = "password";
|
||||
private String database = "database";
|
||||
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
/**
|
||||
* Copyright (C) 2018-2020
|
||||
* All rights reserved, Designed By www.yixiang.co
|
||||
* 注意:
|
||||
* 本软件为www.yixiang.co开发研制,未经购买不得使用
|
||||
* 购买后可获得全部源代码(禁止转卖、分享、上传到码云、github等开源平台)
|
||||
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
|
||||
*/
|
||||
package co.yixiang.message.redis.config;
|
||||
|
||||
import co.yixiang.modules.shop.domain.YxSystemConfig;
|
||||
import co.yixiang.modules.shop.service.YxSystemConfigService;
|
||||
import co.yixiang.utils.RedisUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* api服务启动初始化reids
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||
public class RedisKeyInitialization implements CommandLineRunner {
|
||||
|
||||
|
||||
private final YxSystemConfigService systemConfigService;
|
||||
|
||||
|
||||
private final RedisUtils redisUtils;
|
||||
|
||||
|
||||
/**
|
||||
* 初始化redis
|
||||
*/
|
||||
private void redisKeyInitialization(){
|
||||
try {
|
||||
List<YxSystemConfig> systemConfigs = systemConfigService.list();
|
||||
for (YxSystemConfig systemConfig : systemConfigs) {
|
||||
redisUtils.set(systemConfig.getMenuName(),systemConfig.getValue());
|
||||
}
|
||||
|
||||
log.info("---------------redisKey初始化成功---------------");
|
||||
}catch (Exception e){
|
||||
log.error("redisKey初始化失败: {}",e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
this.redisKeyInitialization();
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Copyright (C) 2018-2020
|
||||
* All rights reserved, Designed By www.yixiang.co
|
||||
* 注意:
|
||||
* 本软件为www.yixiang.co开发研制,未经购买不得使用
|
||||
* 购买后可获得全部源代码(禁止转卖、分享、上传到码云、github等开源平台)
|
||||
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
|
||||
*/
|
||||
package co.yixiang.message.redis.config;
|
||||
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import co.yixiang.message.redis.listener.RedisKeyExpirationListener;
|
||||
import co.yixiang.modules.activity.service.YxStorePinkService;
|
||||
import co.yixiang.modules.order.service.YxStoreOrderService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.listener.PatternTopic;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
|
||||
/**
|
||||
* redis监听配置
|
||||
* @author hupeng
|
||||
* @since 2020-02-27
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
@AllArgsConstructor
|
||||
public class RedisListenerConfig {
|
||||
|
||||
private final RedisTemplate<String, String> redisTemplate;
|
||||
private final RedisConfigProperties redisConfigProperties;
|
||||
private final YxStoreOrderService storeOrderService;
|
||||
private final YxStorePinkService storePinkService;
|
||||
|
||||
@Bean
|
||||
RedisMessageListenerContainer container(RedisConnectionFactory factory) {
|
||||
String topic =StrUtil.format("__keyevent@{}__:expired", redisConfigProperties.getDatabase());
|
||||
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
||||
container.setConnectionFactory(factory);
|
||||
container.addMessageListener(new RedisKeyExpirationListener(redisTemplate,redisConfigProperties
|
||||
,storeOrderService,storePinkService), new PatternTopic(topic));
|
||||
return container;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,111 @@
|
||||
/**
|
||||
* Copyright (C) 2018-2020
|
||||
* All rights reserved, Designed By www.yixiang.co
|
||||
* 注意:
|
||||
* 本软件为www.yixiang.co开发研制,未经购买不得使用
|
||||
* 购买后可获得全部源代码(禁止转卖、分享、上传到码云、github等开源平台)
|
||||
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
|
||||
*/
|
||||
package co.yixiang.message.redis.listener;
|
||||
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import co.yixiang.constant.ShopConstants;
|
||||
import co.yixiang.enums.OrderInfoEnum;
|
||||
import co.yixiang.enums.ShopCommonEnum;
|
||||
import co.yixiang.message.redis.config.RedisConfigProperties;
|
||||
import co.yixiang.modules.activity.domain.YxStorePink;
|
||||
import co.yixiang.modules.activity.service.YxStorePinkService;
|
||||
import co.yixiang.modules.order.domain.YxStoreOrder;
|
||||
import co.yixiang.modules.order.service.YxStoreOrderService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.connection.Message;
|
||||
import org.springframework.data.redis.connection.MessageListener;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* redis过期监听
|
||||
* @author hupeng
|
||||
* @since 2020-02-27
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class RedisKeyExpirationListener implements MessageListener {
|
||||
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
private RedisConfigProperties redisConfigProperties;
|
||||
private YxStoreOrderService storeOrderService;
|
||||
private YxStorePinkService storePinkService;
|
||||
|
||||
public RedisKeyExpirationListener(RedisTemplate<String, String> redisTemplate,
|
||||
RedisConfigProperties redisConfigProperties,
|
||||
YxStoreOrderService storeOrderService,
|
||||
YxStorePinkService storePinkService){
|
||||
this.redisTemplate = redisTemplate;
|
||||
this.redisConfigProperties = redisConfigProperties;
|
||||
this.storeOrderService = storeOrderService;
|
||||
this.storePinkService = storePinkService;
|
||||
}
|
||||
@Override
|
||||
public void onMessage(Message message, byte[] bytes) {
|
||||
RedisSerializer<?> serializer = redisTemplate.getValueSerializer();
|
||||
String channel = String.valueOf(serializer.deserialize(message.getChannel()));
|
||||
String body = String.valueOf(serializer.deserialize(message.getBody()));
|
||||
//key过期监听
|
||||
if(StrUtil.format("__keyevent@{}__:expired", redisConfigProperties.getDatabase()).equals(channel)){
|
||||
//订单自动取消
|
||||
if(body.contains(ShopConstants.REDIS_ORDER_OUTTIME_UNPAY)) {
|
||||
body = body.replace(ShopConstants.REDIS_ORDER_OUTTIME_UNPAY, "");
|
||||
log.info("body:{}",body);
|
||||
String orderId = body;
|
||||
YxStoreOrder order = storeOrderService.getOne(new QueryWrapper<YxStoreOrder>().lambda()
|
||||
.eq(YxStoreOrder::getId, orderId)
|
||||
.eq(YxStoreOrder::getPaid, OrderInfoEnum.PAY_STATUS_0.getValue()));
|
||||
//只有待支付的订单能取消
|
||||
if(order != null){
|
||||
storeOrderService.cancelOrder(order.getOrderId(),null);
|
||||
log.info("订单id:{},未在规定时间支付取消成功",body);
|
||||
}
|
||||
}
|
||||
//订单自动收货
|
||||
if(body.contains(ShopConstants.REDIS_ORDER_OUTTIME_UNCONFIRM)) {
|
||||
body = body.replace(ShopConstants.REDIS_ORDER_OUTTIME_UNCONFIRM, "");
|
||||
log.info("body:{}",body);
|
||||
String orderId = body;
|
||||
YxStoreOrder order = storeOrderService.getOne(new QueryWrapper<YxStoreOrder>().lambda()
|
||||
.eq(YxStoreOrder::getId, orderId)
|
||||
.eq(YxStoreOrder::getPaid,OrderInfoEnum.PAY_STATUS_1.getValue())
|
||||
.eq(YxStoreOrder::getStatus,OrderInfoEnum.STATUS_1.getValue()));
|
||||
|
||||
//只有待收货的订单能收货
|
||||
if(order != null){
|
||||
storeOrderService.takeOrder(order.getOrderId(),null);
|
||||
log.info("订单id:{},自动收货成功",body);
|
||||
}
|
||||
}
|
||||
|
||||
//拼团过期取消
|
||||
if(body.contains(ShopConstants.REDIS_PINK_CANCEL_KEY)) {
|
||||
body = body.replace(ShopConstants.REDIS_PINK_CANCEL_KEY, "");
|
||||
log.info("body:{}",body);
|
||||
String pinkId = body;
|
||||
YxStorePink storePink = storePinkService.getOne(Wrappers.<YxStorePink>lambdaQuery()
|
||||
.eq(YxStorePink::getId,pinkId)
|
||||
.eq(YxStorePink::getStatus,OrderInfoEnum.PINK_STATUS_1.getValue())
|
||||
.eq(YxStorePink::getIsRefund,OrderInfoEnum.PINK_REFUND_STATUS_0.getValue()));
|
||||
|
||||
//取消拼团
|
||||
if(storePink != null){
|
||||
storePinkService.removePink(storePink.getUid(),storePink.getCid(),storePink.getId());
|
||||
log.info("拼团订单id:{},未在规定时间完成取消成功",body);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -1,49 +1,61 @@
|
||||
package co.yixiang.message.rocketmq;//package co.yixiang.commonold.rocketmq;
|
||||
//
|
||||
//import cn.hutool.core.util.ObjectUtil;
|
||||
//import co.yixiang.modules.order.entity.YxStoreOrder;
|
||||
//import co.yixiang.modules.order.service.YxStoreOrderService;
|
||||
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
//import lombok.AllArgsConstructor;
|
||||
//import lombok.extern.slf4j.Slf4j;
|
||||
//import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
|
||||
//import org.apache.rocketmq.spring.core.RocketMQListener;
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.stereotype.Component;
|
||||
//
|
||||
///**
|
||||
// * @ClassName 消费者
|
||||
// * @Author hupeng <610796224@qq.com>
|
||||
// * @Date 2020/1/1
|
||||
// **/
|
||||
////@Component
|
||||
////@RocketMQMessageListener(
|
||||
//// topic = "yshop-topic",
|
||||
//// consumerGroup = "yshop-group",
|
||||
//// selectorExpression = "*"
|
||||
////)
|
||||
//@Slf4j
|
||||
//@AllArgsConstructor
|
||||
//public class MqConsumer implements RocketMQListener<String> {
|
||||
//
|
||||
// private final YxStoreOrderService storeOrderService;
|
||||
//
|
||||
// @Override
|
||||
// public void onMessage(String msg) {
|
||||
// log.info("系统开始处理延时任务---订单超时未付款---订单id:" + msg);
|
||||
//
|
||||
// Integer id = Integer.valueOf(msg);
|
||||
//
|
||||
// YxStoreOrder order = storeOrderService.getOne(new QueryWrapper<YxStoreOrder>()
|
||||
// .eq("id", id).eq("is_del",0).eq("paid",0));
|
||||
//
|
||||
// if(ObjectUtil.isNull(order)) {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// storeOrderService.cancelOrderByTask(id);
|
||||
//
|
||||
// log.info("=====处理成功======");
|
||||
//
|
||||
// }
|
||||
//}
|
||||
/**
|
||||
* Copyright (C) 2018-2020
|
||||
* All rights reserved, Designed By www.yixiang.co
|
||||
* 注意:
|
||||
* 本软件为www.yixiang.co开发研制,未经购买不得使用
|
||||
* 购买后可获得全部源代码(禁止转卖、分享、上传到码云、github等开源平台)
|
||||
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
|
||||
*/
|
||||
package co.yixiang.message.rocketmq;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
|
||||
import co.yixiang.enums.OrderInfoEnum;
|
||||
import co.yixiang.modules.order.domain.YxStoreOrder;
|
||||
import co.yixiang.modules.order.service.YxStoreOrderService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
|
||||
import org.apache.rocketmq.spring.core.RocketMQListener;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @ClassName 消费者
|
||||
* @Author hupeng <610796224@qq.com>
|
||||
* @Date 2020/1/1
|
||||
**/
|
||||
//@Component
|
||||
//@RocketMQMessageListener(
|
||||
// topic = "yshop-topic",
|
||||
// consumerGroup = "yshop-group",
|
||||
// selectorExpression = "*"
|
||||
//)
|
||||
@Slf4j
|
||||
@AllArgsConstructor
|
||||
public class MqConsumer implements RocketMQListener<String> {
|
||||
|
||||
private final YxStoreOrderService storeOrderService;
|
||||
|
||||
@Override
|
||||
public void onMessage(String msg) {
|
||||
log.info("系统开始处理延时任务---订单超时未付款---订单id:" + msg);
|
||||
|
||||
Long id = Long.valueOf(msg);
|
||||
|
||||
YxStoreOrder order = storeOrderService.lambdaQuery()
|
||||
.eq(YxStoreOrder::getId, id)
|
||||
.eq(YxStoreOrder::getPaid, OrderInfoEnum.PAY_STATUS_0.getValue())
|
||||
.one();
|
||||
|
||||
//只有待支付的订单能取消
|
||||
if(order != null){
|
||||
storeOrderService.cancelOrder(order.getOrderId(),null);
|
||||
log.info("订单id:{},未在规定时间支付取消成功",id);
|
||||
}
|
||||
|
||||
log.info("=====处理成功======");
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,51 +1,59 @@
|
||||
package co.yixiang.message.rocketmq;//package co.yixiang.commonold.rocketmq;
|
||||
//
|
||||
//import co.yixiang.exception.ErrorRequestException;
|
||||
//import lombok.AllArgsConstructor;
|
||||
//import org.apache.rocketmq.client.exception.MQBrokerException;
|
||||
//import org.apache.rocketmq.client.exception.MQClientException;
|
||||
//import org.apache.rocketmq.client.producer.DefaultMQProducer;
|
||||
//import org.apache.rocketmq.commonold.message.Message;
|
||||
//import org.apache.rocketmq.remoting.exception.RemotingException;
|
||||
//import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.stereotype.Component;
|
||||
//
|
||||
///**
|
||||
// * @ClassName 生成者
|
||||
// * @Author hupeng <610796224@qq.com>
|
||||
// * @Date 2020/1/1
|
||||
// **/
|
||||
////@Component
|
||||
//@AllArgsConstructor
|
||||
//public class MqProducer {
|
||||
// //注入rocketMQ的模板
|
||||
// private final RocketMQTemplate rocketMQTemplate;
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * 发送延时消息10分钟
|
||||
// *
|
||||
// * @param topic
|
||||
// * @param msg
|
||||
// */
|
||||
// public void sendMsg(String topic, String msg) {
|
||||
// DefaultMQProducer defaultMQProducer = rocketMQTemplate.getProducer();
|
||||
//
|
||||
// Message message = new Message(topic,msg.getBytes());
|
||||
// message.setDelayTimeLevel(14);
|
||||
//
|
||||
// try {
|
||||
// defaultMQProducer.send(message);
|
||||
// } catch (MQClientException e) {
|
||||
// throw new ErrorRequestException("RocketMQ服务没启动哦");
|
||||
// } catch (RemotingException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (MQBrokerException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (InterruptedException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//}
|
||||
/**
|
||||
* Copyright (C) 2018-2020
|
||||
* All rights reserved, Designed By www.yixiang.co
|
||||
* 注意:
|
||||
* 本软件为www.yixiang.co开发研制,未经购买不得使用
|
||||
* 购买后可获得全部源代码(禁止转卖、分享、上传到码云、github等开源平台)
|
||||
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
|
||||
*/
|
||||
package co.yixiang.message.rocketmq;
|
||||
|
||||
import co.yixiang.exception.ErrorRequestException;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.apache.rocketmq.client.exception.MQBrokerException;
|
||||
import org.apache.rocketmq.client.exception.MQClientException;
|
||||
import org.apache.rocketmq.client.producer.DefaultMQProducer;
|
||||
import org.apache.rocketmq.common.message.Message;
|
||||
import org.apache.rocketmq.remoting.exception.RemotingException;
|
||||
import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @ClassName 生成者
|
||||
* @Author hupeng <610796224@qq.com>
|
||||
* @Date 2020/1/1
|
||||
**/
|
||||
//@Component
|
||||
@AllArgsConstructor
|
||||
public class MqProducer {
|
||||
//注入rocketMQ的模板
|
||||
private final RocketMQTemplate rocketMQTemplate;
|
||||
|
||||
|
||||
/**
|
||||
* 发送延时消息10分钟
|
||||
*
|
||||
* @param topic 主题
|
||||
* @param msg 消息
|
||||
*/
|
||||
public void sendMsg(String topic, String msg) {
|
||||
DefaultMQProducer defaultMQProducer = rocketMQTemplate.getProducer();
|
||||
|
||||
Message message = new Message(topic,msg.getBytes());
|
||||
message.setDelayTimeLevel(14);
|
||||
|
||||
try {
|
||||
defaultMQProducer.send(message);
|
||||
} catch (MQClientException e) {
|
||||
throw new ErrorRequestException("RocketMQ服务没启动哦");
|
||||
} catch (RemotingException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MQBrokerException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user