Java源码示例:org.springframework.retry.RetryOperations
示例1
@Bean
public RetryOperations retryOperations() {
RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(3, Collections.<Class<? extends Throwable>, Boolean>singletonMap(RedisConnectionFailureException.class, true)));
ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
backOffPolicy.setInitialInterval(1000L);
backOffPolicy.setMaxInterval(1000L);
backOffPolicy.setMultiplier(2);
retryTemplate.setBackOffPolicy(backOffPolicy);
return retryTemplate;
}
示例2
@Bean
public RetryOperations retryOperations() {
RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(3,
Collections.<Class<? extends Throwable>, Boolean>singletonMap(RedisConnectionFailureException.class, true)));
ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
backOffPolicy.setInitialInterval(1000L);
backOffPolicy.setMaxInterval(1000L);
backOffPolicy.setMultiplier(2);
retryTemplate.setBackOffPolicy(backOffPolicy);
return retryTemplate;
}
示例3
/**
* リトライのポリシー(間隔、回数)を設定し、
* {@link RetryOperations}のインスタンスをDIコンテナに登録します。
* @return {@link RetryTemplate}のインスタンス
*/
@Bean
public RetryOperations retryOperations() {
RetryTemplate template = new RetryTemplate();
template.setRetryPolicy(retryPolicy());
template.setBackOffPolicy(backOffPolicy());
return template;
}
示例4
@Test
public void デフォルト値が設定されて指定のインスタンスがDIコンテナに登録される() {
assertThat(config.errorExchangeKey, is("error.exchange"));
assertThat(config.exceptionBindingKey, is("#.exception.#"));
assertThat(config.exceptionExchangeKey, is("exception.exchange"));
assertThat(config.recoverableExceptionQueue, is("recoverable.exception.messages.queue"));
assertThat(config.unrecoverableExceptionQueue, is("unrecoverable.exception.messages.queue"));
assertThat(config.unrecoverableExceptionBindingKey, is("#.unrecoverable.exception.#"));
assertThat(config.recoverableExceptionBindingKey, is("#.recoverable.exception.#"));
assertThat(config.unknownExceptionRoutingKey, is("unknown.unrecoverable.exception.key"));
TopicExchange error = context.getBean("errorExchange", TopicExchange.class);
assertThat(error, is(notNullValue()));
assertThat(error.getType(), is("topic"));
assertThat(error.getName(), is("error.exchange"));
TopicExchange exception = context.getBean("exceptionExchange", TopicExchange.class);
assertThat(exception, is(notNullValue()));
assertThat(exception.getType(), is("topic"));
assertThat(exception.getName(), is("exception.exchange"));
Queue recover = context.getBean("recoverableExceptionQueue", Queue.class);
assertThat(recover, is(notNullValue()));
assertThat(recover.getName(), is("recoverable.exception.messages.queue"));
Queue unrecover = context.getBean("unrecoverableExceptionQueue", Queue.class);
assertThat(unrecover, is(notNullValue()));
assertThat(unrecover.getName(), is("unrecoverable.exception.messages.queue"));
Binding exceptionBinding = context.getBean("exceptionBinding", Binding.class);
assertThat(exceptionBinding, is(notNullValue()));
assertThat(exceptionBinding.getExchange(), is("error.exchange"));
assertThat(exceptionBinding.getDestination(), is("exception.exchange"));
assertThat(exceptionBinding.getRoutingKey(), is("#.exception.#"));
Binding recoverBinding = context.getBean("recoverableExceptionQueueBinding", Binding.class);
assertThat(recoverBinding, is(notNullValue()));
assertThat(recoverBinding.getExchange(), is("exception.exchange"));
assertThat(recoverBinding.getDestination(), is("recoverable.exception.messages.queue"));
assertThat(recoverBinding.getRoutingKey(), is("#.recoverable.exception.#"));
Binding unrecoverBinding = context.getBean("unrecoverableExceptionQueueBinding", Binding.class);
assertThat(unrecoverBinding, is(notNullValue()));
assertThat(unrecoverBinding.getExchange(), is("exception.exchange"));
assertThat(unrecoverBinding.getDestination(), is("unrecoverable.exception.messages.queue"));
assertThat(unrecoverBinding.getRoutingKey(), is("#.unrecoverable.exception.#"));
assertThat(config.retryCount, is(10));
assertThat(config.backOffPeriod, is(1000L));
assertThat(context.getBean(RetryOperations.class), is(notNullValue()));
assertThat(context.getBean(ExceptionMessageExchanger.class), is(notNullValue()));
assertThat(context.getBean(LoggingErrorHandler.class), is(notNullValue()));
LoggingErrorHandler handler = context.getBean(LoggingErrorHandler.class);
BinaryExceptionClassifier mapping = handler.getRetryableClassifier();
assertThat(mapping, is(notNullValue()));
assertThat(mapping.classify(new AmqpApplicationRecoverableException("")), is(true));
assertThat(mapping.classify(new AmqpApplicationUnrecoverableException("")), is(false));
assertThat(context.getBean(LoggingErrorHandler.class), is(notNullValue()));
assertThat(context.getBean(Advice[].class), is(notNullValue()));
assertThat(context.getBean(StatefulRetryOperationsInterceptor.class), is(notNullValue()));
}
示例5
@Test
public void デフォルト値が設定されて指定のインスタンスがDIコンテナに登録される() {
assertThat(config.errorExchangeKey, is("override.error"));
assertThat(config.exceptionBindingKey, is("override.exception.binding"));
assertThat(config.exceptionExchangeKey, is("override.exception"));
assertThat(config.recoverableExceptionQueue, is("override.recoverable.exception.message.queue"));
assertThat(config.unrecoverableExceptionQueue, is("override.unrecoverable.exception.message.queue"));
assertThat(config.unrecoverableExceptionBindingKey, is("override.unrecoverable.exception.binding"));
assertThat(config.recoverableExceptionBindingKey, is("override.recoverable.exception.binding"));
assertThat(config.unknownExceptionRoutingKey, is("override.exception.key"));
TopicExchange error = context.getBean("errorExchange", TopicExchange.class);
assertThat(error, is(notNullValue()));
assertThat(error.getType(), is("topic"));
assertThat(error.getName(), is("override.error"));
TopicExchange exception = context.getBean("exceptionExchange", TopicExchange.class);
assertThat(exception, is(notNullValue()));
assertThat(exception.getType(), is("topic"));
assertThat(exception.getName(), is("override.exception"));
Queue recover = context.getBean("recoverableExceptionQueue", Queue.class);
assertThat(recover, is(notNullValue()));
assertThat(recover.getName(), is("override.recoverable.exception.message.queue"));
Queue unrecover = context.getBean("unrecoverableExceptionQueue", Queue.class);
assertThat(unrecover, is(notNullValue()));
assertThat(unrecover.getName(), is("override.unrecoverable.exception.message.queue"));
Binding exceptionBinding = context.getBean("exceptionBinding", Binding.class);
assertThat(exceptionBinding, is(notNullValue()));
assertThat(exceptionBinding.getExchange(), is("override.error"));
assertThat(exceptionBinding.getDestination(), is("override.exception"));
assertThat(exceptionBinding.getRoutingKey(), is("override.exception.binding"));
Binding recoverBinding = context.getBean("recoverableExceptionQueueBinding", Binding.class);
assertThat(recoverBinding, is(notNullValue()));
assertThat(recoverBinding.getExchange(), is("override.exception"));
assertThat(recoverBinding.getDestination(), is("override.recoverable.exception.message.queue"));
assertThat(recoverBinding.getRoutingKey(), is("override.recoverable.exception.binding"));
Binding unrecoverBinding = context.getBean("unrecoverableExceptionQueueBinding", Binding.class);
assertThat(unrecoverBinding, is(notNullValue()));
assertThat(unrecoverBinding.getExchange(), is("override.exception"));
assertThat(unrecoverBinding.getDestination(), is("override.unrecoverable.exception.message.queue"));
assertThat(unrecoverBinding.getRoutingKey(), is("override.unrecoverable.exception.binding"));
assertThat(config.retryCount, is(999));
assertThat(config.backOffPeriod, is(99999L));
assertThat(context.getBean(RetryOperations.class), is(notNullValue()));
assertThat(context.getBean(ExceptionMessageExchanger.class), is(notNullValue()));
assertThat(context.getBean(LoggingErrorHandler.class), is(notNullValue()));
LoggingErrorHandler handler = context.getBean(LoggingErrorHandler.class);
BinaryExceptionClassifier mapping = handler.getRetryableClassifier();
assertThat(mapping, is(notNullValue()));
assertThat(mapping.classify(new AmqpApplicationRecoverableException("")), is(true));
assertThat(mapping.classify(new AmqpApplicationUnrecoverableException("")), is(false));
assertThat(context.getBean(LoggingErrorHandler.class), is(notNullValue()));
assertThat(context.getBean(Advice[].class), is(notNullValue()));
assertThat(context.getBean(StatefulRetryOperationsInterceptor.class), is(notNullValue()));
}
示例6
/**
* Mutator for metadata retry operations.
* @param metadataRetryOperations the retry configuration
*/
public void setMetadataRetryOperations(RetryOperations metadataRetryOperations) {
this.metadataRetryOperations = metadataRetryOperations;
}