1.IoService的作用
IoService提供了基本的io服务并管理 I/O Sessions,详细内容如下:
- sessions管理 : 创建和删除sessions, 检测 idleness.
- 过滤器链管理 : 处理过滤链, 允许用户改变过滤链
- handler 触发: 当接收到新消息等时,调用 handler
- 统计管理 : 更新下面发送的信息量:消息, 比特流, 和其他的东西
- 监听器管理 : 管理用户可以建立的监听器
- 通信管理 : 处理服务端和客户端两者之间的数据传输
2.IoService详细
IoService是所有IoConnector和IoAcceptor的父接口,提供io服务和session管理。提供了如下方法:
- getTransportMetadata()
- addListener()
- removeListener()
- isDisposing()
- isDisposed()
- dispose()
- getHandler()
- setHandler()
- getManagedSessions()
- getManagedSessionCount()
- getSessionConfig()
- getFilterChainBuilder()
- setFilterChainBuilder()
- getFilterChain()
- isActive()
- getActivationTime()
- broadcast()
- setSessionDataStructureFactory()
- getScheduledWriteBytes()
- getScheduledWriteMessages()
- getStatistics()
3.服务端IoService之IoAcceptor
创建
public TcpServer() throws IOException { // Create a TCP acceptor IoAcceptor acceptor = new NioSocketAcceptor(); // Associate the acceptor to an IoHandler instance (your application) acceptor.setHandler(this); // Bind : this will start the server... acceptor.bind(new InetSocketAddress(PORT)); System.out.println("Server started..."); }
// Create an UDP acceptor IoAcceptor acceptor = new NioDatagramAcceptor();
销毁
// Stop the service, waiting for the pending sessions to be inactive acceptor.dispose(); 或者 // Stop the service, waiting for the processing session to be properly completed acceptor.dispose( true );
获取IoService状态:
isActive()
isDisposing()
isDisposed()
管理IoHandler
setHandler(IoHandler) 或者 getHandler()
管理过滤器链
// Add a logger filter DefaultIoFilterChainBuilder chain = acceptor.getFilterChain(); chain.addLast("logger", new LoggingFilter()); 或者 // Add a logger filter DefaultIoFilterChainBuilder chain = new DefaultIoFilterChainBuilder(); chain.addLast("logger", new LoggingFilter()); // And inject the created chain builder in the service acceptor.setFilterChainBuilder(chain);
4.客户端IoService之IoConnector
- NioSocketConnector : the non-blocking Socket transport Connector
- NioDatagramConnector : the non-blocking UDP transport * Connector*
- AprSocketConnector : the blocking Socket transport * Connector*, based on APR
- ProxyConnector : a Connector providing proxy support
- SerialConnector : a Connector for a serial transport
- VmPipeConnector : the in-VM * Connector*
微信公众号: 架构师日常笔记 欢迎关注!