mina中责任链模式的实现
一、mina的框架回顾
责任链模式在mina中有重要的作用,其中Filter机制就是基于责任链实现的。
二、mina中filter
2.1、mina中的filterchain包结构
- IoFilter接口:Filter层的每个filter都是对上图IoFilter接口的实现。
- IoFilterChainBuilder接口和DefaultIoFilterChainBuilder实现不再细讲,从字面意思就是IoFilterChain的建造者。
- IoFilterEvent是代表filter事件,IoFilterLifeCycleException是指filter中出现循环链表异常。
三、mina的filter中实现类
3.0、NextFilter接口
NextFilter接口看上去和IoFilter接口差不多,但NextFilter接口代表的是“下一个filter”,这里的下是抽象的,因为在mina的各种链中,处理顺序有的是从头到尾,有的是从尾到头,而这里的下就代表了熟悉中的下一个filter。
3.1、HeadFilter
HeadFilter类只对发送消息处理方法重载,HeadFilter是发送消息最后的处理节点。
3.1.1、方法列表
3.1.2、调用IoService(边界)
关键代码见下面,将待发送的数据(字节)写到缓冲队列里,或直接调用flush()写到系统缓冲区。
WriteRequestQueue writeRequestQueue = s.getWriteRequestQueue(); if (!s.isWriteSuspended()) { if (writeRequestQueue.size() == 0) { // We can write directly the message s.getProcessor().write(s, writeRequest); } else { s.getWriteRequestQueue().offer(s, writeRequest); s.getProcessor().flush(s); } } else { s.getWriteRequestQueue().offer(s, writeRequest); }
3.1.1、全部源码
private class HeadFilter extends IoFilterAdapter { @SuppressWarnings("unchecked") @Override public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception { AbstractIoSession s = (AbstractIoSession) session; // Maintain counters. if (writeRequest.getMessage() instanceof IoBuffer) { IoBuffer buffer = (IoBuffer) writeRequest.getMessage(); // I/O processor implementation will call buffer.reset() // it after the write operation is finished, because // the buffer will be specified with messageSent event. buffer.mark(); int remaining = buffer.remaining(); if (remaining == 0) { // Zero-sized buffer means the internal message // delimiter. s.increaseScheduledWriteMessages(); } else { s.increaseScheduledWriteBytes(remaining); } } else { s.increaseScheduledWriteMessages(); } WriteRequestQueue writeRequestQueue = s.getWriteRequestQueue(); if (!s.isWriteSuspended()) { if (writeRequestQueue.size() == 0) { // We can write directly the message s.getProcessor().write(s, writeRequest); } else { s.getWriteRequestQueue().offer(s, writeRequest); s.getProcessor().flush(s); } } else { s.getWriteRequestQueue().offer(s, writeRequest); } } @SuppressWarnings("unchecked") @Override public void filterClose(NextFilter nextFilter, IoSession session) throws Exception { ((AbstractIoSession) session).getProcessor().remove(session); } }
3.2、TailFilter
TailFilter类只对接受消息处理方法重载。
3.2.1、方法列表
3.2.2、调用IoHandler(边界)
先判断待处理数据类型,如果不满足条件则继续读取数据。否则,调用handler类来接收处理消息。
public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception { AbstractIoSession s = (AbstractIoSession) session; if (!(message instanceof IoBuffer)) { s.increaseReadMessages(System.currentTimeMillis()); } else if (!((IoBuffer) message).hasRemaining()) { s.increaseReadMessages(System.currentTimeMillis()); } try { session.getHandler().messageReceived(s, message); } finally { if (s.getConfig().isUseReadOperation()) { s.offerReadFuture(message); } } }
3.2.3、全部源码
private static class TailFilter extends IoFilterAdapter { @Override public void sessionCreated(NextFilter nextFilter, IoSession session) throws Exception { try { session.getHandler().sessionCreated(session); } finally { // Notify the related future. ConnectFuture future = (ConnectFuture) session.removeAttribute(SESSION_CREATED_FUTURE); if (future != null) { future.setSession(session); } } } @Override public void sessionOpened(NextFilter nextFilter, IoSession session) throws Exception { session.getHandler().sessionOpened(session); } @Override public void sessionClosed(NextFilter nextFilter, IoSession session) throws Exception { AbstractIoSession s = (AbstractIoSession) session; try { s.getHandler().sessionClosed(session); } finally { try { s.getWriteRequestQueue().dispose(session); } finally { try { s.getAttributeMap().dispose(session); } finally { try { // Remove all filters. session.getFilterChain().clear(); } finally { if (s.getConfig().isUseReadOperation()) { s.offerClosedReadFuture(); } } } } } } @Override public void sessionIdle(NextFilter nextFilter, IoSession session, IdleStatus status) throws Exception { session.getHandler().sessionIdle(session, status); } @Override public void exceptionCaught(NextFilter nextFilter, IoSession session, Throwable cause) throws Exception { AbstractIoSession s = (AbstractIoSession) session; try { s.getHandler().exceptionCaught(s, cause); } finally { if (s.getConfig().isUseReadOperation()) { s.offerFailedReadFuture(cause); } } } @Override public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception { AbstractIoSession s = (AbstractIoSession) session; if (!(message instanceof IoBuffer)) { s.increaseReadMessages(System.currentTimeMillis()); } else if (!((IoBuffer) message).hasRemaining()) { s.increaseReadMessages(System.currentTimeMillis()); } try { session.getHandler().messageReceived(s, message); } finally { if (s.getConfig().isUseReadOperation()) { s.offerReadFuture(message); } } } @Override public void messageSent(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception { session.getHandler().messageSent(session, writeRequest.getMessage()); } @Override public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception { nextFilter.filterWrite(session, writeRequest); } @Override public void filterClose(NextFilter nextFilter, IoSession session) throws Exception { nextFilter.filterClose(session); } }
3.3、Entry接口
Entry接口是IoFilterChain接口的一个内部接口,Entry是对IoFilter和NextFilter的封装整合接口,它属于链表IoFilterChain中的元素。指向当前和下一个过滤器。其中IoFilterChain接口代表了过滤器的容器,它本身就是一个对象引用形成的链表结构,默认的实现DefaultIoFilterChain其实有对链表头(head)的引用,找到头后就可以顺着头向下找,一直找到尾(tail)。
Entry接口与IoFilterChain接口关系如下:
3.4、EntryImpl类
EntryImpl是Entry接口的实现类,类的成员中含两个接口,filter和nextFilter,他们所用的接口是不一样的,至于为什么不用同一个接口,我想可能是因为接口职责单一的原则吧。
从EntryImpl类的构造方法看到,EntryImpl中保持对上一个节点和下一个节点引用,双向链表结构,name即过滤层名称,filter即过滤层的具体实现,而nextFilter是在构造方法中的内部实现。
private class EntryImpl implements Entry { private EntryImpl prevEntry; private EntryImpl nextEntry; private final String name; private IoFilter filter; private final NextFilter nextFilter;
3.4.2、IoFilter接口和NextFilter接口:
虽然有IoFilter和NextFilter两个接口,接口方法都差不多,但最后真正业务的执行者还是IoFilter的实现,
IoFilter:IoFilterAdapter做为它的默认实现,完成了适配器的功能,以后的类可以直接继承它而不用实现IoFilter接口,想实现哪个方法只需要覆盖IoFilterAdapter的类的方法即可。
NextFilter:只起到转发的作用,看EntryImpl中的匿名的NextFilter实现类中,基本每个方法都是调用callNextSessionIdle()来完成转发的。
3.4.3、全部源码
private class EntryImpl implements Entry { private EntryImpl prevEntry; private EntryImpl nextEntry; private final String name; private IoFilter filter; private final NextFilter nextFilter; private EntryImpl(EntryImpl prevEntry, EntryImpl nextEntry, String name, IoFilter filter) { if (filter == null) { throw new IllegalArgumentException("filter"); } if (name == null) { throw new IllegalArgumentException("name"); } this.prevEntry = prevEntry; this.nextEntry = nextEntry; this.name = name; this.filter = filter; this.nextFilter = new NextFilter() { public void sessionCreated(IoSession session) { Entry nextEntry = EntryImpl.this.nextEntry; callNextSessionCreated(nextEntry, session); } public void sessionOpened(IoSession session) { Entry nextEntry = EntryImpl.this.nextEntry; callNextSessionOpened(nextEntry, session); } public void sessionClosed(IoSession session) { Entry nextEntry = EntryImpl.this.nextEntry; callNextSessionClosed(nextEntry, session); } public void sessionIdle(IoSession session, IdleStatus status) { Entry nextEntry = EntryImpl.this.nextEntry; callNextSessionIdle(nextEntry, session, status); } public void exceptionCaught(IoSession session, Throwable cause) { Entry nextEntry = EntryImpl.this.nextEntry; callNextExceptionCaught(nextEntry, session, cause); } public void messageReceived(IoSession session, Object message) { Entry nextEntry = EntryImpl.this.nextEntry; callNextMessageReceived(nextEntry, session, message); } public void messageSent(IoSession session, WriteRequest writeRequest) { Entry nextEntry = EntryImpl.this.nextEntry; callNextMessageSent(nextEntry, session, writeRequest); } public void filterWrite(IoSession session, WriteRequest writeRequest) { Entry nextEntry = EntryImpl.this.prevEntry; callPreviousFilterWrite(nextEntry, session, writeRequest); } public void filterClose(IoSession session) { Entry nextEntry = EntryImpl.this.prevEntry; callPreviousFilterClose(nextEntry, session); } public String toString() { return EntryImpl.this.nextEntry.name; } }; }public void addAfter(String name, IoFilter filter) { DefaultIoFilterChain.this.addAfter(getName(), name, filter); } public void addBefore(String name, IoFilter filter) { DefaultIoFilterChain.this.addBefore(getName(), name, filter); } public void remove() { DefaultIoFilterChain.this.remove(getName()); } public void replace(IoFilter newFilter) { DefaultIoFilterChain.this.replace(getName(), newFilter); } }
3.5、IoFilterEvent
public class IoEvent implements Runnable { private final IoEventType type; private final IoSession session; private final Object parameter; public IoEvent(IoEventType type, IoSession session, Object parameter) { if (type == null) { throw new IllegalArgumentException("type"); } if (session == null) { throw new IllegalArgumentException("session"); } this.type = type; this.session = session; this.parameter = parameter; } public void run() { fire(); } public void fire() { switch (getType()) { case MESSAGE_RECEIVED: getSession().getFilterChain().fireMessageReceived(getParameter()); break; case MESSAGE_SENT: getSession().getFilterChain().fireMessageSent((WriteRequest) getParameter()); break; case WRITE: getSession().getFilterChain().fireFilterWrite((WriteRequest) getParameter()); break; case CLOSE: getSession().getFilterChain().fireFilterClose(); break; case EXCEPTION_CAUGHT: getSession().getFilterChain().fireExceptionCaught((Throwable) getParameter()); break; case SESSION_IDLE: getSession().getFilterChain().fireSessionIdle((IdleStatus) getParameter()); break; case SESSION_OPENED: getSession().getFilterChain().fireSessionOpened(); break; case SESSION_CREATED: getSession().getFilterChain().fireSessionCreated(); break; case SESSION_CLOSED: getSession().getFilterChain().fireSessionClosed(); break; default: throw new IllegalArgumentException("Unknown event type: " + getType()); } } }
IoFilterEvent 实现类
public class IoFilterEvent extends IoEvent { /** A logger for this class */ static Logger LOGGER = LoggerFactory.getLogger(IoFilterEvent.class); /** A speedup for logs */ static boolean DEBUG = LOGGER.isDebugEnabled(); private final NextFilter nextFilter; public IoFilterEvent(NextFilter nextFilter, IoEventType type, IoSession session, Object parameter) { super(type, session, parameter); if (nextFilter == null) { throw new IllegalArgumentException("nextFilter must not be null"); } this.nextFilter = nextFilter; } public NextFilter getNextFilter() { return nextFilter; } @Override public void fire() { IoSession session = getSession(); NextFilter nextFilter = getNextFilter(); IoEventType type = getType(); if (DEBUG) { LOGGER.debug("Firing a {} event for session {}", type, session.getId()); } switch (type) { case MESSAGE_RECEIVED: Object parameter = getParameter(); nextFilter.messageReceived(session, parameter); break; case MESSAGE_SENT: WriteRequest writeRequest = (WriteRequest) getParameter(); nextFilter.messageSent(session, writeRequest); break; case WRITE: writeRequest = (WriteRequest) getParameter(); nextFilter.filterWrite(session, writeRequest); break; case CLOSE: nextFilter.filterClose(session); break; case EXCEPTION_CAUGHT: Throwable throwable = (Throwable) getParameter(); nextFilter.exceptionCaught(session, throwable); break; case SESSION_IDLE: nextFilter.sessionIdle(session, (IdleStatus) getParameter()); break; case SESSION_OPENED: nextFilter.sessionOpened(session); break; case SESSION_CREATED: nextFilter.sessionCreated(session); break; case SESSION_CLOSED: nextFilter.sessionClosed(session); break; default: throw new IllegalArgumentException("Unknown event type: " + type); } } }
3.6、IoFilterLifeCycleException
mina中有3处使用了它,