java.lang.IllegalStateException: LettuceConnectionFactory was destroyed and cannot be used anymore

解决方案:lettuce提供了校验连接的方法,lettuce提供了校验连接的方法 只是默认没开启 开启的话是每次获取连接都会校验。可以定时校验来解决 或者开启获取连接的校验
1.定时校验来解决

/*
每隔2秒校验异常lettuce连接是否正常,解决长期空闲lettuce连接关闭但是netty不能及时监控到的问题
*/
@author hujun
/
@Component
@Slf4j
public class LettuceConnectionValidTask {
@Autowired
private RedisConnectionFactory redisConnectionFactory;

    @Scheduled(cron="0/2 * * * * ?")

    public void task() {

        if(redisConnectionFactory instanceof LettuceConnectionFactory){

            LettuceConnectionFactory c=(LettuceConnectionFactory)redisConnectionFactory;

            c.validateConnection();

        }

    }

}

2.开启获取连接的校验

@Component
@Slf4j
public class LettuceConnectionValidConfig implements InitializingBean {
@Autowired
private RedisConnectionFactory redisConnectionFactory;


@Override

public void afterPropertiesSet() throws Exception {

    if(redisConnectionFactory instanceof LettuceConnectionFactory){

        LettuceConnectionFactory c=(LettuceConnectionFactory)redisConnectionFactory;

        c.setValidateConnection(true);

    }

}

}

你可能感兴趣的:(java,servlet,spring)