博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Netty3 -会话状态监听
阅读量:4041 次
发布时间:2019-05-24

本文共 3005 字,大约阅读时间需要 10 分钟。

在指定的间期内,客户端与服务端的连接是否正常,是否有相关的请求发生

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

 

 

 

 

转载地址:http://cwadi.baihongyu.com/

你可能感兴趣的文章
在unity中建立最小的shader(Minimal Shader)
查看>>
1.3 Debugging of Shaders (调试着色器)
查看>>
关于phpcms中模块_tag.class.php中的pc_tag()方法的含义
查看>>
vsftp 配置具有匿名登录也有系统用户登录,系统用户有管理权限,匿名只有下载权限。
查看>>
linux安装usb wifi接收器
查看>>
补充自动屏蔽攻击ip
查看>>
谷歌走了
查看>>
多线程使用随机函数需要注意的一点
查看>>
getpeername,getsockname
查看>>
让我做你的下一行Code
查看>>
浅析:setsockopt()改善程序的健壮性
查看>>
关于对象赋值及返回临时对象过程中的构造与析构
查看>>
VS 2005 CRT函数的安全性增强版本
查看>>
SQL 多表联合查询
查看>>
Visual Studio 2010:C++0x新特性
查看>>
drwtsn32.exe和adplus.vbs进行dump文件抓取
查看>>
cppcheck c++静态代码检查
查看>>
在C++中使用Lua
查看>>
一些socket的编程经验
查看>>
socket编程中select的使用
查看>>