简体   繁体   中英

error in Using netty with jedis

I'm using netty and redis (jedis client), and in each request the query method of redisdb call, when i test it in Apache benchmarking with this command

ab -c 10 -n 10 localhost:2080

the below error occurs.

    Mar 10, 2014 3:29:48 PM io.netty.channel.DefaultChannelPipeline$TailHandler exceptionCaught
WARNING: An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
java.lang.NullPointerException
    at redis.clients.jedis.Protocol.sendCommand(Protocol.java:39)
    at redis.clients.jedis.Protocol.sendCommand(Protocol.java:33)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:80)
    at redis.clients.jedis.BinaryClient.append(BinaryClient.java:200)
    at redis.clients.jedis.Client.append(Client.java:125)
    at redis.clients.jedis.Jedis.append(Jedis.java:616)
    at com.kdgames.server.asyncdatabase.redisdb.query(redisdb.java:14)
    at ServerInboundHandler.channelRead0(ServerInboundHandler.java:41)
    at ServerInboundHandler.channelRead0(ServerInboundHandler.java:1)
    at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:103)
    at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:340)
    at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:326)
    at io.netty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86)
    at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:340)
    at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:326)
    at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
    at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:340)
    at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:326)
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:155)
    at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:340)
    at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:326)
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:785)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:116)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:494)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:461)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:378)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:350)
    at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101)
    at java.lang.Thread.run(Unknown Source)

and here is the code

public class redisdb {
    Jedis jedis;

    public redisdb() {
        jedis = new Jedis("192.168.56.101", 6179);
    }

    public void query() {

        jedis.append("foo", "bar"); 

    }
}

Rather wild guess: in a multithreaded environment you should use a connection pool as described in Jedis documentation .

The code would look like:

public class redisdb {
    JedisPool pool;

    public redisdb() {
        pool = new JedisPool(new JedisPoolConfig(), "192.168.56.101", 6179)
    }

    public void query() {
        Jedis jedis = pool.getResource();
        try {
            jedis.append("foo", "bar"); 
        } catch (JedisConnectionException e) {
            // returnBrokenResource when the state of the object is unrecoverable
            if (null != jedis) {
                pool.returnBrokenResource(jedis);
                jedis = null;
            }
        } finally {
          /// ... it's important to return the Jedis instance to the pool once you've finished using it
          if (null != jedis)
            pool.returnResource(jedis);
        }
    }
}

And don't forget to close the connections when closing your application:

pool.destroy();

You need the JedisPool which is thread-safe. And if you're using Java7, you can use try-with-resource because Jedis class implements Closable in the latest version

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM