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

io.smallrye.faulttolerance.DefaultFallbackHandlerProvider Maven / Gradle / Ivy

There is a newer version: 6.6.2
Show newest version
package io.smallrye.faulttolerance;

import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.inject.spi.BeanManager;
import jakarta.enterprise.inject.spi.Unmanaged;
import jakarta.inject.Inject;

import org.eclipse.microprofile.faulttolerance.ExecutionContext;
import org.eclipse.microprofile.faulttolerance.FallbackHandler;

import io.smallrye.faulttolerance.config.FaultToleranceOperation;

/**
 * Default implementation of {@link FallbackHandlerProvider}.
 *
 * @author Martin Kouba
 */
@Dependent
public class DefaultFallbackHandlerProvider implements FallbackHandlerProvider {

    @Inject
    BeanManager beanManager;

    @Override
    public  FallbackHandler get(FaultToleranceOperation operation) {
        if (operation.hasFallback()) {
            //noinspection Convert2Lambda
            return new FallbackHandler() {
                @Override
                public T handle(ExecutionContext context) {
                    Unmanaged> unmanaged = new Unmanaged<>(beanManager,
                            (Class>) operation.getFallback().value());
                    Unmanaged.UnmanagedInstance> unmanagedInstance = unmanaged.newInstance();
                    FallbackHandler handler = unmanagedInstance.produce().inject().postConstruct().get();
                    try {
                        return handler.handle(context);
                    } finally {
                        // The instance exists to service a single invocation only
                        unmanagedInstance.preDestroy().dispose();
                    }
                }
            };
        }
        return null;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy