org.springframework.retry.RetryPolicy Java Examples

The following examples show how to use org.springframework.retry.RetryPolicy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultRetryTemplateConverterTest.java    From distributed-lock with MIT License 6 votes vote down vote up
private void assertRetryTemplateConstruction(final RetryTemplate retryTemplate, final long timeout, final long backOff) {
  final var wrapper = PropertyAccessorFactory.forDirectFieldAccess(retryTemplate);

  assertThat(wrapper.getPropertyValue("retryPolicy")).isInstanceOf(CompositeRetryPolicy.class);
  assertThat((RetryPolicy[]) wrapper.getPropertyValue("retryPolicy.policies"))
    .hasSize(2)
    .anyMatch(policy1 -> {
      if (policy1 instanceof TimeoutRetryPolicy) {
        final var timeoutRetryPolicy = (TimeoutRetryPolicy) policy1;
        assertThat(timeoutRetryPolicy.getTimeout()).isEqualTo(timeout);
        return true;
      }
      return false;
    })
    .anyMatch(policy2 -> {
      if (policy2 instanceof SimpleRetryPolicy) {
        final var simpleRetryPolicy = (SimpleRetryPolicy) policy2;
        assertThat(simpleRetryPolicy.getMaxAttempts()).isEqualTo(Integer.MAX_VALUE);
        return true;
      }
      return false;
    });

  assertThat(wrapper.getPropertyValue("backOffPolicy")).isInstanceOf(FixedBackOffPolicy.class);
  assertThat(((FixedBackOffPolicy) wrapper.getPropertyValue("backOffPolicy")).getBackOffPeriod()).isEqualTo(backOff);
}
 
Example #2
Source File: InteractiveQueryService.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieve and return a queryable store by name created in the application.
 * @param storeName name of the queryable store
 * @param storeType type of the queryable store
 * @param <T> generic queryable store
 * @return queryable store.
 */
public <T> T getQueryableStore(String storeName, QueryableStoreType<T> storeType) {

	RetryTemplate retryTemplate = new RetryTemplate();

	KafkaStreamsBinderConfigurationProperties.StateStoreRetry stateStoreRetry = this.binderConfigurationProperties.getStateStoreRetry();
	RetryPolicy retryPolicy = new SimpleRetryPolicy(stateStoreRetry.getMaxAttempts());
	FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
	backOffPolicy.setBackOffPeriod(stateStoreRetry.getBackoffPeriod());

	retryTemplate.setBackOffPolicy(backOffPolicy);
	retryTemplate.setRetryPolicy(retryPolicy);

	return retryTemplate.execute(context -> {
		T store = null;

		final Set<KafkaStreams> kafkaStreams = InteractiveQueryService.this.kafkaStreamsRegistry.getKafkaStreams();
		final Iterator<KafkaStreams> iterator = kafkaStreams.iterator();
		Throwable throwable = null;
		while (iterator.hasNext()) {
			try {
				store = iterator.next().store(storeName, storeType);
			}
			catch (InvalidStateStoreException e) {
				// pass through..
				throwable = e;
			}
		}
		if (store != null) {
			return store;
		}
		throw new IllegalStateException("Error when retrieving state store: j " + storeName, throwable);
	});
}
 
Example #3
Source File: DefaultRetryTemplateConverter.java    From distributed-lock with MIT License 5 votes vote down vote up
private CompositeRetryPolicy resolveLockRetryPolicy(final Locked locked) {
  final var compositeRetryPolicy = new CompositeRetryPolicy();

  final var timeoutRetryPolicy = resolveTimeoutRetryPolicy(locked);
  final var exceptionTypeRetryPolicy = resolveExceptionTypeRetryPolicy();

  compositeRetryPolicy.setPolicies(new RetryPolicy[]{timeoutRetryPolicy, exceptionTypeRetryPolicy});
  return compositeRetryPolicy;
}
 
Example #4
Source File: ConnectionRetryConfig.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean
public RetryPolicy retryPolicy() {
  TimeoutRetryPolicy timeoutRetryPolicy = new TimeoutRetryPolicy();
  timeoutRetryPolicy.setTimeout(MAX_CONNECTION_TIMEOUT.toMillis());
  ExceptionClassifierRetryPolicy exceptionClassifierRetryPolicy =
      new ExceptionClassifierRetryPolicy();
  exceptionClassifierRetryPolicy.setPolicyMap(
      ImmutableMap.of(MolgenisDataException.class, timeoutRetryPolicy));
  return exceptionClassifierRetryPolicy;
}
 
Example #5
Source File: SpringRetryConfigBuilder.java    From spring-cloud-circuitbreaker with Apache License 2.0 4 votes vote down vote up
RetryPolicy getRetryPolicy() {
	return retryPolicy;
}
 
Example #6
Source File: SpringRetryConfigBuilder.java    From spring-cloud-circuitbreaker with Apache License 2.0 4 votes vote down vote up
void setRetryPolicy(RetryPolicy retryPolicy) {
	this.retryPolicy = retryPolicy;
}
 
Example #7
Source File: DefaultRetryTemplateConverter.java    From distributed-lock with MIT License 4 votes vote down vote up
private RetryPolicy resolveTimeoutRetryPolicy(final Locked locked) {
  final var timeoutRetryPolicy = new TimeoutRetryPolicy();
  timeoutRetryPolicy.setTimeout(intervalConverter.toMillis(locked.timeout()));
  return timeoutRetryPolicy;
}
 
Example #8
Source File: DefaultRetryTemplateConverter.java    From distributed-lock with MIT License 4 votes vote down vote up
private RetryPolicy resolveExceptionTypeRetryPolicy() {
  return new SimpleRetryPolicy(Integer.MAX_VALUE, Map.of(LockNotAvailableException.class, true));
}
 
Example #9
Source File: ExceptionQueueContextConfig.java    From sinavi-jfw with Apache License 2.0 2 votes vote down vote up
/**
 * リトライの対象となる例外とリトライ回数を指定し、
 * {@link RetryPolicy}のインスタンスを生成します。
 * @return {@link SimpleRetryPolicy}のインスタンス
 */
protected RetryPolicy retryPolicy() {
    return new SimpleRetryPolicy(retryCount, exceptionMapping(), true);
}
 
Example #10
Source File: AmqpContextConfig.java    From sinavi-jfw with Apache License 2.0 2 votes vote down vote up
/**
 * リトライの対象となる例外とリトライ回数を指定し、
 * {@link RetryPolicy}のインスタンスを生成します。
 * @return {@link SimpleRetryPolicy}のインスタンス
 */
protected RetryPolicy simpleRetryPolicy() {
    return new SimpleRetryPolicy(retryCount, Collections.<Class<? extends Throwable>, Boolean> singletonMap(Exception.class, true));
}