Apache MINA 框架之Filters介绍
IoFilter是NIMA框架核心组员之一,扮演者重要角色。它过滤所有IoService和IoHandler之间的I/O事件和请求。
一些典型且经常使用的过滤器:
- LoggingFilter 记录所有事件和请求。
- ProtocolCodecFilter 将传入消息ByteBuffer POJO,反之亦然。
- CompressionFilter 压缩数据。
- SSLFilter 添加SSL - TLS - StartTLS支持。
已经实现的Filter接口:
Filter | class | Description |
---|---|---|
Blacklist | BlacklistFilter | Blocks connections from blacklisted remote addresses |
Buffered Write | BufferedWriteFilter | Buffers outgoing requests like the BufferedOutputStream does |
Compression | CompressionFilter | |
ConnectionThrottle | ConnectionThrottleFilter | |
ErrorGenerating | ErrorGeneratingFilter | |
Executor | ExecutorFilter | |
FileRegionWrite | FileRegionWriteFilter | |
KeepAlive | KeepAliveFilter | |
Logging | LoggingFilter | Logs event messages, like MessageReceived, MessageSent, SessionOpened, ... |
MDC Injection | MdcInjectionFilter | Inject key IoSession properties into the MDC |
Noop | NoopFilter | A filter that does nothing. Useful for tests. |
Profiler | ProfilerTimerFilter | Profile event messages, like MessageReceived, MessageSent, SessionOpened, ... |
ProtocolCodec | ProtocolCodecFilter | A filter in charge of encoding and decoding messages |
Proxy | ProxyFilter | |
Reference counting | ReferenceCountingFilter | Keeps track of the number of usages of this filter |
RequestResponse | RequestResponseFilter | |
SessionAttributeInitializing | SessionAttributeInitializingFilter | |
StreamWrite | StreamWriteFilter | |
SslFilter | SslFilter | |
WriteRequest | WriteRequestFilter |
重载事件:
通过实现IoFilter IoAdapter直接替换已有的事件实现,
public class MyFilter extends IoFilterAdapter { @Override public void sessionOpened(NextFilter nextFilter, IoSession session) throws Exception { // Some logic here... nextFilter.sessionOpened(session); // Some other logic here... } }