redis从2.1.0升级到2.6.0报错:java.lang.NoSuchMethodError

在将项目中的jedis从2.1.0升级到2.6.0时,初始化JedisPool遇到构造函数缺失错误。原因是新版本中JedisPool的构造函数发生了变化。解决方案是更新相关依赖,如commons-pool2,确保使用正确构造函数创建JedisPool实例。
项目中使用了jedis-2.1.0.jar,现在升级为jedis-2.6.0,在初始化资源池时,总是报如下错误。
private JedisPool pool = new JedisPool(config, host, port, timeout, null, dbIndex); //这句报错。

[quote]Caused by: java.lang.NoSuchMethodError: redis.clients.jedis.JedisPool.<init>(Lorg/apache/commons/pool/impl/GenericObjectPool$Config;Ljava/lang/String;IILjava/lang/String;)V[/quote]

原因是因为:在2.6.0版本中,JedisPool缺少对应参数的构造函数,所以报上述错误,代码如下
jedisPool = new JedisPool(getPoolConfig(), redisServerHost, redisServerPort, timeout, redisAuthKey);


ommons-pool2-2.2.jar、jedis-2.6.0 都引入项目中了。

private static void initialPool()
{
ResourceBundle rb = ResourceBundle.getBundle("init");
// 数据库,默认为0,目前总共为16个库
// 读取配置文件中的数据库,默认为0,目前总共为16个库
int dbIndex = Integer.parseInt(rb.getString("redis.dbindex"));
// 读取配置文件中的IP地址
String host = rb.getString("redis.host");
// 读取配置文件中的端口号
int port = Integer.parseInt(rb.getString("redis.port"));
// 读取配置文件中的超时时间
int timeout = Integer.parseInt(rb.getString("redis.timeout"));
// 最大分配的对象数
int maxActive = Integer.parseInt(rb.getString("redis.maxActive"));
// 最大空闲数,数据库连接的最大空闲时间。超过空闲时间,数据库连接将被标记为不可用,然后被释放。设为0表示无限制
int maxIdle = Integer.parseInt(rb.getString("redis.maxIdle"));
// 当池内没有返回对象时,最大等待时间
int maxWait = Integer.parseInt(rb.getString("redis.maxWait"));
// 当调用borrow Object方法时,是否进行有效性检查
String testOnBorrow = rb.getString("redis.testOnBorrow");
boolean onBorrow = false;
if (testOnBorrow.equals("true"))
{
onBorrow = true;
}

org.apache.commons.pool2.impl.GenericObjectPoolConfig config = new org.apache.commons.pool2.impl.GenericObjectPoolConfig ();
//连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
config.setBlockWhenExhausted(true);

//设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)
config.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy");

//是否启用pool的jmx管理功能, 默认true
config.setJmxEnabled(true);

//MBean ObjectName = new ObjectName("org.apache.commons.pool2:type=GenericObjectPool,name=" + "pool" + i); 默认为"pool", JMX不熟,具体不知道是干啥的...默认就好.
config.setJmxNamePrefix("pool");

//是否启用后进先出, 默认true
config.setLifo(true);

//最大空闲连接数, 默认8个
config.setMaxIdle(maxIdle);

//最大连接数, 默认8个
config.setMaxTotal(maxActive);

//获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1
config.setMaxWaitMillis(maxWait);

//逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
config.setMinEvictableIdleTimeMillis(1800000);

//最小空闲连接数, 默认0
config.setMinIdle(0);

//每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
config.setNumTestsPerEvictionRun(3);

//对象空闲多久后逐出, 当空闲时间>该值 且 空闲连接>最大空闲数 时直接逐出,不再根据MinEvictableIdleTimeMillis判断 (默认逐出策略)
config.setSoftMinEvictableIdleTimeMillis(1800000);

//在获取连接的时候检查有效性, 默认false
config.setTestOnBorrow(false);

//在空闲时检查有效性, 默认false
config.setTestWhileIdle(false);

//逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
config.setTimeBetweenEvictionRunsMillis(-1);

private JedisPool pool = new JedisPool(config, host, port, timeout, null, dbIndex);
}

Caused by: java.lang.NoSuchMethodError: redis.clients.jedis.JedisPool.<init>(Lorg/apache/commons/pool2/impl/GenericObjectPoolConfig;Ljava/lang/String;IILjava/lang/String;I)V
at com.zk.common.util.Redis.initialPool(Redis.java:126)
at com.zk.common.util.Redis.getJedis(Redis.java:162)
at com.zk.common.util.Redis.lrange(Redis.java:813)
at com.zk.adms.router.AdmsDataRouter.polling(AdmsDataRouter.java:192)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:273)
at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:264)
... 3 more

参考:http://bbs.youkuaiyun.com/topics/390887579
你遇到的错误: ``` java.lang.NullPointerException at redis.clients.jedis.BinaryJedis$SetFromList.toString(BinaryJedis.java:3568) ``` 表明在使用 Jedis 客户端操作 Redis 时,某个 `SetFromList` 对象在调用 `toString()` 方法时内部存在 `null` 值导致空指针异常。 ### 可能原因分析: 1. **返回结果为 null** 你可能调用了类似 `jedis.smembers(key)` 或 `jedis.lrange(key, 0, -1)` 等方法,而该 key 在 Redis 中不存在,导致返回的 Set 或 List 为 `null`,或者其中包含 `null` 元素。 2. **手动构造了包含 null 的集合对象** 如果你手动创建了类似 `new BinaryJedis.SetFromList(null)`,那么在调用 `toString()` 时会触发这个异常。 3. **Jedis 版本问题** 某些老版本的 Jedis 在处理集合转换为字符串时没有做空值检查,导致 `toString()` 报 NPE。 ### 解决方案: - **检查 key 是否存在** 在操作前先使用 `jedis.exists(key)` 判断 key 是否存在。 - **对返回值做 null 判断** ```java Set<String> members = jedis.smembers("your_key"); if (members != null && !members.isEmpty()) { System.out.println(members); } else { System.out.println("No members found."); } ``` - **升级 Jedis 版本** 如果你使用的是较旧版本的 Jedis(如 2.x),建议升级到最新版本(如 4.x),修复了大量兼容性和 NPE 问题。 - **避免直接调用 toString()** 如果不确定集合内容是否安全,避免直接调用 `toString()`,改用遍历打印或日志记录。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值