redis统一异常处理,通过继承CachingConfigurerSupport覆写errorHandler()

annkee058 2017-11-14 11:02:59
我的代码如下:

package com.ctsig.parking.base.redis.exception;

import org.springframework.cache.interceptor.AbstractCacheInvoker;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class RedisConfig extends CachingConfigurerSupport {

protected final Logger LOG = LoggerFactory.getLogger(RedisConfig.class);
/**
* redis异常处理
*
* @return
*/
@Bean
@Override
public CacheErrorHandler errorHandler() {

CacheErrorHandler cacheErrorHandler = new CacheErrorHandler() {
@Override
public void handleCacheGetError(RuntimeException e, Cache cache, Object o) {
System.out.println(("---handleCacheGetError:" + cache.getName()));

}

@Override
public void handleCachePutError(RuntimeException e, Cache cache, Object o, Object o1) {
System.out.println(("---handleCachePutError:" + cache.getName()));
}

@Override
public void handleCacheEvictError(RuntimeException e, Cache cache, Object o) {
System.out.println(("---handleCacheEvictError:" + cache.getName()));
}

@Override
public void handleCacheClearError(RuntimeException e, Cache cache) {
System.out.println(("---handleCacheClearError:" + cache.getName()));
}
};
return cacheErrorHandler;
}
}

===========
看的一篇博客http://blog.csdn.net/tianyaleixiaowu/article/details/70670329,通过此方法配置后,如果redis出现异常就会进入errorHandler()中的某个方法,使得程序能够继续执行下去查询数据库而不是中断。
那么问题来了,我的也同样跟此链接博主代码一样,通过在配置文件故意配置错误连接信息,但是还是会报异常,而没有进入统一异常处理器。
怎么破?求大神。。。。。。
...全文
1870 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
时光呢 2019-12-24
  • 打赏
  • 举报
回复
引用 楼主 annkee058 的回复:
我的代码如下: package com.ctsig.parking.base.redis.exception; import org.springframework.cache.interceptor.AbstractCacheInvoker; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.interceptor.CacheErrorHandler; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import javax.annotation.Resource; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; @Configuration public class RedisConfig extends CachingConfigurerSupport { protected final Logger LOG = LoggerFactory.getLogger(RedisConfig.class); /** * redis异常处理 * * @return */ @Bean @Override public CacheErrorHandler errorHandler() { CacheErrorHandler cacheErrorHandler = new CacheErrorHandler() { @Override public void handleCacheGetError(RuntimeException e, Cache cache, Object o) { System.out.println(("---handleCacheGetError:" + cache.getName())); } @Override public void handleCachePutError(RuntimeException e, Cache cache, Object o, Object o1) { System.out.println(("---handleCachePutError:" + cache.getName())); } @Override public void handleCacheEvictError(RuntimeException e, Cache cache, Object o) { System.out.println(("---handleCacheEvictError:" + cache.getName())); } @Override public void handleCacheClearError(RuntimeException e, Cache cache) { System.out.println(("---handleCacheClearError:" + cache.getName())); } }; return cacheErrorHandler; } } =========== 看的一篇博客http://blog.csdn.net/tianyaleixiaowu/article/details/70670329,通过此方法配置后,如果redis出现异常就会进入errorHandler()中的某个方法,使得程序能够继续执行下去查询数据库而不是中断。 那么问题来了,我的也同样跟此链接博主代码一样,通过在配置文件故意配置错误连接信息,但是还是会报异常,而没有进入统一异常处理器。 怎么破?求大神。。。。。。
1.楼主说的是配置错误的连接信息,而楼主参照的该篇博客是为了针对已经连上redis的情况做的异常处理 2.springboot加载配置类也有先后顺序,如果连接配置在异常拦截配置前加载,有异常拦截当然也不会生效
Sunyiban 2017-11-14
  • 打赏
  • 举报
回复
引用 2 楼 followshadow 的回复:
引用 1 楼 u011594442 的回复:
他创建redis连接的方法不也写在这里面了么~~或许和这个有关吧~
测试了,跟他这个连接方法无关,真是见怪了
那就是说他这个你就不能用呗~~
annkee058 2017-11-14
  • 打赏
  • 举报
回复
引用 1 楼 u011594442 的回复:
他创建redis连接的方法不也写在这里面了么~~或许和这个有关吧~
测试了,跟他这个连接方法无关,真是见怪了
Sunyiban 2017-11-14
  • 打赏
  • 举报
回复
他创建redis连接的方法不也写在这里面了么~~或许和这个有关吧~

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧