mina2 主要接口整理

IoService
--- Base interface for all IoAcceptors and IoConnectors that provide I/O service and manage IoSessions.

    IoAcceptors和IoConnectors的基础接口,提供I/O服务和IoSession管理功能。

IoAcceptor extends IoService
--- Accepts incoming connection,communicates with clients,and fires events to IoHandlers

    接收连接请求,与客户端交互并且触发事件到IoHandler
    You should bind to the desired socket address to accept in coming connections, and when events for
incoming connections will be sent to the specified default IoHandler.
   

    Threads accept incoming connections start automatically when bind() is invoked ,and stop when unbind()
 is invoked.

IoConnector extends IoService
--- Connects to endpoint,communicates with whe server,and fires events to IoHandlers.
    You should connect to the desired socket address to start communication,and then events for incoming
connections will be sent to the specified default IoHandler.
    Threads connect to endpoint start automatically when connect(SocketAddress) is invoked,and stop when
all connection attempts are finished.

IoHandler
--- Handles all I/O events fired by MINA.

IoSession
--- A handle which represents connection between two end-points regardless of transport types.
    IoSession provides user-defined attributes.User-defined attributes are application-specific data
which are 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.
  Adjusting Transport Type Specific Properties
    You can simply downcast the session to an appropriate subclass.
  Thread Safety
    IoSession is thread-safe. But please note that performing more than one write(Object) calls at the
same time will cause the IoFilter#filterWrite(IoFilter.NextFilter,IoSession,WriteRequest) to be
executed simultaneously, and therefore you have to make sure the IoFilter implementations you're useing
are thread-safe, too.
  Equality of Sessions
    The getId() method is totally wrong.We can't base a method which is designed to create a unique ID
on the hashCode method. equals(Object) and hashCode() shall not be overriden to the default behavior
that is defined in Object.

IoFilter
--- A filter which intercepts IoHandler events like Servlet filters. Filters can be used for these
purposes:Event loging,Performance measurement,Authorization,Overload control,Message transformation
(e.g. encryption and decryption,...), and any more.
    Please NEVER implement your filters to wrap IoSessions. Users can cache the reference to the
session, which might malfunction if any filters are added or removed later.
  The Life Cycle
    IoFilters are activated only when they are inside IoFilterChain.
    When you add an IoFilter to an IoFilterChain:
      init() is invoked by ReferenceCountingFilter if the filter is added ad the first time.
      onPreAdd(IoFilterChain,String,NextFilter) is invoked to notify that the filter will be added
to the chain.
      The filter is added to the chain, and all events and I/O requests pass through the filter from now.
      onPostAdd(IoFilterChain, String, NextFilter) is invoked to notify that the filter is added to the
chain.
      The filter is removed from the chain if onPostAdd(IoFilterChain, String,
org.apache.mina.core.filterchain.IoFilter.NextFilter) threw an exception. destroy() is also invoked by
ReferenceCountingFilter if the filter is the last filter which was added to IoFilterChains.
    When you removed an IoFilter from an IoFilterChain:
      onPreRemove(IoFilterChain, String, NextFilter) is invoked to notify that the filter will be removed
from the chain.
      The filter is removed from the chain, and any events and I/O requests don't pass through the filter
from now.
      onPostRemove(IoFilterChain, String, NextFilter) is invoked to notify that the filter is removed
from the chain.
      destroy() is invoked by ReferenceCountingFilter if the removed filter was the last one.

IoFilterChain
--- A container of IoFilters that forwards IoHandler events to the consisting filters and terminal
IoHandler sequentially.
    Every IoSession has its own IoFilterChain (1-to-1 relationship).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,mina-filter是一个用于Apache MINA(Multipurpose Infrastructure for Network Applications)框架的过滤器。MINA是一个基于Java的网络应用程序框架,用于开发高性能和可扩展的网络应用程序。 MINA框架使用过滤器链来处理输入和输出数据。过滤器链由多个过滤器组成,每个过滤器负责处理特定的任务,例如数据压缩、数据加密、协议解析等。过滤器链可以根据应用程序的需求进行配置和定制。 以下是一个使用mina-filter的示例: ```java import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder; import org.apache.mina.core.filterchain.IoFilterAdapter; import org.apache.mina.core.session.IoSession; import org.apache.mina.filter.codec.ProtocolCodecFilter; import org.apache.mina.filter.logging.LoggingFilter; public class MinaFilterExample { public static void main(String[] args) { // 创建过滤器链 DefaultIoFilterChainBuilder filterChain = new DefaultIoFilterChainBuilder(); // 添加日志过滤器 filterChain.addLast("logger", new LoggingFilter()); // 添加协议编解码过滤器 filterChain.addLast("codec", new ProtocolCodecFilter(new MyProtocolCodecFactory())); // 添加自定义过滤器 filterChain.addLast("myFilter", new MyFilter()); // 创建MINA服务器 NioSocketAcceptor acceptor = new NioSocketAcceptor(); acceptor.setFilterChainBuilder(filterChain); // 启动服务器 try { acceptor.bind(new InetSocketAddress(8888)); System.out.println("Server started on port 8888"); } catch (IOException e) { e.printStackTrace(); } } // 自定义协议编解码工厂 private static class MyProtocolCodecFactory implements ProtocolCodecFactory { // 实现编解码逻辑 // ... } // 自定义过滤器 private static class MyFilter extends IoFilterAdapter { @Override public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception { // 处理接收到的消息 // ... } @Override public void messageSent(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception { // 处理发送的消息 // ... } } } ``` 上述示例中,我们创建了一个MINA服务器,并配置了一个过滤器链。过滤器链包括日志过滤器、协议编解码过滤器和自定义过滤器。日志过滤器用于记录日志信息,协议编解码过滤器用于处理数据的编码和解码,自定义过滤器用于处理接收和发送的消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值