netty中的session属性设置和链接事件捕获

本文通过实例展示了如何在Netty中为连接设置属性,并捕获链接事件,包括注册、注销以及读写超时事件。通过开启telnet客户端进行测试,验证了Netty在链接管理上的灵活性和强大功能。

Netty的使用比mina更灵活,也更复杂,下面通过一个例子,主要说明netty中如何对某个链接设置属性,并顺带描述了链接事件捕获

 

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.AttributeKey;

public class NettyAttrTest {

	public static void main(String[] args) throws InterruptedException {
	    EventLoopGroup bossGroup = new NioEventLoopGroup(); 
	    EventLoopGroup workerGroup = new NioEventLoopGroup();
	    try {
	        ServerBootstrap b = new ServerBootstrap();
	        b.group(bossGroup, workerGroup)
	         .channel(NioServerSocketChannel.class)
	         .childHandler(new ChannelInitializer<SocketChannel>() {
	        	private static final int IDEL_TIME_OUT = 10;
	            private static final int READ_IDEL_TIME_OUT = 4;
	        	private static final int WRITE_IDEL_TIME_OUT = 5;
	        	private AttributeKey<Long> START_TIME = new AttributeKey<Long>("START_TIME");
	             @Override
	             public void initChannel(SocketChannel ch) throws Exception {
	            	 //idle<strong>事件</strong>监听
	         		ch.pipeline().addLast("idleStateHandler", new IdleStateHandler(READ_IDEL_TIME_OUT, WRITE_IDEL_TIME_OUT, IDEL_TIME_OUT));
	                ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
	                	
	                	@Override
	                	public void channelRegistered(ChannelHandlerContext ctx)//链接<strong>事件</strong>监听
	                			throws Exception {
	                		System.out.println("channel register");
	                		//<strong>设置</strong>链接<strong>属性</strong>,记录链接开始的时间
	                		ctx.attr(START_TIME).set(System.currentTimeMillis());
	                	}
	                	
	                	@Override
	                	public void channelUnregistered(//断开链接<strong>事件</strong>
	                			ChannelHandlerContext ctx) throws Exception {
	                		System.out.println("channel unregister");
	                	}
	                	
	                	@Override
	                	public void userEventTriggered(//<strong>事件</strong>监听
	                			ChannelHandlerContext ctx, Object evt)
	                			throws Exception {
	                		//监听idle<strong>事件</strong>
	                		if(IdleStateEvent.class.isAssignableFrom(evt.getClass())){
	                			IdleStateEvent event = (IdleStateEvent) evt;
	                			//获取链接相关的<strong>属性</strong>(即链接开始的时间戳)
	                			Long startTime = ctx.attr(START_TIME).get();
	                			if(startTime == null)
	                				//not arrive forever
	                				return;
	                			//read idle
	                			if(event.state() == IdleState.READER_IDLE)
	                				System.out.println("read idle"+startTime);
	                			//write idle
	                			else if(event.state() == IdleState.WRITER_IDLE)
	                				System.out.println("write idle"+startTime);
	                			//read and write idle
	                			else if(event.state() == IdleState.ALL_IDLE)
	                				System.out.println("all idle"+startTime);
	                		}
	                	}
	                });
	             }
	         })
	         .option(ChannelOption.SO_BACKLOG, 128)
	         .childOption(ChannelOption.SO_KEEPALIVE, true);
	        
	        ChannelFuture f = b.bind(8080).sync();
	        f.channel().closeFuture().sync();
	    } finally {
	        workerGroup.shutdownGracefully();
	        bossGroup.shutdownGracefully();
	    }
	}
	
	
}


channelRegistered用于监听链接事件,当客户端链接时,将链接的时间记录在ctx的attr中。

 userEventTriggered中针对某一个特定的链接取出其链接时间,并输出出来。

测试:开启两个telnet客户端:

telnet 127.0.0.1 8080

结果:

channel register

channel register

read idle1378195113609

write idle1378195113609

read idle1378195117015

read idle1378195113609

write idle1378195117015

all idle1378195113609

write idle1378195113609

read idle1378195117015

read idle1378195113609

all idle1378195117015

write idle1378195117015

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值