2016-04-22 13 views
0

メソッドhelloWorldにアドバイスを適用したいと思います。私は 'x' TPSを超える通話を抑制し、それらの通話を再試行したいと思います。コールレートは、コールが絞られた場合に再試行「はx」の Spring AOP - 複数のインターセプタが連携していません

  • 再試行インターセプタ、超え絞る

    1. スロットルインターセプタ、 -

      は、だから私は2つのインターセプタを持っています。

    問題は、私は2つのインターセプタを組み合わせたときにスロットル インターセプターは、私は0としてレートを提供しても仕事をいないようだということで、それが実行されますのHelloWorld methosです。

    私が を使用している場合は、Throttling Interceptorが正常に動作します。

    私のSpring Configurationはこのようなものです。

    <bean id="retryPolicy" class="xyz.RetryPolicyFactoryBean"> 
        <property name="backoffCoefficient"><value>2</value></property> 
        <property name="multiplierMillis"><value>100</value></property> 
        <property name="maxDelayMillis"><value>2000</value></property> 
        <property name="maxAttempts"><value>2</value></property> 
        <property name="expirationDurationMillis"><value>60000</value></property> 
    
    <bean id="retryInterceptor" class="xyz.RetryInterceptor"> 
    <constructor-arg index="0"><ref bean="retryPolicy"/></constructor-arg> 
    </bean> 
    
    <bean id="throttlingInterceptor" class="xyz.ThrottlingInterceptor"> 
        <constructor-arg index="0"><value>key</value></constructor-arg> 
    </bean> 
    
    <bean id="helloWorld" class="xyz.helloWorld" /> 
    
    <bean id="helloWorldProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> 
        <property name="target" ref="helloWorld" /> 
        <property name="interceptorNames"> 
         <list> 
          <value>retryInterceptor</value> 
          <value>throttlingInterceptor</value> 
         </list> 
        </property> 
    </bean> 
    

    Throttling Interceptorこのようなものです:

    @Override 
    public Object invoke(MethodInvocation methodInvocation) throws Throwable { 
        if (isDecorated(methodInvocation)) { 
         if (throttler.isThrottled(key)) { 
    
          throw new ThrottlingException("Call throttled."); 
         } 
        } 
        return methodInvocation.proceed(); 
    } 
    

    retry interceptorは、このようなものです:私はここに

    public class RetryInterceptor extends RetryAdvice { 
    
    public RetryInterceptor(RetryPolicy retryPolicy) { 
        super(retryPolicy); 
    } 
    
    @Override 
    public Object invoke(final MethodInvocation methodInvocation) throws Throwable { 
        return super.invoke(methodInvocation); 
    } 
    

    何をしないのですか?

  • +0

    「RetryAdvice」を拡張することは何ですか?そして、なぜあなたはAOPを適用する古い方法を使用していますか? 'RetryAdvice'とは何ですか? –

    +0

    @ M.Deinum私はSpring AOPの初心者ですので、AOPを使用する標準的な方法を教えていただければ幸いです。再試行アドバイスは、再試行ポリシーを適用するクラスです。それは、それに提供されたRetryPolicyを単に適用し、methodInvocation.proceed()を呼び出すinvoke()メソッドを持っています。再試行を使用するより良い方法はありますか? – AgentX

    +0

    しかし、なぜそれを拡張していますか?あなたの拡張機能は何も追加しません...オーバーライドとコンストラクタは意味をなさない...しかし、あなたはAOPを指定するために ''を使いたいと思うでしょう。あなたは手動で 'ProxyFactoryBean各AOP Beanに対して ''それはエラーが起こりやすく、例外が作られるでしょう。 –

    答えて

    0

    私はAspectJ Annotations &を使ってより新しいアプローチに従いました(今は少なくともインターセプタが呼び出されています)。

    注釈ここ@Aspect

    とのアドバイスが含まれているクラスは、私のスロットルアドバイスです:

    @Before(pointcut="execution(* com.company.xyz.method())") 
    public void invoke() throws ThrottlingException { 
        if (throttler.isThrottled(throttleKey)) { 
         throw new ThrottlingException("Call Throttled"); 
        } 
    } 
    

    マイ再試行インターセプタ:

    @AfterThrowing(pointcut="execution(* com.company.xyz.method())", throwing="exception") 
    public Object invoke(JoinPoint jp, ThrottlingException exception) throws Throwable { 
    return RetryingCallable.newRetryingCallable(new Callable<Object>() { 
    
        @Override 
        public Object call() throws Exception { 
          MethodSignature signature = (MethodSignature) p.getSignature(); 
          Method method = signature.getMethod(); 
          return method.invoke(jp.getThis(), (Object[]) null); 
        } 
    
    }, retryPolicy).call(); 
    

    }

    関連春コンフィグ

    <bean id="retryInterceptor" class="com.company.xyz.RetryInterceptor"> 
    <constructor-arg index="0"><ref bean="retryPolicy"/></constructor-arg> 
    </bean> 
    
    
    <bean id="throttlingInterceptor" class="com.company.xyz.ThrottlingInterceptor"> 
    <constructor-arg><value>throttleKey</value></constructor-arg> 
    </bean> 
    
    <aop:aspectj-autoproxy/> 
    

    もう1つの注意点は、インターセプタが呼び出されるOrderです。 @Order注釈を使用して順序を確認します。

    関連する問題