专题1234导航

Spring整合Jedis分片 空指针异常

来源:互联网
发表时间:2017/4/21 16:51:44
责任编辑:李志喜
字体:

为了帮助网友解决“Spring整合Jedis分片 空指针异常”相关的问题,学网通过互联网对“Spring整合Jedis分片 空指针异常”相关的解决方案进行了整理,用户详细问题包括:RedisSpring分片空异常这是抛出的异常
严重: Servlet.service() for servlet TestServlet threw exception
java.lang.NullPointerException
at base.redis.RedisClientTemplateImpl.set(RedisClientTemplateImpl.java:49)
at test.web.TestServlet.doPost(TestServlet.java:27)
at test.web.TestServlet.doGet(TestServlet.java:18)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)

下面贴出我的代码:

点击图片看大图学网 www.xue163.com



spring-redis.xml

 <context:property-placeholder location="classpath*:redis.properties"
    ignore-unresolvable="true" />
   
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal">
        <value>${redis.pool.maxActive}</value>
        </property>
        <property name="maxIdle">
         <value>${redis.pool.maxIdle}</value>
        </property>
        <property name="testOnBorrow" value="true"/>
        <property name="testOnReturn" value="true"/>
    </bean>
   
     <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool"  scope="singleton">
        <constructor-arg index="0" ref="jedisPoolConfig" />
        <constructor-arg index="1">
            <list>
                <bean class="redis.clients.jedis.JedisShardInfo">
                    <constructor-arg name="host" value="${redis.uri}" />
                </bean>
            </list>
        </constructor-arg>
    </bean>
    <bean id="redisDataSource" class="base.redis.RedisDataSourceImpl">
    <property name="shardedJedisPool"></property>
    </bean>
    <bean id="redisClientTemplate" class="base.redis.RedisClientTemplateImpl">
    <property name="redisDataSource"></property>
    </bean>


RedisDataSourseImpl
private static final Logger log = Logger.getLogger(RedisDataSourceImpl.class);

private ShardedJedisPool shardedJedisPool;

public ShardedJedisPool getShardedJedisPool() {
return shardedJedisPool;
}
public void setShardedJedisPool(ShardedJedisPool shardedJedisPool) {
this.shardedJedisPool = shardedJedisPool;
}
/**
 * @return ShardedJedis
 * <获取redis客户端连接,执行命令>
 */
public ShardedJedis getRedisClient() {
System.out.println("执行getResource()前");
    try {
        ShardedJedis shardedJedis = shardedJedisPool.getResource();
        System.out.println("执行getResource()后");
        return shardedJedis;
    } catch (Exception e) {
        log.error("获取redis客户端连接失败",e);
    }
    return null;
}
/**
 * 
 * @param shardedJedis
 * <将资源返还个pool>
 */
public void returnResource(ShardedJedis shardedJedis) {

    shardedJedisPool.close();

}
/**
 * 
 * @param shardedJedis
 * <出现异常后,将资源返还给pool>
 */ 
public void returnResource(ShardedJedis shardedJedis, boolean broken) {

    if(broken){
     shardedJedisPool.close();
    }else{
     shardedJedisPool.close();
    }

}

RedisClientTemplateImpl
private static final Logger log = Logger.getLogger(RedisClientTemplateImpl.class);

    private RedisDataSource redisDataSource;

public RedisDataSource getRedisDataSource() {
return redisDataSource;
}
public void setRedisDataSource(RedisDataSource redisDataSource) {
this.redisDataSource = redisDataSource;
}
    public void disconnect() {
    System.out.println("disconnect()断开连接");
        ShardedJedis shardedJedis = redisDataSource.getRedisClient();
        shardedJedis.disconnect();
    }
    public String set(String key, String value) {
        String result = null;
        System.out.println("set方法");
        ShardedJedis shardedJedis = redisDataSource.getRedisClient();
        System.out.println("getRedisClient()执行后");
        if (shardedJedis == null) {
            return result;
        }
        boolean broken = false;
        try {
            result = shardedJedis.set(key, value);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            broken = true;
        } finally {
            redisDataSource.returnResource(shardedJedis, broken);
        }
        return result;
    }
TestServlet测试方法
RedisClientTemplateImpl redisClient = new RedisClientTemplateImpl();
        String value = redisClient.set("yaya", "123156644");
        System.out.println("获取到的值:"+value);
        response.sendRedirect("index.jsp");

都有注入为什么抛出空异常,试了很多方法都不行,求大神帮忙解答一下,多分,具体解决方案如下:

解决方案1:
没人回复吗?

www.xue163.com true http://www.xue163.com/588880/39215/392158356.html report 10761 Spring整合Jedis分片 空指针异常:解决方案1:没人回复吗?...
相关信息
最近关注
首页推荐
热门图片
最新添加资讯
24小时热门资讯
精彩资讯
精彩推荐
热点推荐
真视界
精彩图片
社区精粹
关于本站 | 广告服务 | 手机版 | 商务合作 | 免责申明 | 招聘信息 | 联系我们
Copyright © 2004-2016 Xue163.com All Rights Reserved. 学网 版权所有
京ICP备10044368号-1 京公网安备11010802011102号