本文介绍了Jedis java.lang.Long无法强制转换为[B的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到"java.lang.ClassCastException:运行此代码时无法将java.lang.Long强制转换为[B]:
I got "java.lang.ClassCastException: java.lang.Long cannot be cast to [B" while running this code:
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(10);
config.setMinIdle(1);
config.setMaxWaitMillis(30000);
JedisPool jedisPool = new JedisPool(config, "localhost", 6379);
Jedis jedis = null;
jedis = jedisPool.getResource();
String msisdn = "3331122333";
Long balance = new Long(1000);
int balanceValidity = 30;
Transaction t = jedis.multi();
t.watch(msisdn);
t.set(msisdn, balance.toString());
t.expire(msisdn, balanceValidity);
t.exec();
运行此代码,一切正常:
Everythings works fine running this code:
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.watch(msisdn);
jedis.set(msisdn, balance.toString());
jedis.expire(msisdn, balanceValidity);
jedis.publish("myChannel", msisdn + " " + balance.toString());
} finally {
if (jedis != null)
jedis.close();
}
这些二手货:
- Redis 3.2.4
- Jedis客户端2.9.0
- JDK 1.6
推荐答案
Jedis不是线程安全的.
Jedis is not thread safe.
每次从池中获取jedis实例.然后关闭那个jedis实例,以便它返回到池中
get the jedis instance every time from the pool when you do some operation. and after that close jedis instance so that it goes back to the pool
这篇关于Jedis java.lang.Long无法强制转换为[B的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!