@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2002 - 2014 the original author or authors .
* Copyright 2002 - 2015 the original author or authors .
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
@ -67,17 +67,6 @@ public class EnableAsyncTests {
@@ -67,17 +67,6 @@ public class EnableAsyncTests {
asyncBean . work ( ) ;
}
@Configuration
@EnableAsync
static class AsyncConfig {
@Bean
public AsyncBean asyncBean ( ) {
return new AsyncBean ( ) ;
}
}
@Test
public void withAsyncBeanWithExecutorQualifiedByName ( ) throws ExecutionException , InterruptedException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ( ) ;
@ -95,8 +84,80 @@ public class EnableAsyncTests {
@@ -95,8 +84,80 @@ public class EnableAsyncTests {
assertThat ( workerThread3 . get ( ) . getName ( ) , startsWith ( "otherExecutor-" ) ) ;
}
@Test
public void asyncProcessorIsOrderedLowestPrecedenceByDefault ( ) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ( ) ;
ctx . register ( AsyncConfig . class ) ;
ctx . refresh ( ) ;
AsyncAnnotationBeanPostProcessor bpp = ctx . getBean ( AsyncAnnotationBeanPostProcessor . class ) ;
assertThat ( bpp . getOrder ( ) , is ( Ordered . LOWEST_PRECEDENCE ) ) ;
}
@Test
public void orderAttributeIsPropagated ( ) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ( ) ;
ctx . register ( OrderedAsyncConfig . class ) ;
ctx . refresh ( ) ;
AsyncAnnotationBeanPostProcessor bpp = ctx . getBean ( AsyncAnnotationBeanPostProcessor . class ) ;
assertThat ( bpp . getOrder ( ) , is ( Ordered . HIGHEST_PRECEDENCE ) ) ;
}
@Test
public void customAsyncAnnotationIsPropagated ( ) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ( ) ;
ctx . register ( CustomAsyncAnnotationConfig . class ) ;
ctx . refresh ( ) ;
Object bean = ctx . getBean ( CustomAsyncBean . class ) ;
assertTrue ( AopUtils . isAopProxy ( bean ) ) ;
boolean isAsyncAdvised = false ;
for ( Advisor advisor : ( ( Advised ) bean ) . getAdvisors ( ) ) {
if ( advisor instanceof AsyncAnnotationAdvisor ) {
isAsyncAdvised = true ;
break ;
}
}
assertTrue ( "bean was not async advised as expected" , isAsyncAdvised ) ;
}
/ * *
* Fails with classpath errors on trying to classload AnnotationAsyncExecutionAspect
* /
@Test ( expected = BeanDefinitionStoreException . class )
public void aspectModeAspectJAttemptsToRegisterAsyncAspect ( ) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ( ) ;
ctx . register ( AspectJAsyncAnnotationConfig . class ) ;
ctx . refresh ( ) ;
}
@Test
public void customExecutorIsPropagated ( ) throws InterruptedException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ( ) ;
ctx . register ( CustomExecutorAsyncConfig . class ) ;
ctx . refresh ( ) ;
AsyncBean asyncBean = ctx . getBean ( AsyncBean . class ) ;
asyncBean . work ( ) ;
Thread . sleep ( 500 ) ;
assertThat ( asyncBean . getThreadOfExecution ( ) . getName ( ) , startsWith ( "Custom-" ) ) ;
TestableAsyncUncaughtExceptionHandler exceptionHandler = ( TestableAsyncUncaughtExceptionHandler )
ctx . getBean ( "exceptionHandler" ) ;
assertFalse ( "handler should not have been called yet" , exceptionHandler . isCalled ( ) ) ;
asyncBean . fail ( ) ;
Thread . sleep ( 500 ) ;
Method m = ReflectionUtils . findMethod ( AsyncBean . class , "fail" ) ;
exceptionHandler . assertCalledWith ( m , UnsupportedOperationException . class ) ;
ctx . close ( ) ;
}
static class AsyncBeanWithExecutorQualifiedByName {
@Async
public Future < Thread > work0 ( ) {
return new AsyncResult < Thread > ( Thread . currentThread ( ) ) ;
@ -120,6 +181,7 @@ public class EnableAsyncTests {
@@ -120,6 +181,7 @@ public class EnableAsyncTests {
static class AsyncBean {
private Thread threadOfExecution ;
@Async
@ -138,60 +200,10 @@ public class EnableAsyncTests {
@@ -138,60 +200,10 @@ public class EnableAsyncTests {
}
@Test
public void asyncProcessorIsOrderedLowestPrecedenceByDefault ( ) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ( ) ;
ctx . register ( AsyncConfig . class ) ;
ctx . refresh ( ) ;
AsyncAnnotationBeanPostProcessor bpp = ctx . getBean ( AsyncAnnotationBeanPostProcessor . class ) ;
assertThat ( bpp . getOrder ( ) , is ( Ordered . LOWEST_PRECEDENCE ) ) ;
}
@Test
public void orderAttributeIsPropagated ( ) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ( ) ;
ctx . register ( OrderedAsyncConfig . class ) ;
ctx . refresh ( ) ;
AsyncAnnotationBeanPostProcessor bpp = ctx . getBean ( AsyncAnnotationBeanPostProcessor . class ) ;
assertThat ( bpp . getOrder ( ) , is ( Ordered . HIGHEST_PRECEDENCE ) ) ;
}
@Configuration
@EnableAsync ( order = Ordered . HIGHEST_PRECEDENCE )
static class OrderedAsyncConfig {
@Bean
public AsyncBean asyncBean ( ) {
return new AsyncBean ( ) ;
}
}
@Test
public void customAsyncAnnotationIsPropagated ( ) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ( ) ;
ctx . register ( CustomAsyncAnnotationConfig . class ) ;
ctx . refresh ( ) ;
Object bean = ctx . getBean ( CustomAsyncBean . class ) ;
assertTrue ( AopUtils . isAopProxy ( bean ) ) ;
boolean isAsyncAdvised = false ;
for ( Advisor advisor : ( ( Advised ) bean ) . getAdvisors ( ) ) {
if ( advisor instanceof AsyncAnnotationAdvisor ) {
isAsyncAdvised = true ;
break ;
}
}
assertTrue ( "bean was not async advised as expected" , isAsyncAdvised ) ;
}
@Configuration
@EnableAsync ( annotation = CustomAsync . class )
@EnableAsync ( annotation = CustomAsync . class )
static class CustomAsyncAnnotationConfig {
@Bean
public CustomAsyncBean asyncBean ( ) {
return new CustomAsyncBean ( ) ;
@ -206,25 +218,26 @@ public class EnableAsyncTests {
@@ -206,25 +218,26 @@ public class EnableAsyncTests {
static class CustomAsyncBean {
@CustomAsync
public void work ( ) {
}
}
/ * *
* Fails with classpath errors on trying to classload AnnotationAsyncExecutionAspect
* /
@Test ( expected = BeanDefinitionStoreException . class )
public void aspectModeAspectJAttemptsToRegisterAsyncAspect ( ) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ( ) ;
ctx . register ( AspectJAsyncAnnotationConfig . class ) ;
ctx . refresh ( ) ;
@Configuration
@EnableAsync ( order = Ordered . HIGHEST_PRECEDENCE )
static class OrderedAsyncConfig {
@Bean
public AsyncBean asyncBean ( ) {
return new AsyncBean ( ) ;
}
}
@Configuration
@EnableAsync ( mode = AdviceMode . ASPECTJ )
@EnableAsync ( mode = AdviceMode . ASPECTJ )
static class AspectJAsyncAnnotationConfig {
@Bean
public AsyncBean asyncBean ( ) {
@ -233,33 +246,21 @@ public class EnableAsyncTests {
@@ -233,33 +246,21 @@ public class EnableAsyncTests {
}
@Test
public void customExecutorIsPropagated ( ) throws InterruptedException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ( ) ;
ctx . register ( CustomExecutorAsyncConfig . class ) ;
ctx . refresh ( ) ;
AsyncBean asyncBean = ctx . getBean ( AsyncBean . class ) ;
asyncBean . work ( ) ;
Thread . sleep ( 500 ) ;
assertThat ( asyncBean . getThreadOfExecution ( ) . getName ( ) , startsWith ( "Custom-" ) ) ;
TestableAsyncUncaughtExceptionHandler exceptionHandler = ( TestableAsyncUncaughtExceptionHandler )
ctx . getBean ( "exceptionHandler" ) ;
assertFalse ( "handler should not have been called yet" , exceptionHandler . isCalled ( ) ) ;
asyncBean . fail ( ) ;
Thread . sleep ( 500 ) ;
Method m = ReflectionUtils . findMethod ( AsyncBean . class , "fail" ) ;
exceptionHandler . assertCalledWith ( m , UnsupportedOperationException . class ) ;
@Configuration
@EnableAsync
static class AsyncConfig {
ctx . close ( ) ;
@Bean
public AsyncBean asyncBean ( ) {
return new AsyncBean ( ) ;
}
}
@Configuration
@EnableAsync
static class CustomExecutorAsyncConfig implements AsyncConfigurer {
@Bean
public AsyncBean asyncBean ( ) {
return new AsyncBean ( ) ;
@ -288,6 +289,7 @@ public class EnableAsyncTests {
@@ -288,6 +289,7 @@ public class EnableAsyncTests {
@Configuration
@EnableAsync
static class AsyncWithExecutorQualifiedByNameConfig {
@Bean
public AsyncBeanWithExecutorQualifiedByName asyncBean ( ) {
return new AsyncBeanWithExecutorQualifiedByName ( ) ;
@ -295,15 +297,14 @@ public class EnableAsyncTests {
@@ -295,15 +297,14 @@ public class EnableAsyncTests {
@Bean
public Executor e1 ( ) {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor ( ) ;
return executor ;
return new ThreadPoolTaskExecutor ( ) ;
}
@Bean
@Qualifier ( "e2" )
public Executor otherExecutor ( ) {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor ( ) ;
return executor ;
return new ThreadPoolTaskExecutor ( ) ;
}
}
}