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)
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[0]);
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?