Skip to content

Commit 15e89c3

Browse files
Rework infra logic onto IntegrationProperties (#3881)
* Rework infra logic onto IntegrationProperties The `Properties` bean has been deprecated since `5.5` * Register a default `integrationGlobalProperties` as a `IntegrationProperties` * Fix respective usages from the `Properties` * * Remove unused constant from the `IntegrationContextUtils` * * Fix language in the `whats-new.adoc` Co-authored-by: Gary Russell <[email protected]> Co-authored-by: Gary Russell <[email protected]>
1 parent 8d241c6 commit 15e89c3

File tree

14 files changed

+107
-158
lines changed

14 files changed

+107
-158
lines changed

spring-integration-amqp/src/main/java/org/springframework/integration/amqp/channel/AbstractSubscribableAmqpChannel.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,7 +32,6 @@
3232
import org.springframework.integration.MessageDispatchingException;
3333
import org.springframework.integration.amqp.support.AmqpHeaderMapper;
3434
import org.springframework.integration.amqp.support.DefaultAmqpHeaderMapper;
35-
import org.springframework.integration.context.IntegrationProperties;
3635
import org.springframework.integration.dispatcher.AbstractDispatcher;
3736
import org.springframework.integration.dispatcher.MessageDispatcher;
3837
import org.springframework.integration.support.AbstractIntegrationMessageBuilder;
@@ -166,10 +165,10 @@ public void onInit() {
166165
super.onInit();
167166
this.dispatcher = this.createDispatcher();
168167
if (this.maxSubscribers == null) {
169-
this.maxSubscribers = this.getIntegrationProperty(this.isPubSub ?
170-
IntegrationProperties.CHANNELS_MAX_BROADCAST_SUBSCRIBERS :
171-
IntegrationProperties.CHANNELS_MAX_UNICAST_SUBSCRIBERS,
172-
Integer.class);
168+
this.maxSubscribers =
169+
this.isPubSub
170+
? getIntegrationProperties().getChannelsMaxBroadcastSubscribers()
171+
: getIntegrationProperties().getChannelsMaxUnicastSubscribers();
173172
}
174173
setMaxSubscribers(this.maxSubscribers);
175174
String queue = obtainQueueName(this.channelName);

spring-integration-core/src/main/java/org/springframework/integration/channel/DirectChannel.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.integration.channel;
1818

19-
import org.springframework.integration.context.IntegrationProperties;
2019
import org.springframework.integration.dispatcher.LoadBalancingStrategy;
2120
import org.springframework.integration.dispatcher.RoundRobinLoadBalancingStrategy;
2221
import org.springframework.integration.dispatcher.UnicastingDispatcher;
@@ -86,10 +85,7 @@ protected UnicastingDispatcher getDispatcher() {
8685
protected void onInit() {
8786
super.onInit();
8887
if (this.maxSubscribers == null) {
89-
Integer max = getIntegrationProperty(IntegrationProperties.CHANNELS_MAX_UNICAST_SUBSCRIBERS, Integer.class);
90-
if (max != null) {
91-
setMaxSubscribers(max);
92-
}
88+
setMaxSubscribers(getIntegrationProperties().getChannelsMaxUnicastSubscribers());
9389
}
9490
}
9591

spring-integration-core/src/main/java/org/springframework/integration/channel/ExecutorChannel.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,11 +18,11 @@
1818

1919
import java.util.concurrent.Executor;
2020

21-
import org.springframework.integration.context.IntegrationProperties;
2221
import org.springframework.integration.dispatcher.LoadBalancingStrategy;
2322
import org.springframework.integration.dispatcher.RoundRobinLoadBalancingStrategy;
2423
import org.springframework.integration.dispatcher.UnicastingDispatcher;
2524
import org.springframework.integration.util.ErrorHandlingTaskExecutor;
25+
import org.springframework.lang.Nullable;
2626
import org.springframework.util.Assert;
2727
import org.springframework.util.ErrorHandler;
2828

@@ -42,13 +42,14 @@
4242
* @author Mark Fisher
4343
* @author Gary Russell
4444
* @author Artem Bilan
45+
*
4546
* @since 1.0.3
4647
*/
4748
public class ExecutorChannel extends AbstractExecutorChannel {
4849

49-
private volatile boolean failover = true;
50+
private final LoadBalancingStrategy loadBalancingStrategy;
5051

51-
private volatile LoadBalancingStrategy loadBalancingStrategy;
52+
private boolean failover = true;
5253

5354
/**
5455
* Create an ExecutorChannel that delegates to the provided
@@ -69,13 +70,13 @@ public ExecutorChannel(Executor executor) {
6970
* @param executor The executor.
7071
* @param loadBalancingStrategy The load balancing strategy implementation.
7172
*/
72-
public ExecutorChannel(Executor executor, LoadBalancingStrategy loadBalancingStrategy) {
73+
public ExecutorChannel(Executor executor, @Nullable LoadBalancingStrategy loadBalancingStrategy) {
7374
super(executor);
7475
Assert.notNull(executor, "executor must not be null");
7576
UnicastingDispatcher unicastingDispatcher = new UnicastingDispatcher(executor);
76-
if (loadBalancingStrategy != null) {
77-
this.loadBalancingStrategy = loadBalancingStrategy;
78-
unicastingDispatcher.setLoadBalancingStrategy(loadBalancingStrategy);
77+
this.loadBalancingStrategy = loadBalancingStrategy;
78+
if (this.loadBalancingStrategy != null) {
79+
unicastingDispatcher.setLoadBalancingStrategy(this.loadBalancingStrategy);
7980
}
8081
this.dispatcher = unicastingDispatcher;
8182
}
@@ -108,8 +109,7 @@ public final void onInit() {
108109
UnicastingDispatcher unicastingDispatcher = new UnicastingDispatcher(this.executor);
109110
unicastingDispatcher.setFailover(this.failover);
110111
if (this.maxSubscribers == null) {
111-
this.maxSubscribers =
112-
getIntegrationProperty(IntegrationProperties.CHANNELS_MAX_UNICAST_SUBSCRIBERS, Integer.class);
112+
this.maxSubscribers = getIntegrationProperties().getChannelsMaxUnicastSubscribers();
113113
}
114114
unicastingDispatcher.setMaxSubscribers(this.maxSubscribers);
115115
if (this.loadBalancingStrategy != null) {

spring-integration-core/src/main/java/org/springframework/integration/channel/PublishSubscribeChannel.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@
2020

2121
import org.springframework.beans.factory.BeanFactory;
2222
import org.springframework.integration.IntegrationPatternType;
23-
import org.springframework.integration.context.IntegrationProperties;
2423
import org.springframework.integration.dispatcher.BroadcastingDispatcher;
2524
import org.springframework.integration.util.ErrorHandlingTaskExecutor;
2625
import org.springframework.lang.Nullable;
@@ -38,10 +37,10 @@
3837
*/
3938
public class PublishSubscribeChannel extends AbstractExecutorChannel implements BroadcastCapableChannel {
4039

41-
private ErrorHandler errorHandler;
42-
4340
private final boolean requireSubscribers;
4441

42+
private ErrorHandler errorHandler;
43+
4544
private boolean ignoreFailures;
4645

4746
private boolean applySequence;
@@ -122,7 +121,7 @@ public void setErrorHandler(ErrorHandler errorHandler) {
122121

123122
/**
124123
* Specify whether failures for one or more of the handlers should be
125-
* ignored. By default this is false meaning that an Exception
124+
* ignored. By default, this is false meaning that an Exception
126125
* will be thrown whenever a handler fails. To override this and suppress
127126
* Exceptions, set the value to true.
128127
* @param ignoreFailures true if failures should be ignored.
@@ -188,9 +187,7 @@ else if (this.errorHandler != null) {
188187
}
189188

190189
if (this.maxSubscribers == null) {
191-
Integer maxSubscribers =
192-
getIntegrationProperty(IntegrationProperties.CHANNELS_MAX_BROADCAST_SUBSCRIBERS, Integer.class);
193-
setMaxSubscribers(maxSubscribers);
190+
setMaxSubscribers(getIntegrationProperties().getChannelsMaxBroadcastSubscribers());
194191
}
195192
dispatcherToUse.setBeanFactory(beanFactory);
196193

spring-integration-core/src/main/java/org/springframework/integration/config/DefaultConfiguringBeanFactoryPostProcessor.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
138138
@Override
139139
public void afterSingletonsInstantiated() {
140140
if (LOGGER.isDebugEnabled()) {
141-
Properties integrationProperties = IntegrationContextUtils.getIntegrationProperties(this.beanFactory);
141+
Properties integrationProperties =
142+
IntegrationContextUtils.getIntegrationProperties(this.beanFactory)
143+
.toProperties();
142144

143145
StringWriter writer = new StringWriter();
144146
integrationProperties.list(new PrintWriter(writer));
@@ -237,11 +239,8 @@ private void registerErrorChannel() {
237239
}
238240

239241
private PublishSubscribeChannel createErrorChannel() {
240-
Properties integrationProperties = IntegrationContextUtils.getIntegrationProperties(this.beanFactory);
241-
String requireSubscribers =
242-
integrationProperties.getProperty(IntegrationProperties.ERROR_CHANNEL_REQUIRE_SUBSCRIBERS);
243-
244-
return new PublishSubscribeChannel(Boolean.parseBoolean(requireSubscribers));
242+
IntegrationProperties integrationProperties = IntegrationContextUtils.getIntegrationProperties(this.beanFactory);
243+
return new PublishSubscribeChannel(integrationProperties.isErrorChannelRequireSubscribers());
245244
}
246245

247246
/**
@@ -318,16 +317,19 @@ private void registerTaskScheduler() {
318317
*/
319318
private void registerIntegrationProperties() {
320319
if (!this.beanFactory.containsBean(IntegrationContextUtils.INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME)) {
321-
// TODO Revise in favor of 'IntegrationProperties' instance in the next 6.0 version
322-
BeanDefinitionBuilder integrationPropertiesBuilder =
323-
BeanDefinitionBuilder.genericBeanDefinition(PropertiesFactoryBean.class,
324-
PropertiesFactoryBean::new)
325-
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE)
326-
.addPropertyValue("properties", IntegrationProperties.defaults())
327-
.addPropertyValue("locations", "classpath*:META-INF/spring.integration.properties");
320+
BeanDefinition userProperties =
321+
BeanDefinitionBuilder.genericBeanDefinition(PropertiesFactoryBean.class, PropertiesFactoryBean::new)
322+
.addPropertyValue("locations", "classpath*:META-INF/spring.integration.properties")
323+
.getBeanDefinition();
324+
325+
BeanDefinition integrationProperties =
326+
BeanDefinitionBuilder.genericBeanDefinition(IntegrationProperties.class)
327+
.setFactoryMethod("parse")
328+
.addConstructorArgValue(userProperties)
329+
.getBeanDefinition();
328330

329331
this.registry.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME,
330-
integrationPropertiesBuilder.getBeanDefinition());
332+
integrationProperties);
331333
}
332334
}
333335

spring-integration-core/src/main/java/org/springframework/integration/context/IntegrationContextUtils.java

Lines changed: 7 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,13 @@
1616

1717
package org.springframework.integration.context;
1818

19-
import java.util.Properties;
20-
2119
import org.apache.commons.logging.Log;
2220
import org.apache.commons.logging.LogFactory;
23-
import org.jetbrains.annotations.Nullable;
2421

2522
import org.springframework.beans.factory.BeanFactory;
26-
import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
2723
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2824
import org.springframework.beans.factory.config.BeanDefinition;
2925
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
30-
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
31-
import org.springframework.beans.factory.support.RootBeanDefinition;
3226
import org.springframework.expression.spel.support.SimpleEvaluationContext;
3327
import org.springframework.expression.spel.support.StandardEvaluationContext;
3428
import org.springframework.integration.metadata.MetadataStore;
@@ -70,8 +64,6 @@ public abstract class IntegrationContextUtils {
7064

7165
public static final String INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME = "integrationGlobalProperties";
7266

73-
public static final String MERGED_INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME = "mergedIntegrationGlobalProperties";
74-
7567
public static final String CHANNEL_INITIALIZER_BEAN_NAME = "channelInitializer";
7668

7769
public static final String AUTO_CREATE_CHANNEL_CANDIDATES_BEAN_NAME = "$autoCreateChannelCandidates";
@@ -179,8 +171,6 @@ private static <T> T getBeanOfType(BeanFactory beanFactory, String beanName, Cla
179171
return beanFactory.getBean(beanName, type);
180172
}
181173

182-
// TODO Revise in favor of 'IntegrationProperties' instance in the next 6.0 version
183-
184174
/**
185175
* @param beanFactory The bean factory.
186176
* @return the global {@link IntegrationContextUtils#INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME}
@@ -191,59 +181,16 @@ private static <T> T getBeanOfType(BeanFactory beanFactory, String beanName, Cla
191181
* {@link IntegrationContextUtils#INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME} bean in the
192182
* provided {@code #beanFactory} or provided {@code #beanFactory} is null.
193183
*/
194-
public static Properties getIntegrationProperties(BeanFactory beanFactory) {
195-
Properties properties;
184+
public static IntegrationProperties getIntegrationProperties(BeanFactory beanFactory) {
185+
IntegrationProperties integrationProperties = null;
196186
if (beanFactory != null) {
197-
properties = getBeanOfType(beanFactory, MERGED_INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME, Properties.class);
198-
if (properties == null) {
199-
Properties propertiesToRegister = new Properties();
200-
propertiesToRegister.putAll(IntegrationProperties.defaults());
201-
Properties userProperties = obtainUserProperties(beanFactory);
202-
if (userProperties != null) {
203-
propertiesToRegister.putAll(userProperties);
204-
}
205-
206-
if (beanFactory instanceof BeanDefinitionRegistry registry) {
207-
RootBeanDefinition beanDefinition = new RootBeanDefinition(Properties.class);
208-
beanDefinition.setInstanceSupplier(() -> propertiesToRegister);
209-
210-
registry.registerBeanDefinition(MERGED_INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME, beanDefinition);
211-
}
212-
213-
properties = propertiesToRegister;
214-
}
215-
}
216-
else {
217-
properties = new Properties();
218-
properties.putAll(IntegrationProperties.defaults());
219-
}
220-
return properties;
221-
}
222-
223-
@Nullable
224-
private static Properties obtainUserProperties(BeanFactory beanFactory) {
225-
Object userProperties = getBeanOfType(beanFactory, INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME, Object.class);
226-
if (userProperties instanceof Properties) {
227-
if (BeanDefinition.ROLE_INFRASTRUCTURE !=
228-
((BeanDefinitionRegistry) beanFactory)
229-
.getBeanDefinition(INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME)
230-
.getRole()) {
231-
232-
LOGGER.warn("The 'integrationGlobalProperties' bean must be declared as an instance of " +
233-
"'org.springframework.integration.context.IntegrationProperties'. " +
234-
"A 'java.util.Properties' support as a bean is deprecated for " +
235-
"'integrationGlobalProperties' since version 5.5.");
236-
}
237-
return (Properties) userProperties;
238-
}
239-
else if (userProperties instanceof IntegrationProperties) {
240-
return ((IntegrationProperties) userProperties).toProperties();
187+
integrationProperties =
188+
getBeanOfType(beanFactory, INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME, IntegrationProperties.class);
241189
}
242-
else if (userProperties != null) {
243-
throw new BeanNotOfRequiredTypeException(INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME,
244-
IntegrationProperties.class, userProperties.getClass());
190+
if (integrationProperties == null) {
191+
integrationProperties = IntegrationProperties.DEFAULT_INSTANCE;
245192
}
246-
return null;
193+
return integrationProperties;
247194
}
248195

249196
/**

spring-integration-core/src/main/java/org/springframework/integration/context/IntegrationObjectSupport.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.integration.context;
1818

19-
import java.util.Properties;
2019
import java.util.UUID;
2120

2221
import org.springframework.aop.framework.AopProxyUtils;
@@ -86,7 +85,7 @@ public abstract class IntegrationObjectSupport implements BeanNameAware, NamedCo
8685

8786
private TaskScheduler taskScheduler;
8887

89-
private Properties integrationProperties = IntegrationProperties.defaults();
88+
private IntegrationProperties integrationProperties = new IntegrationProperties();
9089

9190
private ConversionService conversionService;
9291

@@ -290,12 +289,9 @@ protected ApplicationContext getApplicationContext() {
290289

291290
/**
292291
* @return The global integration properties.
293-
* @deprecated since version 5.5 in favor of {@link #getIntegrationProperty(String, Class)};
294-
* will be replaced with {@link IntegrationProperties} variant in the next major version.
295292
* @see IntegrationContextUtils#getIntegrationProperties(BeanFactory)
296293
*/
297-
@Deprecated
298-
protected Properties getIntegrationProperties() {
294+
protected IntegrationProperties getIntegrationProperties() {
299295
return this.integrationProperties;
300296
}
301297

@@ -315,9 +311,11 @@ public void setMessageBuilderFactory(MessageBuilderFactory messageBuilderFactory
315311
* @param tClass the class to convert a value of Integration property.
316312
* @param <T> The expected type of the property.
317313
* @return the value of the Integration property converted to the provide type.
314+
* @deprecated in favor of {@link #getIntegrationProperties()}
318315
*/
316+
@Deprecated(since = "6.0")
319317
protected <T> T getIntegrationProperty(String key, Class<T> tClass) {
320-
return this.defaultConversionService.convert(this.integrationProperties.getProperty(key), tClass);
318+
return this.defaultConversionService.convert(this.integrationProperties.toProperties().getProperty(key), tClass);
321319
}
322320

323321
@Override

0 commit comments

Comments
 (0)