Skip to content

Commit e2af25b

Browse files
committed
avoid hard-coded dependency on backport-concurrent (SPR-6161)
1 parent b4ffdcd commit e2af25b

File tree

1 file changed

+26
-34
lines changed

1 file changed

+26
-34
lines changed

org.springframework.context/src/main/java/org/springframework/scheduling/config/ExecutorBeanDefinitionParser.java

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,32 @@
1616

1717
package org.springframework.scheduling.config;
1818

19-
import java.util.concurrent.ThreadPoolExecutor;
20-
2119
import org.w3c.dom.Element;
2220

2321
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
22+
import org.springframework.beans.factory.support.RootBeanDefinition;
2423
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
2524
import org.springframework.beans.factory.xml.ParserContext;
2625
import org.springframework.core.JdkVersion;
2726
import org.springframework.util.StringUtils;
2827

2928
/**
3029
* Parser for the 'executor' element of the 'task' namespace.
31-
*
30+
*
3231
* @author Mark Fisher
32+
* @author Juergen Hoeller
3333
* @since 3.0
3434
*/
3535
public class ExecutorBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
3636

3737
@Override
3838
protected String getBeanClassName(Element element) {
39-
if (this.shouldUseBackport(element)) {
39+
if (shouldUseBackport(element)) {
4040
return "org.springframework.scheduling.backportconcurrent.ThreadPoolTaskExecutor";
4141
}
42-
return "org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor";
42+
else {
43+
return "org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor";
44+
}
4345
}
4446

4547
@Override
@@ -52,7 +54,7 @@ protected void doParse(Element element, ParserContext parserContext, BeanDefinit
5254
if (StringUtils.hasText(queueCapacity)) {
5355
builder.addPropertyValue("queueCapacity", queueCapacity);
5456
}
55-
this.configureRejectionPolicy(element, builder);
57+
configureRejectionPolicy(element, builder);
5658
String poolSize = element.getAttribute("pool-size");
5759
if (!StringUtils.hasText(poolSize)) {
5860
return;
@@ -104,43 +106,33 @@ private void configureRejectionPolicy(Element element, BeanDefinitionBuilder bui
104106
if (!StringUtils.hasText(rejectionPolicy)) {
105107
return;
106108
}
107-
Object handler = null;
108-
boolean createBackportHandler = this.shouldUseBackport(element);
109+
String prefix = "java.util.concurrent.ThreadPoolExecutor.";
110+
if (builder.getRawBeanDefinition().getBeanClassName().contains("backport")) {
111+
prefix = "edu.emory.mathcs.backport." + prefix;
112+
}
113+
String policyClassName;
109114
if (rejectionPolicy.equals("ABORT")) {
110-
if (createBackportHandler) {
111-
handler = new edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor.AbortPolicy();
112-
}
113-
else {
114-
handler = new ThreadPoolExecutor.AbortPolicy();
115-
}
115+
policyClassName = prefix + "AbortPolicy";
116116
}
117-
if (rejectionPolicy.equals("CALLER_RUNS")) {
118-
if (createBackportHandler) {
119-
handler = new edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy();
120-
}
121-
else {
122-
handler = new ThreadPoolExecutor.CallerRunsPolicy();
123-
}
117+
else if (rejectionPolicy.equals("CALLER_RUNS")) {
118+
policyClassName = prefix + "CallerRunsPolicy";
124119
}
125-
if (rejectionPolicy.equals("DISCARD")) {
126-
if (createBackportHandler) {
127-
handler = new edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor.DiscardPolicy();
128-
}
129-
handler = new ThreadPoolExecutor.DiscardPolicy();
120+
else if (rejectionPolicy.equals("DISCARD")) {
121+
policyClassName = prefix + "DiscardPolicy";
130122
}
131-
if (rejectionPolicy.equals("DISCARD_OLDEST")) {
132-
if (createBackportHandler) {
133-
handler = new edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy();
134-
}
135-
handler = new ThreadPoolExecutor.DiscardOldestPolicy();
123+
else if (rejectionPolicy.equals("DISCARD_OLDEST")) {
124+
policyClassName = prefix + "DiscardOldestPolicy";
125+
}
126+
else {
127+
policyClassName = rejectionPolicy;
136128
}
137-
builder.addPropertyValue("rejectedExecutionHandler", handler);
129+
builder.addPropertyValue("rejectedExecutionHandler", new RootBeanDefinition(policyClassName));
138130
}
139131

140132
private boolean shouldUseBackport(Element element) {
141133
String poolSize = element.getAttribute("pool-size");
142-
return StringUtils.hasText(poolSize) && poolSize.startsWith("0")
143-
&& JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_16;
134+
return (StringUtils.hasText(poolSize) && poolSize.startsWith("0") &&
135+
JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_16);
144136
}
145137

146138
}

0 commit comments

Comments
 (0)