Jedis instance works strange to me in the single_thread environment

40 views
Skip to first unread message

jedis_r...@163.com

unread,
Jan 4, 2015, 9:30:55 AM1/4/15
to jedis_redis
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[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?



Nils Kilden-Pedersen

unread,
Jan 4, 2015, 10:05:36 AM1/4/15
to jedis...@googlegroups.com

Why are you calling sync()? I don’t even know what it does, since it’s only documented as an internal command.


--
You received this message because you are subscribed to the Google Groups "Jedis" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jedis_redis...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages