`

阿里巴巴druid源码分析之 filter-chain设计模式

阅读更多

druid是号称目前最好的java数据库连接池,温少写的代码中有很多设计模式的思想,其中最重要的一个就是filter-chain设计模式给druid所带来的可扩展性。对此,我很感兴趣,并希望日后借鉴,所以看了看他的源码,以此记录一下。

druid中设计到filter-chain设计模式的类或接口主要有一下几个Filter、FilterAdapter、FilterChain、FilterChainImpl、FilterEventAdapter、FilterManager 以及举例讲讲两个具体的filter:StatFilter、Slf4jLogFilter

 

具体职责划分如下:

Filter接口:定义了过滤器需要关注的事件,以及可以处理的事件

 

FilterChain接口:定义了这个过滤器链可以处理的事件,druid的设计中,这两个接口几乎一样。

 

FilterChainImpl实现类:主要有两个具体作用,其一,具体处理chain中事件的具体事件;其二,将请求分发给下一个filter‘“处理”,注意,这里的处理,并不是去处理这个事件,而是做一些doBefore,doAfter事件。

 

FilterEventAdapter类:将我们关注的具体事件,通过适配器的设计模式,分为doBefore,do,doAfter三种事件

StatFilter、Slf4jLogFilter类:实现了doBefore,doAfter,这样的话,配置了这两个filter的类就可以做一些切面的事情了

 

FilterManager类:工具类,提供了filter的spi方式加载方式,也在于一种解耦,将filter和chain解耦,方便日后扩展。

 

举例数据库的连接过程

其中filter的接口,每个filter只能临时拥有chain,而每个chain拥有filter:

 ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException;

 FilterChain接口,druid中,最终执行具体方法的是FilterChainImpl,

ConnectionProxy connection_connect(Properties info) throws SQLException;
 int getFilterSize();

 FilterChainImpl具体如何实现调用和分发请求的呢?请看

 public ConnectionProxy connection_connect(Properties info) throws SQLException {
        if (this.pos < filterSize) {
            return nextFilter().connection_connect(this, info);
        }

        Driver driver = dataSource.getRawDriver();
        String url = dataSource.getRawJdbcUrl();

        Connection nativeConnection = driver.connect(url, info);
        return wrap(nativeConnection, info);
    }

 从这里,我们可以看到,其实具体数据库的连接,没必要每个filter都具体去做,为什么呢,因为具体的数据库连接这一步是固定的,写不出花来的,不需要每一个filter去实现,所以,filterChainImpl帮我们做了,这个也是filter-chain和其他的chain不同的地方,它的filter更关注切面的处理。而这里好像也看不出来怎么分发,

请看下面代码:

  private Filter nextFilter() {
        Filter filter = getFilters().get(pos++);
        return filter;
    }

 其实他就是遍历了list,记住list的偏移量,一个个取出来filter的

ok,到这里我们就可以看看filter具体怎样处理事件的。根据上面说的,每个事件被分成了doBefore,do,doAfter那么这是在哪里被切分的呢?这时候就引出了FilterEventAdapter类了

  public ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException {
        connection_connectBefore(chain, info);

        ConnectionProxy connection = super.connection_connect(chain, info);

        connection_connectAfter(connection);

        return connection;
    }

    public void connection_connectBefore(FilterChain chain, Properties info) {

    }

    public void connection_connectAfter(ConnectionProxy connection) {

    }

 看见了吧,这里就被分成了三个事件了,所以,我们可以肯定,每个filter都是继承的FilterEventAdapter

 public ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException {
        ConnectionProxy connection = null;
        {
            long startNano = System.nanoTime();
            long startTime = System.currentTimeMillis();

            long nanoSpan;
            long nowTime = System.currentTimeMillis();

            JdbcDataSourceStat dataSourceStat = chain.getDataSource().getDataSourceStat();
            dataSourceStat.getConnectionStat().beforeConnect();
            try {
                connection = chain.connection_connect(info);
                nanoSpan = System.nanoTime() - startNano;
            } catch (SQLException ex) {
                dataSourceStat.getConnectionStat().connectError(ex);
                throw ex;
            }
            dataSourceStat.getConnectionStat().afterConnected(nanoSpan);

            if (connection != null) {
                JdbcConnectionStat.Entry statEntry = getConnectionInfo(connection);

                dataSourceStat.getConnections().put(connection.getId(), statEntry);

                statEntry.setConnectTime(new Date(startTime));
                statEntry.setConnectTimespanNano(nanoSpan);
                statEntry.setEstablishNano(System.nanoTime());
                statEntry.setEstablishTime(nowTime);
                statEntry.setConnectStackTrace(new Exception());

                dataSourceStat.getConnectionStat().setActiveCount(dataSourceStat.getConnections().size());
            }
        }
        return connection;
    }

 从标红代码中可以看到,果然如此,druid的filter-chain就是这样设计的。FilterManager工具类就不用过多介绍了。

 

oracle interceptor-filter 设计模式

http://www.oracle.com/technetwork/java/interceptingfilter-142169.html

 

 

 

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics