zoukankan      html  css  js  c++  java
  • spring控制并发数的工具类ConcurrencyThrottleSupport和ConcurrencyThrottleInterceptor

    官方文档:

    /**
     * Support class for throttling concurrent access to a specific resource.
     *
     * <p>Designed for use as a base class, with the subclass invoking
     * the {@link #beforeAccess()} and {@link #afterAccess()} methods at
     * appropriate points of its workflow. Note that {@code afterAccess}
     * should usually be called in a finally block!
     *
     * <p>The default concurrency limit of this support class is -1
     * ("unbounded concurrency"). Subclasses may override this default;
     * check the javadoc of the concrete class that you're using.
     *
     * @author Juergen Hoeller
     * @since 1.2.5
     * @see #setConcurrencyLimit
     * @see #beforeAccess()
     * @see #afterAccess()
     * @see org.springframework.aop.interceptor.ConcurrencyThrottleInterceptor
     * @see java.io.Serializable
     */

    beforeAccess()实现

    /**
         * To be invoked before the main execution logic of concrete subclasses.
         * <p>This implementation applies the concurrency throttle.
         * @see #afterAccess()
         */
        protected void beforeAccess() {
            if (this.concurrencyLimit == NO_CONCURRENCY) {
                throw new IllegalStateException(
                        "Currently no invocations allowed - concurrency limit set to NO_CONCURRENCY");
            }
            if (this.concurrencyLimit > 0) {
                boolean debug = logger.isDebugEnabled();
                synchronized (this.monitor) {
                    boolean interrupted = false;
                    while (this.concurrencyCount >= this.concurrencyLimit) {
                        if (interrupted) {
                            throw new IllegalStateException("Thread was interrupted while waiting for invocation access, " +
                                    "but concurrency limit still does not allow for entering");
                        }
                        if (debug) {
                            logger.debug("Concurrency count " + this.concurrencyCount +
                                    " has reached limit " + this.concurrencyLimit + " - blocking");
                        }
                        try {
                            this.monitor.wait();
                        }
                        catch (InterruptedException ex) {
                            // Re-interrupt current thread, to allow other threads to react.
                            Thread.currentThread().interrupt();
                            interrupted = true;
                        }
                    }
                    if (debug) {
                        logger.debug("Entering throttle at concurrency count " + this.concurrencyCount);
                    }
                    this.concurrencyCount++;
                }
            }
        }

    afterAccess()实现

        /**
         * To be invoked after the main execution logic of concrete subclasses.
         * @see #beforeAccess()
         */
        protected void afterAccess() {
            if (this.concurrencyLimit >= 0) {
                synchronized (this.monitor) {
                    this.concurrencyCount--;
                    if (logger.isDebugEnabled()) {
                        logger.debug("Returning from throttle at concurrency count " + this.concurrencyCount);
                    }
                    this.monitor.notify();
                }
            }
        }

    ConcurrencyThrottleSupport是个抽象类,其具体的实现类ConcurrencyThrottleInterceptor

    /**
     * Interceptor that throttles concurrent access, blocking invocations
     * if a specified concurrency limit is reached.
     *
     * <p>Can be applied to methods of local services that involve heavy use
     * of system resources, in a scenario where it is more efficient to
     * throttle concurrency for a specific service rather than restricting
     * the entire thread pool (e.g. the web container's thread pool).
     *
     * <p>The default concurrency limit of this interceptor is 1.
     * Specify the "concurrencyLimit" bean property to change this value.
     *
     * @author Juergen Hoeller
     * @since 11.02.2004
     * @see #setConcurrencyLimit
     */
    @SuppressWarnings("serial")
    public class ConcurrencyThrottleInterceptor extends ConcurrencyThrottleSupport
            implements MethodInterceptor, Serializable {
    
        public ConcurrencyThrottleInterceptor() {
            setConcurrencyLimit(1);
        }
    
        @Override
        public Object invoke(MethodInvocation methodInvocation) throws Throwable {
            beforeAccess();
            try {
                return methodInvocation.proceed();
            }
            finally {
                afterAccess();
            }
        }
    
    }
  • 相关阅读:
    使用comet架构实现了一个基于网页的视频监控prototype!!!!哇哈哈庆祝一下
    Pixysoft.Framework.Noebe.Datamining 数据挖掘开发实录
    论创业成功!让大家的青春充满着无限美好的回忆
    新年第一篇 数据库备份恢复系统上线的挫折
    .Net FrameWork 4.0中使用EF向数据库插入数据报datatime2类型错误的解决办法
    RoRoWoBlog 开源博客系统介绍
    第一次偶然出现的“System.Data.Entity.dll”类型的异常
    序列化类型 System.Data.Entity.DynamicProxies 的对象时检测到循环引用
    我也来说说Entity Frame Work 4中的数据库优先和代码优先两种方式(2)
    Asp.net MVC 2 + Castle + NHibernate 项目实战(1)
  • 原文地址:https://www.cnblogs.com/davidwang456/p/5998254.html
Copyright © 2011-2022 走看看