2012-11-22 38 views
1

我想限制Spring應用程序中的併發方法調用。使用Spring aop限制併發方法調用

interceptor這個和here使用這個攔截器的例子。 但問題是,方法(它需要被限制)不在一個bean中,我每次需要調用方法時都會創建一個新對象。 在這種情況下是否有可能實現限制?

+1

對象同步策略是對象設計的固有部分,而AOP是關於解決橫切關注點,我敢肯定,必須有實現這種更直接的方法,例如設計一個可以接受請求並在隊列中處理它們的服務。即同步信息(使用哪些鎖等)不應從類源文件中刪除。 –

回答

2

您可以使用Load-time weaving with 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(){} 
} 
+0

這是工程,但裏面一個模塊(項目) – vacuum

相關問題