我使用netty和redis (jedis client),在每个请求中使用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");
}
}发布于 2014-03-10 13:00:40
相当疯狂的猜测:在多线程环境中,您应该使用连接池如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();发布于 2015-01-21 06:26:39
您需要线程安全的JedisPool。如果使用的是Java7,则可以使用“使用资源的尝试”,因为Jedis类在最新版本中实现了可关闭的功能。
https://stackoverflow.com/questions/22299673
复制相似问题