Search in sources :

Example 1 with BootstrapRegistry

use of org.springframework.boot.BootstrapRegistry in project spring-cloud-consul by spring-cloud.

the class ConsulRetryBootstrapper method initialize.

@Override
public void initialize(BootstrapRegistry registry) {
    if (!RETRY_IS_PRESENT) {
        return;
    }
    registry.registerIfAbsent(RetryProperties.class, context -> context.get(Binder.class).bind(RetryProperties.PREFIX, RetryProperties.class).orElseGet(RetryProperties::new));
    registry.registerIfAbsent(RetryTemplate.class, context -> {
        RetryProperties properties = context.get(RetryProperties.class);
        if (properties.isEnabled()) {
            return RetryTemplate.builder().maxAttempts(properties.getMaxAttempts()).exponentialBackoff(properties.getInitialInterval(), properties.getMultiplier(), properties.getMaxInterval()).build();
        }
        return null;
    });
    registry.registerIfAbsent(LoaderInterceptor.class, context -> {
        RetryTemplate retryTemplate = context.get(RetryTemplate.class);
        if (retryTemplate != null) {
            return loadContext -> retryTemplate.execute(retryContext -> loadContext.getInvocation().apply(loadContext.getLoaderContext(), loadContext.getResource()));
        }
        // disabled
        return null;
    });
}
Also used : RetryProperties(org.springframework.cloud.consul.RetryProperties) BootstrapRegistryInitializer(org.springframework.boot.BootstrapRegistryInitializer) BootstrapRegistry(org.springframework.boot.BootstrapRegistry) ClassUtils(org.springframework.util.ClassUtils) Binder(org.springframework.boot.context.properties.bind.Binder) RetryTemplate(org.springframework.retry.support.RetryTemplate) RetryProperties(org.springframework.cloud.consul.RetryProperties) LoaderInterceptor(org.springframework.cloud.consul.config.ConsulBootstrapper.LoaderInterceptor) Binder(org.springframework.boot.context.properties.bind.Binder) RetryTemplate(org.springframework.retry.support.RetryTemplate)

Aggregations

BootstrapRegistry (org.springframework.boot.BootstrapRegistry)1 BootstrapRegistryInitializer (org.springframework.boot.BootstrapRegistryInitializer)1 Binder (org.springframework.boot.context.properties.bind.Binder)1 RetryProperties (org.springframework.cloud.consul.RetryProperties)1 LoaderInterceptor (org.springframework.cloud.consul.config.ConsulBootstrapper.LoaderInterceptor)1 RetryTemplate (org.springframework.retry.support.RetryTemplate)1 ClassUtils (org.springframework.util.ClassUtils)1