超时问题
Posted KoKo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了超时问题相关的知识,希望对你有一定的参考价值。
package org.zln.netty.tout.server; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; import io.netty.handler.codec.DelimiterBasedFrameDecoder; import io.netty.handler.codec.Delimiters; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; import io.netty.handler.timeout.IdleStateHandler; import io.netty.handler.timeout.WriteTimeoutHandler; import io.netty.util.CharsetUtil; import java.util.concurrent.TimeUnit; /** * Created by sherry on 16/12/15. */ public class TimeoutServerInit extends ChannelInitializer<SocketChannel> { private static final int READ_IDEL_TIME_OUT = 4; // 读超时 - 客户端连接后不写数据 private static final int WRITE_IDEL_TIME_OUT = 1;// 写超时 private static final int ALL_IDEL_TIME_OUT = 2; // 所有超时 @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("timeoutHandler",new IdleStateHandler(READ_IDEL_TIME_OUT,WRITE_IDEL_TIME_OUT, ALL_IDEL_TIME_OUT, TimeUnit.SECONDS)); // pipeline.addLast("readtimeout",new ReadTimeoutHandler(4)); // pipeline.addLast("writetimeout",new WriteTimeoutHandler(2)); pipeline.addLast("framer", new DelimiterBasedFrameDecoder(1024, Delimiters.lineDelimiter() )); pipeline.addLast("decoder",new StringDecoder(CharsetUtil.UTF_8)); pipeline.addLast("encoder",new StringEncoder(CharsetUtil.UTF_8)); pipeline.addLast("handler",new TimeoutServerHandler()); } } package org.zln.netty.tout.client; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; import io.netty.handler.codec.DelimiterBasedFrameDecoder; import io.netty.handler.codec.Delimiters; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; import io.netty.handler.timeout.IdleStateHandler; import io.netty.util.CharsetUtil; import java.util.concurrent.TimeUnit; /** * Created by sherry on 16/12/15. */ public class TimeoutClientInit extends ChannelInitializer<SocketChannel> { private static final int READ_IDEL_TIME_OUT = 10; // 读超时 - 多久没从服务器获取到返回数据 private static final int WRITE_IDEL_TIME_OUT = 50;// 写超时 - 连接到服务器后,多久没有写数据 private static final int ALL_IDEL_TIME_OUT = 70; // 所有超时 @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("timeoutHandler",new IdleStateHandler(READ_IDEL_TIME_OUT,WRITE_IDEL_TIME_OUT, ALL_IDEL_TIME_OUT, TimeUnit.SECONDS)); pipeline.addLast("framer", new DelimiterBasedFrameDecoder(1024, Delimiters.lineDelimiter() )); pipeline.addLast("decoder",new StringDecoder(CharsetUtil.UTF_8)); pipeline.addLast("encoder",new StringEncoder(CharsetUtil.UTF_8)); pipeline.addLast("handler",new TimeoutClientHandler()); } } 上面两段代码,分别在服务端与客户端做了超时设置。 目前没找到有什么方案能够直接使用Netty进行请求处理的超时设置 甚至没有检测客户端/服务端是否在线的好的方法。一旦进行channelRead0,其他方法只有在运行完read后才能被调用,所以检测是否在线等方法无效。只能间接通过心跳检测判断服务器/客户端是否在线。
以上是关于超时问题的主要内容,如果未能解决你的问题,请参考以下文章