Search in sources :

Example 6 with Retryable

use of org.springframework.retry.annotation.Retryable in project paascloud-master by paascloud.

the class OptQiniuOssServiceImpl method deleteFile.

@Override
@Retryable(value = Exception.class, backoff = @Backoff(delay = 5000, multiplier = 2))
public void deleteFile(String fileName, String bucketName) throws QiniuException {
    log.info("deleteFile - 删除OSS文件. fileName={}, bucketName={}", fileName, bucketName);
    Preconditions.checkArgument(StringUtils.isNotEmpty(fileName), ErrorCodeEnum.OPC10040010.msg());
    Preconditions.checkArgument(StringUtils.isNotEmpty(bucketName), "存储空间不能为空");
    Response response = bucketManager.delete(bucketName, fileName);
    log.info("deleteFile - 删除OSS文件. [OK] response={}", response);
}
Also used : Response(com.qiniu.http.Response) Retryable(org.springframework.retry.annotation.Retryable)

Example 7 with Retryable

use of org.springframework.retry.annotation.Retryable in project spring-cloud-consul by spring-cloud.

the class ConsulPropertySourceLocator method locate.

@Override
@Retryable(interceptor = "consulRetryInterceptor")
public PropertySource<?> locate(Environment environment) {
    if (environment instanceof ConfigurableEnvironment) {
        ConfigurableEnvironment env = (ConfigurableEnvironment) environment;
        String appName = properties.getName();
        if (appName == null) {
            appName = env.getProperty("spring.application.name");
        }
        List<String> profiles = Arrays.asList(env.getActiveProfiles());
        String prefix = this.properties.getPrefix();
        List<String> suffixes = new ArrayList<>();
        if (this.properties.getFormat() != FILES) {
            suffixes.add("/");
        } else {
            suffixes.add(".yml");
            suffixes.add(".yaml");
            suffixes.add(".properties");
        }
        String defaultContext = getContext(prefix, this.properties.getDefaultContext());
        for (String suffix : suffixes) {
            this.contexts.add(defaultContext + suffix);
        }
        for (String suffix : suffixes) {
            addProfiles(this.contexts, defaultContext, profiles, suffix);
        }
        String baseContext = getContext(prefix, appName);
        for (String suffix : suffixes) {
            this.contexts.add(baseContext + suffix);
        }
        for (String suffix : suffixes) {
            addProfiles(this.contexts, baseContext, profiles, suffix);
        }
        Collections.reverse(this.contexts);
        CompositePropertySource composite = new CompositePropertySource("consul");
        for (String propertySourceContext : this.contexts) {
            try {
                ConsulPropertySource propertySource = null;
                if (this.properties.getFormat() == FILES) {
                    Response<GetValue> response = this.consul.getKVValue(propertySourceContext, this.properties.getAclToken());
                    addIndex(propertySourceContext, response.getConsulIndex());
                    if (response.getValue() != null) {
                        ConsulFilesPropertySource filesPropertySource = new ConsulFilesPropertySource(propertySourceContext, this.consul, this.properties);
                        filesPropertySource.init(response.getValue());
                        propertySource = filesPropertySource;
                    }
                } else {
                    propertySource = create(propertySourceContext, contextIndex);
                }
                if (propertySource != null) {
                    composite.addPropertySource(propertySource);
                }
            } catch (Exception e) {
                if (this.properties.isFailFast()) {
                    log.error("Fail fast is set and there was an error reading configuration from consul.");
                    ReflectionUtils.rethrowRuntimeException(e);
                } else {
                    log.warn("Unable to load consul config from " + propertySourceContext, e);
                }
            }
        }
        return composite;
    }
    return null;
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) CompositePropertySource(org.springframework.core.env.CompositePropertySource) ArrayList(java.util.ArrayList) GetValue(com.ecwid.consul.v1.kv.model.GetValue) Retryable(org.springframework.retry.annotation.Retryable)

Example 8 with Retryable

use of org.springframework.retry.annotation.Retryable in project spring-cloud-config by spring-cloud.

the class ConfigServicePropertySourceLocator method locate.

@Override
@Retryable(interceptor = "configServerRetryInterceptor")
public org.springframework.core.env.PropertySource<?> locate(org.springframework.core.env.Environment environment) {
    ConfigClientProperties properties = this.defaultProperties.override(environment);
    CompositePropertySource composite = new CompositePropertySource("configService");
    RestTemplate restTemplate = this.restTemplate == null ? getSecureRestTemplate(properties) : this.restTemplate;
    Exception error = null;
    String errorBody = null;
    logger.info("Fetching config from server at: " + properties.getRawUri());
    try {
        String[] labels = new String[] { "" };
        if (StringUtils.hasText(properties.getLabel())) {
            labels = StringUtils.commaDelimitedListToStringArray(properties.getLabel());
        }
        String state = ConfigClientStateHolder.getState();
        // Try all the labels until one works
        for (String label : labels) {
            Environment result = getRemoteEnvironment(restTemplate, properties, label.trim(), state);
            if (result != null) {
                log(result);
                if (result.getPropertySources() != null) {
                    // result.getPropertySources() can be null if using xml
                    for (PropertySource source : result.getPropertySources()) {
                        @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) source.getSource();
                        composite.addPropertySource(new MapPropertySource(source.getName(), map));
                    }
                }
                if (StringUtils.hasText(result.getState()) || StringUtils.hasText(result.getVersion())) {
                    HashMap<String, Object> map = new HashMap<>();
                    putValue(map, "config.client.state", result.getState());
                    putValue(map, "config.client.version", result.getVersion());
                    composite.addFirstPropertySource(new MapPropertySource("configClient", map));
                }
                return composite;
            }
        }
    } catch (HttpServerErrorException e) {
        error = e;
        if (MediaType.APPLICATION_JSON.includes(e.getResponseHeaders().getContentType())) {
            errorBody = e.getResponseBodyAsString();
        }
    } catch (Exception e) {
        error = e;
    }
    if (properties.isFailFast()) {
        throw new IllegalStateException("Could not locate PropertySource and the fail fast property is set, failing", error);
    }
    logger.warn("Could not locate PropertySource: " + (errorBody == null ? error == null ? "label not found" : error.getMessage() : errorBody));
    return null;
}
Also used : HashMap(java.util.HashMap) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) IOException(java.io.IOException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) PropertySource(org.springframework.cloud.config.environment.PropertySource) CompositePropertySource(org.springframework.core.env.CompositePropertySource) MapPropertySource(org.springframework.core.env.MapPropertySource) CompositePropertySource(org.springframework.core.env.CompositePropertySource) MapPropertySource(org.springframework.core.env.MapPropertySource) RestTemplate(org.springframework.web.client.RestTemplate) Environment(org.springframework.cloud.config.environment.Environment) HashMap(java.util.HashMap) Map(java.util.Map) Retryable(org.springframework.retry.annotation.Retryable)

Example 9 with Retryable

use of org.springframework.retry.annotation.Retryable in project herd by FINRAOS.

the class CredStashHelper method getCredentialFromCredStash.

/**
 * Gets a password from the credstash.
 *
 * @param credStashEncryptionContext the encryption context
 * @param credentialName the credential name
 *
 * @return the password
 * @throws CredStashGetCredentialFailedException if CredStash fails to get a credential
 */
@Retryable(maxAttempts = 3, value = CredStashGetCredentialFailedException.class, backoff = @Backoff(delay = 5000, multiplier = 2))
public String getCredentialFromCredStash(String credStashEncryptionContext, String credentialName) throws CredStashGetCredentialFailedException {
    // Get the credstash table name and credential names for the keystore and truststore.
    String credStashAwsRegion = configurationHelper.getProperty(ConfigurationValue.CREDSTASH_AWS_REGION_NAME);
    String credStashTableName = configurationHelper.getProperty(ConfigurationValue.CREDSTASH_TABLE_NAME);
    // Log configuration values and input parameters.
    LOGGER.info("credStashAwsRegion={} credStashTableName={} credStashEncryptionContext={} credentialName={}", credStashAwsRegion, credStashTableName, credStashEncryptionContext, credentialName);
    // Get the AWS client configuration.
    ClientConfiguration clientConfiguration = awsHelper.getClientConfiguration(awsHelper.getAwsParamsDto());
    // Get the keystore and truststore passwords from Credstash.
    CredStash credstash = credStashFactory.getCredStash(credStashAwsRegion, credStashTableName, clientConfiguration);
    // Try to obtain the credentials from cred stash.
    String password = null;
    String errorMessage = null;
    try {
        // Convert the JSON config file version of the encryption context to a Java Map class.
        @SuppressWarnings("unchecked") Map<String, String> credstashEncryptionContextMap = jsonHelper.unmarshallJsonToObject(Map.class, credStashEncryptionContext);
        // Get the keystore and truststore passwords from credstash.
        password = credstash.getCredential(credentialName, credstashEncryptionContextMap);
    } catch (Exception exception) {
        LOGGER.error("Caught exception when attempting to get a credential value from CredStash.", exception);
        errorMessage = exception.getMessage();
    }
    // as credentials from cred stash, then throw a CredStashGetCredentialFailedException.
    if (StringUtils.isEmpty(password)) {
        throw new CredStashGetCredentialFailedException(String.format("Failed to obtain the keystore or truststore credential from credstash.%s " + "credStashAwsRegion=%s credStashTableName=%s credStashEncryptionContext=%s credentialName=%s", StringUtils.isNotBlank(errorMessage) ? " Reason: " + errorMessage : "", credStashAwsRegion, credStashTableName, credStashEncryptionContext, credentialName));
    }
    // Return the keystore and truststore passwords in a map.
    return password;
}
Also used : CredStash(org.finra.herd.dao.credstash.CredStash) ClientConfiguration(com.amazonaws.ClientConfiguration) CredStashGetCredentialFailedException(org.finra.herd.dao.exception.CredStashGetCredentialFailedException) CredStashGetCredentialFailedException(org.finra.herd.dao.exception.CredStashGetCredentialFailedException) Retryable(org.springframework.retry.annotation.Retryable)

Example 10 with Retryable

use of org.springframework.retry.annotation.Retryable in project cas by apereo.

the class AccepttoMultifactorValidateUserDeviceRegistrationAction method verifyUserDeviceIsPaired.

/**
 * Verify user device is paired.
 *
 * @return true/false
 */
@Retryable(value = AccepttoUserDeviceRegistrationException.class, maxAttempts = 2, backoff = @Backoff(delay = 1000, maxDelay = 3000))
public boolean verifyUserDeviceIsPaired() {
    val acceptto = casProperties.getAuthn().getMfa().getAcceptto();
    val authentication = WebUtils.getInProgressAuthentication();
    if (!AccepttoApiUtils.isUserDevicePaired(authentication, acceptto)) {
        val email = AccepttoApiUtils.getUserEmail(authentication, acceptto);
        throw new AccepttoUserDeviceRegistrationException("Could not locate registered device for " + email);
    }
    return true;
}
Also used : lombok.val(lombok.val) Retryable(org.springframework.retry.annotation.Retryable)

Aggregations

Retryable (org.springframework.retry.annotation.Retryable)10 IOException (java.io.IOException)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 ArrayList (java.util.ArrayList)2 Response (javax.ws.rs.core.Response)2 lombok.val (lombok.val)2 CompositePropertySource (org.springframework.core.env.CompositePropertySource)2 ClientConfiguration (com.amazonaws.ClientConfiguration)1 GetValue (com.ecwid.consul.v1.kv.model.GetValue)1 JavaTimeModule (com.fasterxml.jackson.datatype.jsr310.JavaTimeModule)1 JsonObject (com.google.gson.JsonObject)1 SystemEvent (com.icthh.xm.ms.entity.domain.kafka.SystemEvent)1 ServicePrincipalInner (com.microsoft.azure.management.graphrbac.implementation.ServicePrincipalInner)1 Response (com.qiniu.http.Response)1 AzureRoleDefinitionListResponse (com.sequenceiq.cloudbreak.cloud.azure.AzureRoleDefinitionListResponse)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Client (javax.ws.rs.client.Client)1 ClientBuilder (javax.ws.rs.client.ClientBuilder)1 Builder (javax.ws.rs.client.Invocation.Builder)1