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

net.yudichev.jiotty.appliance.RetryingAppliance Maven / Gradle / Ivy

package net.yudichev.jiotty.appliance;

import com.google.inject.BindingAnnotation;
import net.yudichev.jiotty.common.async.backoff.RetryableOperationExecutor;

import javax.inject.Inject;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

public final class RetryingAppliance implements Appliance {
    private final Appliance delegate;
    private final RetryableOperationExecutor retryableOperationExecutor;

    @Inject
    public RetryingAppliance(@Dependency Appliance delegate,
                             @Dependency RetryableOperationExecutor retryableOperationExecutor) {
        this.delegate = checkNotNull(delegate);
        this.retryableOperationExecutor = checkNotNull(retryableOperationExecutor);
    }

    @Override
    public CompletableFuture execute(Command command) {
        return retryableOperationExecutor.withBackOffAndRetry(
                "execute " + command + " on " + delegate,
                () -> delegate.execute(command));
    }

    @Override
    public Set> getAllSupportedCommandMetadata() {
        return delegate.getAllSupportedCommandMetadata();
    }

    @BindingAnnotation
    @Target({FIELD, PARAMETER, METHOD})
    @Retention(RUNTIME)
    @interface Dependency {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy