Java Code Examples for org.springframework.retry.RetryPolicy

The following examples show how to use org.springframework.retry.RetryPolicy. These examples are extracted from open source projects. 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
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 Project: distributed-lock   Source File: DefaultRetryTemplateConverter.java    License: 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 3
/**
 * 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 4
@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
RetryPolicy getRetryPolicy() {
	return retryPolicy;
}
 
Example 6
void setRetryPolicy(RetryPolicy retryPolicy) {
	this.retryPolicy = retryPolicy;
}
 
Example 7
Source Project: distributed-lock   Source File: DefaultRetryTemplateConverter.java    License: 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 Project: distributed-lock   Source File: DefaultRetryTemplateConverter.java    License: 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 Project: sinavi-jfw   Source File: ExceptionQueueContextConfig.java    License: 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 Project: sinavi-jfw   Source File: AmqpContextConfig.java    License: 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));
}