超時問題

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後才能被調用,因此檢測是否在線等方法無效。只能間接經過心跳檢測判斷服務器/客戶端是否在線。
相關文章
相關標籤/搜索