1

I want to limit concurrent method invocation in spring application.

There is interceptor for this and here the example of using this interceptor. But the problem is that method(which need to be limited) is not in a bean, I am creating new object every time I need to call method. Is there is possibility to achieve limitation in this case?

4

1 回答 1

2

您可以将Load-time weaving 与 AspectJ 一起使用,并编写一个自定义aspect来进行节流。

例子

@Aspect
public class ThrottlingAspect {
    private static final int MAX_CONCURRENT_INVOCATIONS = 20;
    private final Semaphore throttle = new Semaphore (MAX_CONCURRENT_INVOCATIONS, true);

    @Around("methodsToBeThrottled()")
    public Object profile(ProceedingJoinPoint pjp) throws Throwable {
        throttle.acquire ();
        try {
            return pjp.proceed ();
        }
        finally {
            throttle.release ();
        }
    }

    @Pointcut("execution(public * foo..*.*(..))")
    public void methodsToBeThrottled(){}
}
于 2012-11-22T15:05:27.273 回答