io.quarkus.smallrye.faulttolerance.runtime.QuarkusFallbackHandlerProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-smallrye-fault-tolerance Show documentation
Show all versions of quarkus-smallrye-fault-tolerance Show documentation
Build fault-tolerant network services
package io.quarkus.smallrye.faulttolerance.runtime;
import jakarta.annotation.Priority;
import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.inject.Alternative;
import jakarta.enterprise.inject.Any;
import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;
import org.eclipse.microprofile.faulttolerance.ExecutionContext;
import org.eclipse.microprofile.faulttolerance.FallbackHandler;
import io.smallrye.faulttolerance.FallbackHandlerProvider;
import io.smallrye.faulttolerance.config.FaultToleranceOperation;
@Dependent
@Alternative
@Priority(1)
public class QuarkusFallbackHandlerProvider implements FallbackHandlerProvider {
@Inject
@Any
Instance> instance;
@Override
public FallbackHandler get(FaultToleranceOperation operation) {
if (operation.hasFallback()) {
return new FallbackHandler() {
@SuppressWarnings("unchecked")
@Override
public T handle(ExecutionContext context) {
Class extends FallbackHandler>> clazz = operation.getFallback().value();
FallbackHandler fallbackHandlerInstance = (FallbackHandler) instance.select(clazz).get();
try {
return fallbackHandlerInstance.handle(context);
} finally {
// The instance exists to service a single invocation only
instance.destroy(fallbackHandlerInstance);
}
}
};
}
return null;
}
}