001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.aop.interceptor;
018
019import java.io.Serializable;
020
021import org.aopalliance.intercept.MethodInterceptor;
022import org.aopalliance.intercept.MethodInvocation;
023
024import org.springframework.util.ConcurrencyThrottleSupport;
025
026/**
027 * Interceptor that throttles concurrent access, blocking invocations
028 * if a specified concurrency limit is reached.
029 *
030 * <p>Can be applied to methods of local services that involve heavy use
031 * of system resources, in a scenario where it is more efficient to
032 * throttle concurrency for a specific service rather than restricting
033 * the entire thread pool (e.g. the web container's thread pool).
034 *
035 * <p>The default concurrency limit of this interceptor is 1.
036 * Specify the "concurrencyLimit" bean property to change this value.
037 *
038 * @author Juergen Hoeller
039 * @since 11.02.2004
040 * @see #setConcurrencyLimit
041 */
042@SuppressWarnings("serial")
043public class ConcurrencyThrottleInterceptor extends ConcurrencyThrottleSupport
044                implements MethodInterceptor, Serializable {
045
046        public ConcurrencyThrottleInterceptor() {
047                setConcurrencyLimit(1);
048        }
049
050        @Override
051        public Object invoke(MethodInvocation methodInvocation) throws Throwable {
052                beforeAccess();
053                try {
054                        return methodInvocation.proceed();
055                }
056                finally {
057                        afterAccess();
058                }
059        }
060
061}