jedis instance 表现很奇怪
The error is:
[B cannot be cast to java.lang.Long
at redis.clients.jedis.Connection.getIntegerReply(Connection.java:201)
at redis.clients.jedis.Jedis.hset(Jedis.java:642)
As I read the issue 'https://github.com/xetorthio/jedis/issues/122' So I know the pool.getResource() may be the correct way to use in production.
But there is still something I do not know about the jedis instance because I cannot figure out what the mean with the "multithreded environment".
Let me show My test code:
ImportMain.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.io.File;
import java.io.IOException;
public class ImportMain {
private static Logger logger = LoggerFactory.getLogger(ImportMain.class);
public static void main(String args) {
Importer importer = new Importer();
try {
File file = new File(args);
if (file.isDirectory()) {
File files = file.listFiles();
for (File f : files) {
if(f.isHidden()) {
continue;
}
importer.process(f);
}
} else if (file.isFile() && !file.isHidden()) {
importer.process(file);
} else {
System.exit(-1);
}
} catch (IOException e) {
logger.error(e.getMessage() ,e);
System.exit(-1);
}
}
}
Importer.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import java.io.*;
public class Importer {
private static Logger logger = LoggerFactory.getLogger(Importer.class);
private Jedis jedis;
public Importer() {
jedis = new Jedis("localhost");
logger.info("------{}---------", jedis.toString());
}
public void process(File file) throws IOException {
logger.info("start to process file:{}", file.getAbsoluteFile());
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
String line = null;
long total = 0L;
while ((line = br.readLine()) != null) {
total++;
jedis.hset("test::test", line, "1");
}
jedis.sync();
logger.info("total record:{}", total);
}
}
Now,we can run: ImportMain.java , the only argument I gived is "/tmp/A"
the directory structure:
---/tmp/A
/1.log
/2.log
Then it will run error!
It is unbelievable, right? I have never use any multi-threaded way to use the jedis instance, but It still works wrong, I just want to know why.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
When I attempt to use the JedisPool, I also encoutered some problems:
The default maxIdle is 8, So if I have 9 files under the dircetory, It will still run wrong the same as before, I return the resource by call 'pool.returnResource(jedis);'
But if I return the resource by call 'pool.returnBrokenResource(jedis);' it work fine. It seems that the later is to create a new jedis instance not the one of eight.
Can I ask that the later is the correct usage in this circumstance?
As I understand is : If the connection is not alive in the 15minutes(default), the redis-server will close the connection, and then this connection could be called broken?
没有找到相关结果
已邀请:
1 个回复
zp0824 - 好好学习,天天向上
at redis.clients.jedis.Connection.getIntegerReply(Connection.java:201)
at redis.clients.jedis.Jedis.hset(Jedis.java:642)
你现在写的这个测试代码比较复杂,debug 起来难。
如果你是刚开始学习 Redis 的话,建议从最简单的代码开始尝试,不要一开始就上太复杂的代码,特别是这些涉及 IO 和字符 parse 的。。。最容易写错了。
Jedis 的测试套件里面,提供了很多不错的使用实例,你可以去参考一下: https://github.com/xetorthio/jedis/blob/master/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java