1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -67,17 +67,6 @@ public void proxyingOccurs() {
67
67
asyncBean .work ();
68
68
}
69
69
70
-
71
- @ Configuration
72
- @ EnableAsync
73
- static class AsyncConfig {
74
- @ Bean
75
- public AsyncBean asyncBean () {
76
- return new AsyncBean ();
77
- }
78
- }
79
-
80
-
81
70
@ Test
82
71
public void withAsyncBeanWithExecutorQualifiedByName () throws ExecutionException , InterruptedException {
83
72
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
@@ -95,8 +84,80 @@ public void withAsyncBeanWithExecutorQualifiedByName() throws ExecutionException
95
84
assertThat (workerThread3 .get ().getName (), startsWith ("otherExecutor-" ));
96
85
}
97
86
87
+ @ Test
88
+ public void asyncProcessorIsOrderedLowestPrecedenceByDefault () {
89
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
90
+ ctx .register (AsyncConfig .class );
91
+ ctx .refresh ();
92
+
93
+ AsyncAnnotationBeanPostProcessor bpp = ctx .getBean (AsyncAnnotationBeanPostProcessor .class );
94
+ assertThat (bpp .getOrder (), is (Ordered .LOWEST_PRECEDENCE ));
95
+ }
96
+
97
+ @ Test
98
+ public void orderAttributeIsPropagated () {
99
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
100
+ ctx .register (OrderedAsyncConfig .class );
101
+ ctx .refresh ();
102
+
103
+ AsyncAnnotationBeanPostProcessor bpp = ctx .getBean (AsyncAnnotationBeanPostProcessor .class );
104
+ assertThat (bpp .getOrder (), is (Ordered .HIGHEST_PRECEDENCE ));
105
+ }
106
+
107
+ @ Test
108
+ public void customAsyncAnnotationIsPropagated () {
109
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
110
+ ctx .register (CustomAsyncAnnotationConfig .class );
111
+ ctx .refresh ();
112
+
113
+ Object bean = ctx .getBean (CustomAsyncBean .class );
114
+ assertTrue (AopUtils .isAopProxy (bean ));
115
+ boolean isAsyncAdvised = false ;
116
+ for (Advisor advisor : ((Advised )bean ).getAdvisors ()) {
117
+ if (advisor instanceof AsyncAnnotationAdvisor ) {
118
+ isAsyncAdvised = true ;
119
+ break ;
120
+ }
121
+ }
122
+ assertTrue ("bean was not async advised as expected" , isAsyncAdvised );
123
+ }
124
+
125
+ /**
126
+ * Fails with classpath errors on trying to classload AnnotationAsyncExecutionAspect
127
+ */
128
+ @ Test (expected = BeanDefinitionStoreException .class )
129
+ public void aspectModeAspectJAttemptsToRegisterAsyncAspect () {
130
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
131
+ ctx .register (AspectJAsyncAnnotationConfig .class );
132
+ ctx .refresh ();
133
+ }
134
+
135
+ @ Test
136
+ public void customExecutorIsPropagated () throws InterruptedException {
137
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
138
+ ctx .register (CustomExecutorAsyncConfig .class );
139
+ ctx .refresh ();
140
+
141
+ AsyncBean asyncBean = ctx .getBean (AsyncBean .class );
142
+ asyncBean .work ();
143
+ Thread .sleep (500 );
144
+ assertThat (asyncBean .getThreadOfExecution ().getName (), startsWith ("Custom-" ));
145
+
146
+ TestableAsyncUncaughtExceptionHandler exceptionHandler = (TestableAsyncUncaughtExceptionHandler )
147
+ ctx .getBean ("exceptionHandler" );
148
+ assertFalse ("handler should not have been called yet" , exceptionHandler .isCalled ());
149
+
150
+ asyncBean .fail ();
151
+ Thread .sleep (500 );
152
+ Method m = ReflectionUtils .findMethod (AsyncBean .class , "fail" );
153
+ exceptionHandler .assertCalledWith (m , UnsupportedOperationException .class );
154
+
155
+ ctx .close ();
156
+ }
157
+
98
158
99
159
static class AsyncBeanWithExecutorQualifiedByName {
160
+
100
161
@ Async
101
162
public Future <Thread > work0 () {
102
163
return new AsyncResult <Thread >(Thread .currentThread ());
@@ -120,6 +181,7 @@ public Future<Thread> work3() {
120
181
121
182
122
183
static class AsyncBean {
184
+
123
185
private Thread threadOfExecution ;
124
186
125
187
@ Async
@@ -138,60 +200,10 @@ public Thread getThreadOfExecution() {
138
200
}
139
201
140
202
141
- @ Test
142
- public void asyncProcessorIsOrderedLowestPrecedenceByDefault () {
143
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
144
- ctx .register (AsyncConfig .class );
145
- ctx .refresh ();
146
-
147
- AsyncAnnotationBeanPostProcessor bpp = ctx .getBean (AsyncAnnotationBeanPostProcessor .class );
148
- assertThat (bpp .getOrder (), is (Ordered .LOWEST_PRECEDENCE ));
149
- }
150
-
151
-
152
- @ Test
153
- public void orderAttributeIsPropagated () {
154
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
155
- ctx .register (OrderedAsyncConfig .class );
156
- ctx .refresh ();
157
-
158
- AsyncAnnotationBeanPostProcessor bpp = ctx .getBean (AsyncAnnotationBeanPostProcessor .class );
159
- assertThat (bpp .getOrder (), is (Ordered .HIGHEST_PRECEDENCE ));
160
- }
161
-
162
-
163
- @ Configuration
164
- @ EnableAsync (order =Ordered .HIGHEST_PRECEDENCE )
165
- static class OrderedAsyncConfig {
166
- @ Bean
167
- public AsyncBean asyncBean () {
168
- return new AsyncBean ();
169
- }
170
- }
171
-
172
-
173
- @ Test
174
- public void customAsyncAnnotationIsPropagated () {
175
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
176
- ctx .register (CustomAsyncAnnotationConfig .class );
177
- ctx .refresh ();
178
-
179
- Object bean = ctx .getBean (CustomAsyncBean .class );
180
- assertTrue (AopUtils .isAopProxy (bean ));
181
- boolean isAsyncAdvised = false ;
182
- for (Advisor advisor : ((Advised )bean ).getAdvisors ()) {
183
- if (advisor instanceof AsyncAnnotationAdvisor ) {
184
- isAsyncAdvised = true ;
185
- break ;
186
- }
187
- }
188
- assertTrue ("bean was not async advised as expected" , isAsyncAdvised );
189
- }
190
-
191
-
192
203
@ Configuration
193
- @ EnableAsync (annotation = CustomAsync .class )
204
+ @ EnableAsync (annotation = CustomAsync .class )
194
205
static class CustomAsyncAnnotationConfig {
206
+
195
207
@ Bean
196
208
public CustomAsyncBean asyncBean () {
197
209
return new CustomAsyncBean ();
@@ -206,25 +218,26 @@ public CustomAsyncBean asyncBean() {
206
218
207
219
208
220
static class CustomAsyncBean {
221
+
209
222
@ CustomAsync
210
223
public void work () {
211
224
}
212
225
}
213
226
214
227
215
- /**
216
- * Fails with classpath errors on trying to classload AnnotationAsyncExecutionAspect
217
- */
218
- @ Test ( expected = BeanDefinitionStoreException . class )
219
- public void aspectModeAspectJAttemptsToRegisterAsyncAspect () {
220
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
221
- ctx . register ( AspectJAsyncAnnotationConfig . class );
222
- ctx . refresh ();
228
+ @ Configuration
229
+ @ EnableAsync ( order = Ordered . HIGHEST_PRECEDENCE )
230
+ static class OrderedAsyncConfig {
231
+
232
+ @ Bean
233
+ public AsyncBean asyncBean () {
234
+ return new AsyncBean ( );
235
+ }
223
236
}
224
237
225
238
226
239
@ Configuration
227
- @ EnableAsync (mode = AdviceMode .ASPECTJ )
240
+ @ EnableAsync (mode = AdviceMode .ASPECTJ )
228
241
static class AspectJAsyncAnnotationConfig {
229
242
@ Bean
230
243
public AsyncBean asyncBean () {
@@ -233,33 +246,21 @@ public AsyncBean asyncBean() {
233
246
}
234
247
235
248
236
- @ Test
237
- public void customExecutorIsPropagated () throws InterruptedException {
238
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
239
- ctx .register (CustomExecutorAsyncConfig .class );
240
- ctx .refresh ();
241
-
242
- AsyncBean asyncBean = ctx .getBean (AsyncBean .class );
243
- asyncBean .work ();
244
- Thread .sleep (500 );
245
- assertThat (asyncBean .getThreadOfExecution ().getName (), startsWith ("Custom-" ));
246
-
247
- TestableAsyncUncaughtExceptionHandler exceptionHandler = (TestableAsyncUncaughtExceptionHandler )
248
- ctx .getBean ("exceptionHandler" );
249
- assertFalse ("handler should not have been called yet" , exceptionHandler .isCalled ());
250
-
251
- asyncBean .fail ();
252
- Thread .sleep (500 );
253
- Method m = ReflectionUtils .findMethod (AsyncBean .class , "fail" );
254
- exceptionHandler .assertCalledWith (m , UnsupportedOperationException .class );
249
+ @ Configuration
250
+ @ EnableAsync
251
+ static class AsyncConfig {
255
252
256
- ctx .close ();
253
+ @ Bean
254
+ public AsyncBean asyncBean () {
255
+ return new AsyncBean ();
256
+ }
257
257
}
258
258
259
259
260
260
@ Configuration
261
261
@ EnableAsync
262
262
static class CustomExecutorAsyncConfig implements AsyncConfigurer {
263
+
263
264
@ Bean
264
265
public AsyncBean asyncBean () {
265
266
return new AsyncBean ();
@@ -288,22 +289,22 @@ public AsyncUncaughtExceptionHandler exceptionHandler() {
288
289
@ Configuration
289
290
@ EnableAsync
290
291
static class AsyncWithExecutorQualifiedByNameConfig {
292
+
291
293
@ Bean
292
294
public AsyncBeanWithExecutorQualifiedByName asyncBean () {
293
295
return new AsyncBeanWithExecutorQualifiedByName ();
294
296
}
295
297
296
298
@ Bean
297
299
public Executor e1 () {
298
- ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor ();
299
- return executor ;
300
+ return new ThreadPoolTaskExecutor ();
300
301
}
301
302
302
303
@ Bean
303
304
@ Qualifier ("e2" )
304
305
public Executor otherExecutor () {
305
- ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor ();
306
- return executor ;
306
+ return new ThreadPoolTaskExecutor ();
307
307
}
308
308
}
309
+
309
310
}
0 commit comments