Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register build-time configured fault tolerance exceptions for reflection #26480

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,21 @@ void validateFaultToleranceAnnotations(SmallRyeFaultToleranceRecorder recorder,
BeanArchiveIndexBuildItem beanArchiveIndexBuildItem,
AnnotationProxyBuildItem annotationProxy,
BuildProducer<GeneratedClassBuildItem> generatedClasses,
BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
BuildProducer<ValidationPhaseBuildItem.ValidationErrorBuildItem> errors) {

Config config = ConfigProvider.getConfig();

Set<String> exceptionConfigs = Set.of("CircuitBreaker/failOn", "CircuitBreaker/skipOn",
"Fallback/applyOn", "Fallback/skipOn", "Retry/retryOn", "Retry/abortOn");

for (String exceptionConfig : exceptionConfigs) {
Optional<String[]> exceptionNames = config.getOptionalValue(exceptionConfig, String[].class);
if (exceptionNames.isPresent()) {
reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, exceptionNames.get()));
}
}

AnnotationStore annotationStore = validationPhase.getContext().get(BuildExtension.Key.ANNOTATION_STORE);
IndexView index = beanArchiveIndexBuildItem.getIndex();
// only generating annotation literal classes for MicroProfile/SmallRye Fault Tolerance annotations,
Expand All @@ -271,6 +284,14 @@ void validateFaultToleranceAnnotations(SmallRyeFaultToleranceRecorder recorder,
continue;
}

for (String exceptionConfig : exceptionConfigs) {
Optional<String[]> exceptionNames = config.getOptionalValue(beanClass.name().toString()
+ "/" + exceptionConfig, String[].class);
if (exceptionNames.isPresent()) {
reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, exceptionNames.get()));
}
}

if (scaner.hasFTAnnotations(beanClass)) {
scaner.forEachMethod(beanClass, method -> {
FaultToleranceMethod ftMethod = scaner.createFaultToleranceMethod(beanClass, method);
Expand All @@ -281,6 +302,14 @@ void validateFaultToleranceAnnotations(SmallRyeFaultToleranceRecorder recorder,
exceptions.add(
new DefinitionException("Both @Blocking and @NonBlocking present on '" + method + "'"));
}

for (String exceptionConfig : exceptionConfigs) {
Optional<String[]> exceptionNames = config.getOptionalValue(beanClass.name().toString()
+ "/" + method.name() + "/" + exceptionConfig, String[].class);
if (exceptionNames.isPresent()) {
reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, exceptionNames.get()));
}
}
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@ public String getName() {
return counter + ":" + name;
}

@GET
@Path("/retried")
public String retried() {
AtomicInteger counter = new AtomicInteger();
String name = service.retriedMethod(counter);
return counter + ":" + name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.quarkus.it.faulttolerance;

public class MyFaultToleranceError extends Error {
public MyFaultToleranceError() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;

import org.eclipse.microprofile.faulttolerance.Retry;

import io.smallrye.faulttolerance.api.ApplyFaultTolerance;

@ApplicationScoped
Expand All @@ -27,4 +29,11 @@ public String getName(AtomicInteger counter) {
throw new IllegalStateException("Counter=" + counter.get());
}

@Retry // set of `retryOn` exceptions is configured in application.properties
public String retriedMethod(AtomicInteger counter) {
if (counter.incrementAndGet() >= THRESHOLD) {
return name;
}
throw new MyFaultToleranceError();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ restClientConfigKey/mp-rest/url=${test.url}
restClientBaseUriConfigKey/mp-rest/url=${test.url}
loopback/mp-rest/url=${test.url}/loopback

io.quarkus.it.faulttolerance.Service/retriedMethod/Retry/retryOn=io.quarkus.it.faulttolerance.MyFaultToleranceError

org.eclipse.microprofile.rest.client.propagateHeaders=header-name
# Disabled by default as it establishes external connections.
# Uncomment when you want to test SSL support.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,10 @@ public void testRetry() throws Exception {
.given().baseUri(uri.toString())
.when().get()
.then().body(is("2:Lucie"));

RestAssured
.given().baseUri(uri.toString() + "/retried")
.when().get()
.then().body(is("2:Lucie"));
}
}