- 浏览: 984325 次
文章分类
- 全部博客 (428)
- Hadoop (2)
- HBase (1)
- ELK (1)
- ActiveMQ (13)
- Kafka (5)
- Redis (14)
- Dubbo (1)
- Memcached (5)
- Netty (56)
- Mina (34)
- NIO (51)
- JUC (53)
- Spring (13)
- Mybatis (17)
- MySQL (21)
- JDBC (12)
- C3P0 (5)
- Tomcat (13)
- SLF4J-log4j (9)
- P6Spy (4)
- Quartz (12)
- Zabbix (7)
- JAVA (9)
- Linux (15)
- HTML (9)
- Lucene (0)
- JS (2)
- WebService (1)
- Maven (4)
- Oracle&MSSQL (14)
- iText (11)
- Development Tools (8)
- UTILS (4)
- LIFE (8)
最新评论
-
Donald_Draper:
Donald_Draper 写道刘落落cici 写道能给我发一 ...
DatagramChannelImpl 解析三(多播) -
Donald_Draper:
刘落落cici 写道能给我发一份这个类的源码吗Datagram ...
DatagramChannelImpl 解析三(多播) -
lyfyouyun:
请问楼主,执行消息发送的时候,报错:Transport sch ...
ActiveMQ连接工厂、连接详解 -
ezlhq:
关于 PollArrayWrapper 状态含义猜测:参考 S ...
WindowsSelectorImpl解析一(FdMap,PollArrayWrapper) -
flyfeifei66:
打算使用xmemcache作为memcache的客户端,由于x ...
Memcached分布式客户端(Xmemcached)
Mina Nio处理器:http://donald-draper.iteye.com/blog/2377725
引言:
前面的文章我们看了Nio处理器,先来回顾一下:
NioProcessor内部有一个选择器Selector,一个可重入读写锁用于控制选择器相关的操作,构造主要是初始化线程执行器和选择器。Nio处理器的选择操作,唤醒等操作,实际通过内部的选择器完成。初始化会话,主要是配置会话通道为非阻塞模式,注册会话通道读事件到选择器。注册新选择器,主要是注册旧选择器的选择key(集合)关联的会话,通道,及通道兴趣事件集到新的选择器;会话时附加在通道选择key的Attachment上。处理器处理会话读写操作,主要是通过会话关联的通道完成。关闭会话主要是关闭会话关联的字节通道和取消会话关联选择key。
今天我们来看一下Io会话的定义:
附:
//TransportType
//TrafficMask
引言:
前面的文章我们看了Nio处理器,先来回顾一下:
NioProcessor内部有一个选择器Selector,一个可重入读写锁用于控制选择器相关的操作,构造主要是初始化线程执行器和选择器。Nio处理器的选择操作,唤醒等操作,实际通过内部的选择器完成。初始化会话,主要是配置会话通道为非阻塞模式,注册会话通道读事件到选择器。注册新选择器,主要是注册旧选择器的选择key(集合)关联的会话,通道,及通道兴趣事件集到新的选择器;会话时附加在通道选择key的Attachment上。处理器处理会话读写操作,主要是通过会话关联的通道完成。关闭会话主要是关闭会话关联的字节通道和取消会话关联选择key。
今天我们来看一下Io会话的定义:
/** * A handle which represents connection between two endpoints regardless of * transport types. * * {@link IoSession} provides user-defined attributes. User-defined attributes * are application-specific data which is associated with a session. * It often contains objects that represents the state of a higher-level protocol * and becomes a way to exchange data between filters and handlers. * * <h3>Adjusting Transport Type Specific Properties</h3> * <p> * You can simply downcast the session to an appropriate subclass. * * * <h3>Thread Safety</h3> * * {@link IoSession} is thread-safe. But please note that performing * more than one {@link #write(Object)} calls at the same time will * cause the {@link IoFilter#filterWrite(IoFilter.NextFilter, IoSession, IoFilter.WriteRequest)} * is executed simnutaneously, and therefore you have to make sure the * {@link IoFilter} implementations you're using are thread-safe, too. * * * @author The Apache Directory Project (mina-dev@directory.apache.org) * @version $Rev$, $Date$ */ public interface IoSession { /** * Returns the {@link IoService} which provides I/O service to this session. 获取会话关联的IoService */ IoService getService(); /** * Returns the {@link IoServiceConfig} of this session. 获取会话关联的IoService配置 */ IoServiceConfig getServiceConfig(); /** * Returns the {@link IoHandler} which handles this session. 获取会话Iohandler */ IoHandler getHandler(); /** * Returns the configuration of this session. 获取会话配置 */ IoSessionConfig getConfig(); /** * Returns the filter chain that only affects this session. 获取会话过滤链 */ IoFilterChain getFilterChain(); /** * Writes the specified <code>message</code> to remote peer. This * operation is asynchronous; {@link IoHandler#messageSent(IoSession, Object)} * will be invoked when the message is actually sent to remote peer. * You can also wait for the returned {@link WriteFuture} if you want * to wait for the message actually written. 发送消息给远端的peer。此操作是异步的,当消息实际发送到远端peer时,会调用 IoHandler#messageSent方法法。如果想等待消息实际发送完,可以等待WriteFuture。 */ WriteFuture write(Object message); /** * Closes this session immediately. This operation is asynthronous. * Wait for the returned {@link CloseFuture} if you want to wait for * the session actually closed. 立刻关闭会话。此操作为异步的。如果想要等待会话实际完成关闭,可以等待CloseFuture */ CloseFuture close(); /** * Returns an attachment of this session. * This method is identical with <tt>getAttribute( "" )</tt>. 返回会话附加物 */ Object getAttachment(); /** * Sets an attachment of this session. * This method is identical with <tt>setAttribute( "", attachment )</tt>. * 设置会话附加物 * @return Old attachment. <tt>null</tt> if it is new. */ Object setAttachment(Object attachment); /** * Returns the value of user-defined attribute of this session. * 获取会话属性key对一个的属性值 * @param key the key of the attribute * @return <tt>null</tt> if there is no attribute with the specified key */ Object getAttribute(String key); /** * Sets a user-defined attribute. * 设置会话属性 * @param key the key of the attribute * @param value the value of the attribute * @return The old value of the attribute. <tt>null</tt> if it is new. */ Object setAttribute(String key, Object value); /** * Sets a user defined attribute without a value. This is useful when * you just want to put a 'mark' attribute. Its value is set to * {@link Boolean#TRUE}. * 设置会话无值属性 * @param key the key of the attribute * @return The old value of the attribute. <tt>null</tt> if it is new. */ Object setAttribute(String key); /** * Removes a user-defined attribute with the specified key. * 移除会话属性 * @return The old value of the attribute. <tt>null</tt> if not found. */ Object removeAttribute(String key); /** * Returns <tt>true</tt> if this session contains the attribute with * the specified <tt>key</tt>. 判断是否包含属性key */ boolean containsAttribute(String key); /** * Returns the set of keys of all user-defined attributes. 获取会话所有属性 */ Set getAttributeKeys(); /** * Returns transport type of this session. 获取会话transport类型,socket,Datagram,vmpipe */ TransportType getTransportType(); /** * Returns <code>true</code> if this session is connected with remote peer. 会话是否连接 */ boolean isConnected(); /** * Returns <code>true</tt> if and only if this session is being closed * (but not disconnected yet) or is closed. 会话是否关闭 */ boolean isClosing(); /** * Returns the {@link CloseFuture} of this session. This method returns * the same instance whenever user calls it. 获取会话关闭结果 */ CloseFuture getCloseFuture(); /** * Returns the socket address of remote peer. 获取会话远端peer地址 */ SocketAddress getRemoteAddress(); /** * Returns the socket address of local machine which is associated with this * session. 获取会话本地地址 */ SocketAddress getLocalAddress(); /** * Returns the socket address of the {@link IoService} listens to to manage * this session. If this session is managed by {@link IoAcceptor}, it * returns the {@link SocketAddress} which is specified as a parameter of * {@link IoAcceptor#bind(SocketAddress, IoHandler)}. If this session is * managed by {@link IoConnector}, this method returns the same address with * that of {@link #getRemoteAddress()}. 获取IoService监听管理会话的地址。如果会话被IoAcceptor管理,返回的为IoAcceptor#bind(SocketAddress, IoHandler) 的参数地址。如果会话为IoConnector,则返回的远端peer地址。 */ SocketAddress getServiceAddress(); /** * Returns idle time for the specified type of idleness in seconds. 返回空闲状态的空闲时间s */ int getIdleTime(IdleStatus status); /** * Returns idle time for the specified type of idleness in milliseconds. 返回空闲状态的空闲时间ms */ long getIdleTimeInMillis(IdleStatus status); /** * Sets idle time for the specified type of idleness in seconds. 设置空闲状态的空闲时间 */ void setIdleTime(IdleStatus status, int idleTime); /** * Returns write timeout in seconds. 获取写超时时间s */ int getWriteTimeout(); /** * Returns write timeout in milliseconds. 获取写超时时间ms */ long getWriteTimeoutInMillis(); /** * Sets write timeout in seconds. 设置写超时时间 */ void setWriteTimeout(int writeTimeout); /** * Returns the current {@link TrafficMask} of this session. 返回会话传输状态(读写) */ TrafficMask getTrafficMask(); /** * Sets the {@link TrafficMask} of this session which will result * the parent {@link IoService} to start to control the traffic * of this session immediately. 设置会话传输状态,这将引起Io服务,开始控制会话传输 */ void setTrafficMask(TrafficMask trafficMask); /** * A shortcut method for {@link #setTrafficMask(TrafficMask)} that * suspends read operations for this session. 暂定会话读操作 */ void suspendRead(); /** * A shortcut method for {@link #setTrafficMask(TrafficMask)} that * suspends write operations for this session. 暂定会话写操作 */ void suspendWrite(); /** * A shortcut method for {@link #setTrafficMask(TrafficMask)} that * resumes read operations for this session. 恢复会话读操作 */ void resumeRead(); /** * A shortcut method for {@link #setTrafficMask(TrafficMask)} that * resumes write operations for this session. 恢复会话写操作 */ void resumeWrite(); /** * Returns the total number of bytes which were read from this session. 获取从会话读取的字节数 */ long getReadBytes(); /** * Returns the total number of bytes which were written to this session. 获取会话写的字节数 */ long getWrittenBytes(); /** * Returns the total number of messages which were read and decoded from this session. 获取从会话读取或解码的消息数 */ long getReadMessages(); /** * Returns the total number of messages which were written and encoded by this session. 获取会话发送消息和编码消息的数量 */ long getWrittenMessages(); /** * Returns the total number of write requests which were written to this session. 获取会话写请求的数量 */ long getWrittenWriteRequests(); /** * Returns the number of write requests which are scheduled to be written * to this session. 获取会话写请求实际被调度的数量,即实际发送数量 */ int getScheduledWriteRequests(); /** * Returns the number of bytes which are scheduled to be written to this * session. 获取会话实际发送字节数 */ int getScheduledWriteBytes(); /** * Returns the time in millis when this session is created. 获取会话创建时间 */ long getCreationTime(); /** * Returns the time in millis when I/O occurred lastly. 获取会话上一次发送IO操作的时间 */ long getLastIoTime(); /** * Returns the time in millis when read operation occurred lastly. 获取会话上一次读操作的时间 */ long getLastReadTime(); /** * Returns the time in millis when write operation occurred lastly. 获取会话上一次写操作的时间 */ long getLastWriteTime(); /** * Returns <code>true</code> if this session is idle for the specified * {@link IdleStatus}. 判断会话是否处理空闲状态status */ boolean isIdle(IdleStatus status); /** * Returns the number of the fired continuous <tt>sessionIdle</tt> events * for the specified {@link IdleStatus}. 获取会话处于空闲状态status的次数 * <p> * If <tt>sessionIdle</tt> event is fired first after some time after I/O, * <tt>idleCount</tt> becomes <tt>1</tt>. <tt>idleCount</tt> resets to * <tt>0</tt> if any I/O occurs again, otherwise it increases to * <tt>2</tt> and so on if <tt>sessionIdle</tt> event is fired again without * any I/O between two (or more) <tt>sessionIdle</tt> events. */ int getIdleCount(IdleStatus status); /** * Returns the time in millis when the last <tt>sessionIdle</tt> event * is fired for the specified {@link IdleStatus}. 获取会话上次处于空闲状态status的时间 */ long getLastIdleTime(IdleStatus status); }
附:
//TransportType
/** * Represents network transport types. * MINA provides three transport types by default: * [list] * [*]{@link #SOCKET} - TCP/IP * [*]{@link #DATAGRAM} - UDP/IP * <li>{@link #VM_PIPE} - in-VM pipe support (only available in protocol * layer</li> * [/list] * <p> * You can also create your own transport type. Please refer to * {@link #TransportType(String[], boolean)}. * * @author The Apache Directory Project (mina-dev@directory.apache.org) * @version $Rev$, $Date$ */ public final class TransportType implements Serializable { private static final long serialVersionUID = 3258132470497883447L; private static final Map name2type = new HashMap(); private final String[] names; private final transient boolean connectionless; private final transient Class envelopeType; /** * Transport type: TCP/IP (Registry name: <tt>"SOCKET"</tt> or <tt>"TCP"</tt>) */ public static final TransportType SOCKET = new TransportType(new String[] { "SOCKET", "TCP" }, false); /** * Transport type: UDP/IP (Registry name: <tt>"DATAGRAM"</tt> or <tt>"UDP"</tt>) */ public static final TransportType DATAGRAM = new TransportType( new String[] { "DATAGRAM", "UDP" }, true); /** * Transport type: in-VM pipe (Registry name: <tt>"VM_PIPE"</tt>) * Please refer to * [url=../protocol/vmpipe/package-summary.htm]<tt>org.apache.mina.protocol.vmpipe</tt>[/url] * package. */ public static final TransportType VM_PIPE = new TransportType( new String[] { "VM_PIPE" }, Object.class, false); ... }
//TrafficMask
/** * A type-safe mask that is used to control the traffic of {@link IoSession} * with {@link IoSession#setTrafficMask(TrafficMask)}. * * @author The Apache Directory Project (mina-dev@directory.apache.org) * @version $Rev$, $Date$ */ public class TrafficMask { private final int interestOps; private final String name; /** * This mask suspends both reads and writes. */ public static final TrafficMask NONE = new TrafficMask(0, "none"); /** * This mask suspends writes, and resumes reads if reads were suspended. */ public static final TrafficMask READ = new TrafficMask( SelectionKey.OP_READ, "read"); /** * This mask suspends reads, and resumes writes if writes were suspended. */ public static final TrafficMask WRITE = new TrafficMask( SelectionKey.OP_WRITE, "write"); /** * This mask resumes both reads and writes if any of them were suspended. */ public static final TrafficMask ALL = new TrafficMask(SelectionKey.OP_READ | SelectionKey.OP_WRITE, "all"); ... }
发表评论
-
Mina 报文连接器(NioDatagramConnector)
2017-06-14 08:46 1428Mina 抽象Polling连接器(A ... -
Mina 报文监听器NioDatagramAcceptor二(发送会话消息等)
2017-06-13 16:01 1556Mina 报文监听器NioDatagramAcceptor一( ... -
Mina 报文监听器NioDatagramAcceptor一(初始化,Io处理器)
2017-06-13 09:51 2587Mina Io监听器接口定义及抽象实现:http://dona ... -
Mina 报文通信简单示例
2017-06-12 09:01 2599MINA TCP简单通信实例:http://donald-dr ... -
Mina socket连接器(NioSocketConnector)
2017-06-12 08:37 4793Mina 抽象Polling连接器(AbstractPolli ... -
Mina 抽象Polling连接器(AbstractPollingIoConnector)
2017-06-11 21:29 1018Mina 连接器接口定义及抽象实现(IoConnector ) ... -
Mina 连接器接口定义及抽象实现(IoConnector )
2017-06-11 13:46 1841Mina IoService接口定义及抽象实现:http:// ... -
Mina socket监听器(NioSocketAcceptor)
2017-06-09 08:44 3437Mina IoService接口定义及抽象实现:http:// ... -
Mina 抽象polling监听器
2017-06-08 22:32 791Mina Io监听器接口定义及抽象实现:http://dona ... -
Mina Io监听器接口定义及抽象实现
2017-06-07 13:02 1362Mina IoService接口定义及抽象实现:http:// ... -
Mina IoService接口定义及抽象实现
2017-06-06 23:44 1204Mina IoHandler接口定义:http://donal ... -
Mina Nio会话(Socket,DataGram)
2017-06-06 12:53 1220Mina Socket会话配置:http://donald-d ... -
Mina 抽象Io会话
2017-06-05 22:45 1024Mina Io会话接口定义:http://donald-dra ... -
Mina Nio处理器
2017-06-04 22:19 751Mina Io处理器抽象实现:http://donald-dr ... -
Mina Io处理器抽象实现
2017-06-03 23:52 1156Mina 过滤链抽象实现:http://donald-drap ... -
Mina IoHandler接口定义
2017-06-01 21:30 1741Mina 过滤链抽象实现:http://donald-drap ... -
MINA 多路复用协议编解码器工厂二(多路复用协议解码器)
2017-06-01 12:52 2287MINA 多路复用协议编解码器工厂一(多路复用协议编码器): ... -
MINA 多路复用协议编解码器工厂一(多路复用协议编码器)
2017-05-31 22:22 1877MINA 多路分离解码器实例:http://donald-dr ... -
Mina 累计协议解码器
2017-05-31 00:09 1237MINA 编解码器实例:http://donald-drape ... -
Mina 协议编解码过滤器三(会话write与消息接收过滤)
2017-05-28 07:22 1759Mina 协议编解码过滤器一(协议编解码工厂、协议编码器): ...
相关推荐
- **功能**:IoHandler是Mina中最核心的接口之一,用于处理网络事件,包括会话创建、数据读写、异常处理等。 - **实现**:开发者需要实现该接口的方法,以定制网络通信的具体行为。 #### 三、Mina解析 ##### 3.1 ...
`IdleStatus` 枚举定义了会话的三种空闲状态:读取空闲、写入空闲以及两者同时空闲。通过设置空闲时间,可以帮助我们更好地管理会话资源。 综上所述,Mina 2.0 为开发者提供了丰富的功能和灵活性,使得构建复杂网络...
4. **会话事件**:Mina 会触发各种会话事件,如打开、关闭、异常等,开发者可以通过监听这些事件来执行相应的操作。 5. **数据传输**:数据的读写操作通过IoBuffer对象进行,它提供了一种高效、灵活的数据缓冲机制...
- IoService 接口定义了 Mina 中用于处理网络连接的服务,它包括 IoAcceptor 和 IoConnector。 - **IoAcceptor**: 负责监听并接受来自客户端的连接请求。 - **IoConnector**: 用于发起连接到远程服务器的请求。 ...
Mina使用IoBuffer作为数据缓冲区,它提供了一种高效的字节操作方式。同时,Mina还提供了Codec接口,用于数据的编码和解码。通过实现Codec接口,开发者可以自定义数据格式,实现与其他系统的通信。 五、异步I/O模型 ...
3. IoFilter:这个接口定义了一组拦截器,用于处理日志输出、黑名单过滤等,以及数据的编码(write方向)和解码(read方向)。编码和解码操作是Mina框架中非常核心的部分,因为它涉及到网络中数据传输的格式转换。 ...
- 实现此接口可定义如何处理会话创建、关闭、消息接收等事件。 #### 四、Mina中的长连接与短连接 - **长连接**:建立一次连接后保持连接状态,多次通信使用同一连接,适用于频繁交互的场景。 - **短连接**:每次...
3. IoFilter:这个接口定义了一系列可以进行数据拦截的组件,包括日志输出、数据过滤、编码和解码等操作。这些拦截器可以配置为一个链式结构,对流经的数据进行预处理或后处理。 4. IoHandler:这是业务逻辑处理的...
线程模型主要用来描述网络框架如何使用线程去处理连接、会话、以及请求和响应的逻辑。Mina2与Netty4都使用了Reactor模式的线程模型,但具体实现细节上有所不同。 Mina2使用了一个IoAcceptor线程来监听客户端连接,...
提供了一系列方法,如写入数据、关闭会话、获取会话ID、创建时间、上次IO时间以及配置信息等。 - **方法**: - `write(Object message)`: 将数据发送给客户端。 - `close()`: 关闭会话。 - `getId()`: 获取会话ID...
3. **MINA会话管理**:了解Session对象如何维护客户端连接的状态,包括打开、关闭、读写事件的处理。 4. **MINA服务端口**:查看服务端口的绑定和监听代码,理解如何启动一个MINA服务器,并接受客户端连接。 5. **...
通过“Mina、Netty、Twisted一起学(九):异步IO和回调函数.docx”,我们可以深入了解Twisted的异步I/O机制和回调函数的使用。 这些文档还涵盖了特定主题,如“Mina、Netty、Twisted一起学(三):TCP消息固定大小...
首先,Mina的核心在于它的事件驱动模型,它使用了IO多路复用技术,如Java的NIO(Non-blocking I/O)库,这使得程序能够同时处理多个连接,提高了并发性能。在Mina中,我们通常会定义一个`FilterChain`,这个链中的每...
Mina提供了一种事件驱动的异步模型,通过IO多路复用技术(如NIO)提高了网络通信的效率。在Mina中,网络连接被抽象为Session,数据交换则通过Filter Chain进行处理,这种设计模式允许开发者灵活地添加、修改或移除...
- **Spring Cloud**: 提供了一整套微服务解决方案,包括服务注册、服务发现、配置中心、熔断器、路由、微代理、控制总线、一次性令牌、全局锁、领导选举、分布式会话、集群状态等。 7. **Java并发与多线程**: - ...
- **Factory Method**:定义一个创建产品对象的接口,让子类决定实例化哪一个类。 - **Adapter**:将一个类的接口转换成客户希望的另一个接口。 **3. 经典线程设计模式** - **生产者消费者模型**:通过缓冲区实现...