16
16
17
17
package org .springframework .scheduling .config ;
18
18
19
- import java .util .concurrent .ThreadPoolExecutor ;
20
-
21
19
import org .w3c .dom .Element ;
22
20
23
21
import org .springframework .beans .factory .support .BeanDefinitionBuilder ;
22
+ import org .springframework .beans .factory .support .RootBeanDefinition ;
24
23
import org .springframework .beans .factory .xml .AbstractSingleBeanDefinitionParser ;
25
24
import org .springframework .beans .factory .xml .ParserContext ;
26
25
import org .springframework .core .JdkVersion ;
27
26
import org .springframework .util .StringUtils ;
28
27
29
28
/**
30
29
* Parser for the 'executor' element of the 'task' namespace.
31
- *
30
+ *
32
31
* @author Mark Fisher
32
+ * @author Juergen Hoeller
33
33
* @since 3.0
34
34
*/
35
35
public class ExecutorBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
36
36
37
37
@ Override
38
38
protected String getBeanClassName (Element element ) {
39
- if (this . shouldUseBackport (element )) {
39
+ if (shouldUseBackport (element )) {
40
40
return "org.springframework.scheduling.backportconcurrent.ThreadPoolTaskExecutor" ;
41
41
}
42
- return "org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" ;
42
+ else {
43
+ return "org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" ;
44
+ }
43
45
}
44
46
45
47
@ Override
@@ -52,7 +54,7 @@ protected void doParse(Element element, ParserContext parserContext, BeanDefinit
52
54
if (StringUtils .hasText (queueCapacity )) {
53
55
builder .addPropertyValue ("queueCapacity" , queueCapacity );
54
56
}
55
- this . configureRejectionPolicy (element , builder );
57
+ configureRejectionPolicy (element , builder );
56
58
String poolSize = element .getAttribute ("pool-size" );
57
59
if (!StringUtils .hasText (poolSize )) {
58
60
return ;
@@ -104,43 +106,33 @@ private void configureRejectionPolicy(Element element, BeanDefinitionBuilder bui
104
106
if (!StringUtils .hasText (rejectionPolicy )) {
105
107
return ;
106
108
}
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 ;
109
114
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" ;
116
116
}
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" ;
124
119
}
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" ;
130
122
}
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 ;
136
128
}
137
- builder .addPropertyValue ("rejectedExecutionHandler" , handler );
129
+ builder .addPropertyValue ("rejectedExecutionHandler" , new RootBeanDefinition ( policyClassName ) );
138
130
}
139
131
140
132
private boolean shouldUseBackport (Element element ) {
141
133
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 ) ;
144
136
}
145
137
146
138
}
0 commit comments