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

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

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

import java.util.concurrent.ExecutorService;
import java.util.function.Function;

import javax.enterprise.inject.spi.CDI;
import javax.inject.Inject;
import javax.inject.Singleton;

import org.eclipse.microprofile.config.inject.ConfigProperty;

import io.smallrye.faulttolerance.api.CircuitBreakerMaintenance;
import io.smallrye.faulttolerance.api.FaultTolerance;
import io.smallrye.faulttolerance.api.FaultToleranceSpi;
import io.smallrye.faulttolerance.core.apiimpl.BasicCircuitBreakerMaintenanceImpl;
import io.smallrye.faulttolerance.core.apiimpl.BuilderEagerDependencies;
import io.smallrye.faulttolerance.core.apiimpl.BuilderLazyDependencies;
import io.smallrye.faulttolerance.core.apiimpl.FaultToleranceImpl;
import io.smallrye.faulttolerance.core.event.loop.EventLoop;
import io.smallrye.faulttolerance.core.timer.Timer;

public class CdiFaultToleranceSpi implements FaultToleranceSpi {
    @Singleton
    public static class EagerDependencies implements BuilderEagerDependencies {
        @Inject
        CircuitBreakerMaintenanceImpl cbMaintenance;

        @Override
        public BasicCircuitBreakerMaintenanceImpl cbMaintenance() {
            return cbMaintenance;
        }
    }

    @Singleton
    public static class LazyDependencies implements BuilderLazyDependencies {
        @Inject
        @ConfigProperty(name = "MP_Fault_Tolerance_NonFallback_Enabled", defaultValue = "true")
        boolean ftEnabled;

        @Inject
        ExecutorHolder executorHolder;

        @Inject
        CircuitBreakerMaintenanceImpl cbMaintenance;

        @Override
        public boolean ftEnabled() {
            return ftEnabled;
        }

        @Override
        public ExecutorService asyncExecutor() {
            return executorHolder.getAsyncExecutor();
        }

        @Override
        public EventLoop eventLoop() {
            return executorHolder.getEventLoop();
        }

        @Override
        public Timer timer() {
            return executorHolder.getTimer();
        }
    }

    private BuilderEagerDependencies eagerDependencies() {
        // always lookup from current CDI container, because there's no guarantee that `CDI.current()` will always be
        // the same (in certain environments, e.g. in the test suite, CDI containers come and go freely)
        return CDI.current().select(EagerDependencies.class).get();
    }

    private BuilderLazyDependencies lazyDependencies() {
        // always lookup from current CDI container, because there's no guarantee that `CDI.current()` will always be
        // the same (in certain environments, e.g. in the test suite, CDI containers come and go freely)
        return CDI.current().select(LazyDependencies.class).get();
    }

    @Override
    public boolean applies() {
        try {
            CDI.current();
            return true;
        } catch (Exception ignored) {
            return false;
        }
    }

    @Override
    public int priority() {
        return 1000;
    }

    @Override
    public  FaultTolerance.Builder newBuilder(Function, R> finisher) {
        return new FaultToleranceImpl.BuilderImpl<>(eagerDependencies(), this::lazyDependencies, null, finisher);
    }

    @Override
    public  FaultTolerance.Builder newAsyncBuilder(Class asyncType, Function, R> finisher) {
        return new FaultToleranceImpl.BuilderImpl<>(eagerDependencies(), this::lazyDependencies, asyncType, finisher);
    }

    @Override
    public CircuitBreakerMaintenance circuitBreakerMaintenance() {
        return eagerDependencies().cbMaintenance();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy