关于Mina开源框架的一处问题

    技术2026-01-09  4

    最近一个项目中使用到了MINA2.0作为通讯框架。

    源代码中使用到了IoFilter类的init和destory方法初始化和销毁公共资源。

    MINA中的源码和注释如下:

     

    /** * Invoked by {@link ReferenceCountingFilter} when this filter * is added to a {@link IoFilterChain} at the first time, so you can * initialize shared resources. Please note that this method is never * called if you don't wrap a filter with {@link ReferenceCountingFilter}. */ void init() throws Exception; /** * Invoked by {@link ReferenceCountingFilter} when this filter * is not used by any {@link IoFilterChain} anymore, so you can destroy * shared resources. Please note that this method is never called if * you don't wrap a filter with {@link ReferenceCountingFilter}. */ void destroy() throws Exception; 

     

     

    于是我使用了ReferenceCountingFilter类来包装自定义的类。

    结果出现了当建立两个或者两个以上会话后断开一个会话destroy()方法就被调用了。而后无论如何都不会调用init()方法。

     

    public synchronized void onPreAdd(IoFilterChain parent, String name, NextFilter nextFilter) throws Exception { if (0 == count) { filter.init(); ++count; } filter.onPreAdd(parent, name, nextFilter); }  

     

    将    if (0 == count) {

                filter.init();

                ++count;

            }

    改成if (0 == count) {

                filter.init();

            }

            ++count;

    然后将源码重新打包放上去测试,嗯,结果...你懂的!

     

     

    最新回复(0)