From f01cb29b44b6992326482c43c28c9bf0225daada Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Mon, 4 Feb 2019 06:51:19 +0100 Subject: [PATCH] GH-1511 Polishing and added tests - Polished RetryTemplate changes from the previous commit - Rebased with 2.2.x branch - Added tests - Updated author tags with original committer's name Resolves #1511 Resolves #1588 --- .../main/asciidoc/spring-cloud-stream.adoc | 9 ++ .../cloud/stream/binder/AbstractBinder.java | 22 ++-- .../stream/binder/ConsumerProperties.java | 19 ++- .../stream/config/RetryTemplateTests.java | 119 ++++++++++++++++++ 4 files changed, 150 insertions(+), 19 deletions(-) create mode 100644 spring-cloud-stream/src/test/java/org/springframework/cloud/stream/config/RetryTemplateTests.java diff --git a/docs/src/main/asciidoc/spring-cloud-stream.adoc b/docs/src/main/asciidoc/spring-cloud-stream.adoc index 1f4d223a1..d8f0d047f 100644 --- a/docs/src/main/asciidoc/spring-cloud-stream.adoc +++ b/docs/src/main/asciidoc/spring-cloud-stream.adoc @@ -1054,6 +1054,15 @@ public RetryTemplate myRetryTemplate() { ---- As you can see from the above example you don't need to annotate it with `@Bean` since `@StreamRetryTemplate` is a qualified `@Bean`. +If you need to be more precise with your `RetryTemplate`, you can specify the bean by name in your `ConsumerProperties` to associate +the specific retry bean per binding. + +[source] +---- +spring.cloud.stream.bindings..consumer.retry-template-name= +---- + + [[spring-cloud-stream-overview-binders]] == Binders diff --git a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/AbstractBinder.java b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/AbstractBinder.java index 2478c5a3d..0a15ecffb 100644 --- a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/AbstractBinder.java +++ b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/AbstractBinder.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2018 the original author or authors. + * Copyright 2013-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,8 @@ package org.springframework.cloud.stream.binder; +import java.util.Map; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -49,6 +51,7 @@ import org.springframework.util.StringUtils; * @author Soby Chacko * @author Vinicius Carvalho * @author Oleg Zhurakousky + * @author Nicolas Homble */ public abstract class AbstractBinder implements ApplicationContextAware, InitializingBean, Binder { @@ -65,9 +68,9 @@ public abstract class AbstractBinder consumerBindingRetryTemplates; /** * For binder implementations that support a prefix, apply the prefix to the name. @@ -179,12 +182,8 @@ public abstract class AbstractBinder consumerBindingRetryTemplates = (Map) f.get(binder); + assertTrue(consumerBindingRetryTemplates.size() == 1); + } + + @EnableBinding(Processor.class) + @Import(TestChannelBinderConfiguration.class) + @EnableAutoConfiguration + public static class SingleCustomRetryTemplateConfiguration { + + @StreamRetryTemplate + public RetryTemplate retryTemplate() { + return new RetryTemplate(); + } + + @Bean + public RetryTemplate otherRetryTemplate() { + return new RetryTemplate(); + } + } + + @SuppressWarnings("rawtypes") + @Test + public void testSpecificCustomRetryTemplate() throws Exception { + ApplicationContext context = new SpringApplicationBuilder(SpecificCustomRetryTemplateConfiguration.class) + .web(WebApplicationType.NONE) + .run("--spring.jmx.enabled=false", + "--spring.cloud.stream.bindings.input.consumer.retry-template-name=retryTemplateTwo"); + + RetryTemplate retryTemplateTwo = context.getBean("retryTemplateTwo", RetryTemplate.class); + BindingServiceProperties bindingServiceProperties = context.getBean(BindingServiceProperties.class); + ConsumerProperties consumerProperties = bindingServiceProperties.getConsumerProperties("input"); + AbstractBinder binder = context.getBean(AbstractBinder.class); + + Method m = AbstractBinder.class.getDeclaredMethod("buildRetryTemplate", ConsumerProperties.class); + m.setAccessible(true); + RetryTemplate retryTemplate = (RetryTemplate) m.invoke(binder, consumerProperties); + assertEquals(retryTemplateTwo, retryTemplate); + } + + @EnableBinding(Processor.class) + @Import(TestChannelBinderConfiguration.class) + @EnableAutoConfiguration + public static class SpecificCustomRetryTemplateConfiguration { + + @StreamRetryTemplate + public RetryTemplate retryTemplate() { + return new RetryTemplate(); + } + + @StreamRetryTemplate + public RetryTemplate retryTemplateTwo() { + return new RetryTemplate(); + } + + @Bean + public RetryTemplate otherRetryTemplate() { + return new RetryTemplate(); + } + } +}