Redis
Java原创redisredis过期监听大约 2 分钟约 464 字
redis过期监听
RedisListenerConfig
import cn.hutool.core.util.StrUtil;
import com.wechat.common.config.RedisConfigProperties;
import com.wechat.mall.listener.RedisKeyExpirationListener;
import com.wechat.mall.service.OrderInfoService;
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;
/**
* @author www.wechat.com
*/
@Configuration
@AllArgsConstructor
public class RedisListenerConfig {
private final RedisTemplate<String, String> redisTemplate;
private final RedisConfigProperties redisConfigProperties;
private final OrderInfoService orderInfoService;
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.addMessageListener(new RedisKeyExpirationListener(redisTemplate, redisConfigProperties, orderInfoService), new PatternTopic(StrUtil.format("__keyevent@{}__:expired", redisConfigProperties.getDatabase())));
return container;
}
}
RedisKeyExpirationListener
import cn.hutool.core.util.StrUtil;
import com.wechat.mall.config.CommonConstants;
import com.wechat.common.config.RedisConfigProperties;
import com.wechat.mall.constant.MallConstants;
import com.wechat.mall.entity.OrderInfo;
import com.wechat.mall.enums.OrderInfoEnum;
import com.wechat.mall.service.OrderInfoService;
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过期监听
* 1、自动取消订单
* 2、自动收货
*/
@Component
public class RedisKeyExpirationListener implements MessageListener {
private RedisTemplate<String, String> redisTemplate;
private RedisConfigProperties redisConfigProperties;
private OrderInfoService orderInfoService;
public RedisKeyExpirationListener(RedisTemplate<String, String> redisTemplate,
RedisConfigProperties redisConfigProperties,
OrderInfoService orderInfoService){
this.redisTemplate = redisTemplate;
this.redisConfigProperties = redisConfigProperties;
this.orderInfoService = orderInfoService;
}
@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(MallConstants.REDIS_ORDER_KEY_IS_PAY_0)) {
body = body.replace(MallConstants.REDIS_ORDER_KEY_IS_PAY_0, "");
String[] str = body.split(":");
String wxOrderId = str[1];
OrderInfo orderInfo = orderInfoService.getById(wxOrderId);
if(orderInfo != null && CommonConstants.NO.equals(orderInfo.getIsPay())){//只有待支付的订单能取消
orderInfoService.orderCancel(orderInfo);
}
}
//订单自动收货
if(body.contains(MallConstants.REDIS_ORDER_KEY_STATUS_2)) {
body = body.replace(MallConstants.REDIS_ORDER_KEY_STATUS_2, "");
String[] str = body.split(":");
String orderId = str[1];
OrderInfo orderInfo = orderInfoService.getById(orderId);
if(orderInfo != null && OrderInfoEnum.STATUS_2.getValue().equals(orderInfo.getStatus())){//只有待收货的订单能收货
orderInfoService.orderReceive(orderInfo);
}
}
}
}
}
OrderInfoService
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult;
import com.wechat.mall.dto.PlaceOrderDTO;
import com.wechat.mall.entity.OrderInfo;
import com.wechat.mall.entity.OrderItem;
import java.io.Serializable;
/**
* 商城订单
*
* @author JL
* @date 2019-09-10 15:21:22
*/
public interface OrderInfoService extends IService<OrderInfo> {
IPage<OrderInfo> page1(IPage<OrderInfo> page, Wrapper<OrderInfo> queryWrapper);
/**
* 下单
* @param placeOrderDTO
*/
OrderInfo orderSub(PlaceOrderDTO placeOrderDTO);
IPage<OrderInfo> page2(IPage<OrderInfo> page, OrderInfo orderInfo);
OrderInfo getById2(Serializable id);
/**
* 取消订单
* @param orderInfo
*/
void orderCancel(OrderInfo orderInfo);
/**
* 订单收货
* @param orderInfo
*/
void orderReceive(OrderInfo orderInfo);
/**
* 处理订单回调
* @param orderInfo
*/
void notifyOrder(OrderInfo orderInfo);
/**
* 发起退款
* @param orderItem
*/
void saveRefunds(OrderItem orderItem);
/**
* 操作退款
* @param orderItem
*/
void doOrderRefunds(OrderItem orderItem);
/**
* 退款回调
* @param notifyResult
*/
void notifyRefunds(WxPayRefundNotifyResult notifyResult);
}