All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkus.smallrye.faulttolerance.runtime.QuarkusFallbackHandlerProvider Maven / Gradle / Ivy

There is a newer version: 3.17.0.CR1
Show newest version
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> 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;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy