0

我正在使用netty和redis(jedis客户端),并且在每个请求中redisdb调用的查询方法,当我使用这个命令在Apache基准测试中测试它时

ab -c 10 -n 10 本地主机:2080

发生以下错误。

    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)

这是代码

public class redisdb {
    Jedis jedis;

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

    public void query() {

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

    }
}
4

2 回答 2

2

相当疯狂的猜测:在多线程环境中,您应该使用Jedis 文档中描述的连接池。

代码如下所示:

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);
        }
    }
}

并且不要忘记在关闭应用程序时关闭连接:

pool.destroy();
于 2014-03-10T13:00:40.970 回答
0

您需要线程安全的 JedisPool。如果你使用的是 Java7,你可以使用 try-with-resource 因为 Jedis 类在最新版本中实现了 Closable

于 2015-01-21T06:26:39.450 回答