一. Redis失效机制
Reids中的key设置失效时间后,Redis到一定时间会失效;java中提供了KeyExpirationEventMessageListener 这个监听类,为我们提供了RedisKeyExpirationListener方法来监听过期的Redis Key,当key过期时,会进入该方法。
二. 应用场景
在我们日常工作中,很多流程是需要等待的,比如A系统和B系统进行交互;A系统首先向B系统发送请求,B系统接收到请求,并处理返回处理结果最多需要最多30s,A系统需要判定30s内B系统是否返回正常数据过来,在无法使用长连接的情况下,需要在发送请求之后做30s的定时器。
- A向B发送请求,即存标识到redis中,失效时间设置为30s;
- 如果30s内,B成功返回所需数据回来,则删除该key;
- 如果在RedisKeyExpirationListener方法中触发了该key的失效事件,则代表B一直没有返回正常数据;接下来就可以根据业务做一系列的处理。
package com.crcgas.evc.ds.listener;
import com.crcgas.evc.ds.dao.ShChargeDataDao;
import com.crcgas.evc.ds.dto.EvcShChargeInfoDTO;
import com.crcgas.evc.ds.service.CommonServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
/**
* key失效,触发事件
*/
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
private static final Logger logger = LoggerFactory.getLogger(RedisKeyExpirationListener.class);
@Autowired
private ShChargeDataDao shChargeDataDao;
@Autowired
private CommonServiceImpl commonService;
public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
super(listenerContainer);
}
/**
* 针对redis数据失效事件,进行数据处理
*/
@Override
public void onMessage(Message message, byte[] pattern) {
// 用户做自己的业务处理即可,注意message.toString()可以获取失效的key
String expiredKey = message.toString();
if(expiredKey.startsWith("EVC_SHSTART_CHARGE184_")){
//如果是SEND_CHARGE184_开头的key,获取value,更新充电信息表
updateCharge(expiredKey, 1);
logger.info("SEND_CHARGE184_开头的key:" + expiredKey);
} else if (expiredKey.startsWith("EVC_SHSTOP_CHARGE184_")){
updateCharge(expiredKey, 2);
logger.info("EVC_SHSTOP_CHARGE184_开头的key:" + expiredKey);
} else if (expiredKey.startsWith("chargeCard")){
commonService.sendCardBalance(expiredKey.split(":")[1]);
}
}
/**
* 更新充电信息表超时
*/
private void updateCharge(String expiredKey, int flag){
EvcShChargeInfoDTO shChargeInfo = new EvcShChargeInfoDTO();
shChargeInfo.setOrderNo(expiredKey.split("4_")[1]);
if (flag == 1){
shChargeInfo.setStartupSign("2");
shChargeInfo.setStartupFailureReason("超时");
//订单无效
shChargeInfo.setOrderType("2");
} else {
shChargeInfo.setStopSign("2");
shChargeInfo.setStopFailureReason("超时");
}
shChargeDataDao.updateShCharge(shChargeInfo);
}
}