Apache MINA --- [过滤器]

IoFilter是MINA的核心组成部分,它提供了重要的规则.它过来全部在IoService和IoHandler之间的I/O事件和请求.若是你有过相关的Web应用开发经验,你能够把它简单的想象成Servlet过滤器的"兄弟".许多开箱即用的过滤器被提供用来促进网络应用开发速度,经过使用这些过滤器,能够简化典型的横向关注点,例如:java

    1.LoggingFilter 记录全部的事件和请求web

    2.ProtocolCodecFilter 将输入的字节流转化成简单的消息对象,反之亦然网络

    3.CompressionFilter 压缩数据session

    4.SSLFilter 添加SSL-TLS-StartTLS支持app

    5.更多...ide

你能够继承IoAdapter类来替代直接实现IoFilter接口.除非覆盖方法,否则全部收到的事件将被转直接转发到下一个过滤器.spa

//除非你想本身实现全部相关方法,否则继承IoFilterAdapter是更简单的作法
public class MyFilter extends IoFilterAdapter {
 
    // 须要注意的是,sessionCreated事件必须在I/O Processor线程中执行,不能转发给其余线程
    @Override
    public void sessionCreated(NextFilter nextFilter, IoSession session) throws Exception {
        nextFilter.sessionCreated(session);
    }
 
    @Override
    public void sessionOpened(NextFilter nextFilter, IoSession session) throws Exception {
        nextFilter.sessionOpened(session);
    }
 
    // ProtocolCodecFilter使用空缓冲区来做为一个内部信号,因此,当自定义Filter被放置在ProtocolCodecFilter以前时,须要进行以下判断
    @Override
    public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest request) {
        Object message = request.getMessage();
        if (message instanceof ByteBuffer && !((ByteBuffer) message).hasRemaining()) {
            nextFilter.filterWrite(session, request);
            return;
        }
    }
 
    @Override
    public void messageSent(NextFilter nextFilter, IoSession session, WriteRequest request) throws Exception {
        Object message = request.getMessage();
        if (message instanceof ByteBuffer && !((ByteBuffer) message).hasRemaining()) {
            nextFilter.messageSent(session, request);
            return;
        }
    }
}

备注:线程

    官方文档说明中描述,若是你的过滤器对输入输出对象作了类型转换,那么你必需要同时实现filterWrite和messageSent,若是你的过滤器没有作这样的转换,也建议这样实现....综上所述,自定义过滤器直接实现这两个方法.(我是这么理解的...)code

下面给出原文:orm

If you are going to transform an incoming write request via IoSession.write(), things can get pretty tricky. For example, let's assume your filter transforms HighLevelMessage to LowLevelMessage when IoSession.write() is invoked with a HighLevelMessage object. You could insert appropriate transformation code to your filter's filterWrite() method and think that's all. However, you have to note that you also need to take care of messageSent event because an IoHandler or any filters next to yours will expect messageSent() method is called with HighLevelMessage as a parameter, because it's irrational for the caller to get notified that LowLevelMessage is sent when the caller actually wrote HighLevelMessage. Consequently, you have to implement both filterWrite() and messageSent() if your filter performs transformation.

Please also note that you still need to implement similar mechanism even if the types of the input object and the output object are identical (e.g. CompressionFilter) because the caller of IoSession.write() will expect exactly what he wrote in his or her messageSent() handler method.

相关文章
相关标签/搜索