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(); + } + } +}