Skip to content

Commit

Permalink
Merge pull request #8 from agorapulse/feature/sc-67158-move-order-of-…
Browse files Browse the repository at this point in the history
…rethrow-interceptor

[sc67158] Move Rethrow interceptor around Retryable interceptor.
  • Loading branch information
musketyr authored Feb 24, 2022
2 parents 756f0d4 + ff42a0f commit 6a01343
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@
@Type(RethrowInterceptor.class)
public @interface Rethrow {

/**
* This position moves the {@link Rethrow} annotation around {@link io.micronaut.retry.annotation.Retryable} interceptor.
*
* If {@link io.micronaut.retry.annotation.Retryable} and {@link Rethrow} is used on the same method then then
* rethrowin the exception will happen after the last attempt from the {@link io.micronaut.retry.annotation.Retryable}
* annotation.
*/
int RETHROW_POSITION = -200;

/**
* @return Function or closure which coverts the thrown exception to new one. New exception must extend {@link RuntimeException}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public class RethrowInterceptor implements MethodInterceptor<Object, Object> {

private static final RethrowAsRuntimeException DEFAULT_CONVERTER = new RethrowAsRuntimeException();

@Override
public int getOrder() {
return Rethrow.RETHROW_POSITION;
}

@Override
@SuppressWarnings("unchecked")
public Object intercept(MethodInvocationContext<Object, Object> context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class RethrowInterceptorSpec extends Specification {
@Inject
RethrowingJavaService rethrowingJavaService

@Inject
RetryableService retryableService

void 'simple test'() {
when:
rethrowingService.simpleRethrow()
Expand Down Expand Up @@ -115,4 +118,14 @@ class RethrowInterceptorSpec extends Specification {
thrown(UnsupportedOperationException)
}

void 'rethrow and retryable'() {
when:
retryableService.retryOnException()

then:
thrown(IllegalStateException)

retryableService.counter == 4
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2019-2022 Agorapulse.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.agorapulse.micronaut.rethrow;

import io.micronaut.retry.annotation.Retryable;

import javax.inject.Singleton;

@Singleton
public class RetryableService {

private int counter;

@Rethrow(
only = UnsupportedOperationException.class,
as = IllegalStateException.class
)
@Retryable
void retryOnException() {
counter++;
throw new UnsupportedOperationException("Not Supported");
}

public int getCounter() {
return counter;
}
}

0 comments on commit 6a01343

Please sign in to comment.