avoid hard-coded dependency on backport-concurrent (SPR-6161)
This commit is contained in:
parent
b4ffdcdf18
commit
e2af25b891
|
|
@ -16,11 +16,10 @@
|
||||||
|
|
||||||
package org.springframework.scheduling.config;
|
package org.springframework.scheduling.config;
|
||||||
|
|
||||||
import java.util.concurrent.ThreadPoolExecutor;
|
|
||||||
|
|
||||||
import org.w3c.dom.Element;
|
import org.w3c.dom.Element;
|
||||||
|
|
||||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||||
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||||
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
|
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
|
||||||
import org.springframework.beans.factory.xml.ParserContext;
|
import org.springframework.beans.factory.xml.ParserContext;
|
||||||
import org.springframework.core.JdkVersion;
|
import org.springframework.core.JdkVersion;
|
||||||
|
|
@ -30,16 +29,19 @@ import org.springframework.util.StringUtils;
|
||||||
* Parser for the 'executor' element of the 'task' namespace.
|
* Parser for the 'executor' element of the 'task' namespace.
|
||||||
*
|
*
|
||||||
* @author Mark Fisher
|
* @author Mark Fisher
|
||||||
|
* @author Juergen Hoeller
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public class ExecutorBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
|
public class ExecutorBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getBeanClassName(Element element) {
|
protected String getBeanClassName(Element element) {
|
||||||
if (this.shouldUseBackport(element)) {
|
if (shouldUseBackport(element)) {
|
||||||
return "org.springframework.scheduling.backportconcurrent.ThreadPoolTaskExecutor";
|
return "org.springframework.scheduling.backportconcurrent.ThreadPoolTaskExecutor";
|
||||||
}
|
}
|
||||||
return "org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor";
|
else {
|
||||||
|
return "org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -52,7 +54,7 @@ public class ExecutorBeanDefinitionParser extends AbstractSingleBeanDefinitionPa
|
||||||
if (StringUtils.hasText(queueCapacity)) {
|
if (StringUtils.hasText(queueCapacity)) {
|
||||||
builder.addPropertyValue("queueCapacity", queueCapacity);
|
builder.addPropertyValue("queueCapacity", queueCapacity);
|
||||||
}
|
}
|
||||||
this.configureRejectionPolicy(element, builder);
|
configureRejectionPolicy(element, builder);
|
||||||
String poolSize = element.getAttribute("pool-size");
|
String poolSize = element.getAttribute("pool-size");
|
||||||
if (!StringUtils.hasText(poolSize)) {
|
if (!StringUtils.hasText(poolSize)) {
|
||||||
return;
|
return;
|
||||||
|
|
@ -104,43 +106,33 @@ public class ExecutorBeanDefinitionParser extends AbstractSingleBeanDefinitionPa
|
||||||
if (!StringUtils.hasText(rejectionPolicy)) {
|
if (!StringUtils.hasText(rejectionPolicy)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Object handler = null;
|
String prefix = "java.util.concurrent.ThreadPoolExecutor.";
|
||||||
boolean createBackportHandler = this.shouldUseBackport(element);
|
if (builder.getRawBeanDefinition().getBeanClassName().contains("backport")) {
|
||||||
|
prefix = "edu.emory.mathcs.backport." + prefix;
|
||||||
|
}
|
||||||
|
String policyClassName;
|
||||||
if (rejectionPolicy.equals("ABORT")) {
|
if (rejectionPolicy.equals("ABORT")) {
|
||||||
if (createBackportHandler) {
|
policyClassName = prefix + "AbortPolicy";
|
||||||
handler = new edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor.AbortPolicy();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
handler = new ThreadPoolExecutor.AbortPolicy();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (rejectionPolicy.equals("CALLER_RUNS")) {
|
else if (rejectionPolicy.equals("CALLER_RUNS")) {
|
||||||
if (createBackportHandler) {
|
policyClassName = prefix + "CallerRunsPolicy";
|
||||||
handler = new edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
handler = new ThreadPoolExecutor.CallerRunsPolicy();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (rejectionPolicy.equals("DISCARD")) {
|
else if (rejectionPolicy.equals("DISCARD")) {
|
||||||
if (createBackportHandler) {
|
policyClassName = prefix + "DiscardPolicy";
|
||||||
handler = new edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor.DiscardPolicy();
|
|
||||||
}
|
|
||||||
handler = new ThreadPoolExecutor.DiscardPolicy();
|
|
||||||
}
|
}
|
||||||
if (rejectionPolicy.equals("DISCARD_OLDEST")) {
|
else if (rejectionPolicy.equals("DISCARD_OLDEST")) {
|
||||||
if (createBackportHandler) {
|
policyClassName = prefix + "DiscardOldestPolicy";
|
||||||
handler = new edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy();
|
|
||||||
}
|
|
||||||
handler = new ThreadPoolExecutor.DiscardOldestPolicy();
|
|
||||||
}
|
}
|
||||||
builder.addPropertyValue("rejectedExecutionHandler", handler);
|
else {
|
||||||
|
policyClassName = rejectionPolicy;
|
||||||
|
}
|
||||||
|
builder.addPropertyValue("rejectedExecutionHandler", new RootBeanDefinition(policyClassName));
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean shouldUseBackport(Element element) {
|
private boolean shouldUseBackport(Element element) {
|
||||||
String poolSize = element.getAttribute("pool-size");
|
String poolSize = element.getAttribute("pool-size");
|
||||||
return StringUtils.hasText(poolSize) && poolSize.startsWith("0")
|
return (StringUtils.hasText(poolSize) && poolSize.startsWith("0") &&
|
||||||
&& JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_16;
|
JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_16);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue