1. 程式人生 > >Netty3 -會話狀態監聽

Netty3 -會話狀態監聽

在指定的間期內,客戶端與服務端的連線是否正常,是否有相關的請求發生

package xss.netty.idle;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
import org.jboss.netty.handler.timeout.IdleStateHandler;
import org.jboss.netty.util.HashedWheelTimer;
import org.jboss.netty.util.Timer;

import java.net.InetSocketAddress;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * 會話狀態的監測
 */
public class IdelServer {

    public static void main(String[] args) throws  Exception {
        //服務啟動類
        ServerBootstrap bootstrap=new ServerBootstrap();
        //兩個工作執行緒池
        Executor bossExecutor = Executors.newCachedThreadPool();//boss 執行緒池工作組
        Executor workerExecutor=Executors.newCachedThreadPool();//work執行緒池工作組
        //設定factory
        bootstrap.setFactory(new NioServerSocketChannelFactory(bossExecutor,workerExecutor));

        //連結緩衝池的大小
        bootstrap.setOption("backlog",1024);
        //維持連結的活躍
        bootstrap.setOption("tcpNoDelay",true);
        //關閉延遲傳送
        bootstrap.setOption("keepAlive",true);
        final Timer timer= new HashedWheelTimer();
        //設定管道過濾器工廠
        bootstrap.setPipelineFactory(new ChannelPipelineFactory(){
            public ChannelPipeline getPipeline() throws Exception {
                ChannelPipeline channelPipeline= Channels.pipeline();
                //類似監聽客戶端連線的讀寫請求事件
                channelPipeline.addLast("idleHandler",new IdleStateHandler(timer,6,6,10));
                //將輸出引數轉換為String物件輸出到下一個pipe
                channelPipeline.addLast("decoder",new StringDecoder());
                channelPipeline.addLast("encoder",new StringEncoder());
                //業務處理的過濾器類
//                channelPipeline.addLast("idleCustomHandler",new IdleCustomHandler());
                channelPipeline.addLast("idleCustomHandler",new IdleCustom2Handler());
                return channelPipeline;
            }
        });
        //繫結埠
        bootstrap.bind(new InetSocketAddress(9999));
        System.out.println("Server started...");
    }
}

Handle處理器:

package xss.netty.idle;

import org.jboss.netty.channel.ChannelHandler;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.timeout.IdleState;
import org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler;
import org.jboss.netty.handler.timeout.IdleStateEvent;

/**
 * 有三種狀態:
 *   IdleState.ALL_IDLE; 讀與寫的請求都沒有
     IdleState.READER_IDLE; 客戶端請求服務
     IdleState.WRITER_IDLE  服務端迴應客戶端
 */
public class IdleCustomHandler  extends IdleStateAwareChannelHandler implements ChannelHandler {
    @Override
    public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) throws Exception {
        System.out.println("事件狀態:" + e.getState());
        //super.channelIdle(ctx, e);
    }

}