@ -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 .
@ -16,10 +16,12 @@
@@ -16,10 +16,12 @@
package org.springframework.scheduling.annotation ;
import java.io.IOException ;
import java.io.Serializable ;
import java.lang.annotation.Retention ;
import java.lang.annotation.RetentionPolicy ;
import java.util.HashMap ;
import java.util.concurrent.ExecutionException ;
import java.util.concurrent.Future ;
import org.aopalliance.intercept.MethodInterceptor ;
@ -61,6 +63,7 @@ public class AsyncExecutionTests {
@@ -61,6 +63,7 @@ public class AsyncExecutionTests {
context . registerBeanDefinition ( "autoProxyCreator" , new RootBeanDefinition ( DefaultAdvisorAutoProxyCreator . class ) ) ;
context . registerBeanDefinition ( "asyncAdvisor" , new RootBeanDefinition ( AsyncAnnotationAdvisor . class ) ) ;
context . refresh ( ) ;
AsyncMethodBean asyncTest = context . getBean ( "asyncTest" , AsyncMethodBean . class ) ;
asyncTest . doNothing ( 5 ) ;
asyncTest . doSomething ( 10 ) ;
@ -68,6 +71,24 @@ public class AsyncExecutionTests {
@@ -68,6 +71,24 @@ public class AsyncExecutionTests {
assertEquals ( "20" , future . get ( ) ) ;
ListenableFuture < String > listenableFuture = asyncTest . returnSomethingListenable ( 20 ) ;
assertEquals ( "20" , listenableFuture . get ( ) ) ;
future = asyncTest . returnSomething ( 0 ) ;
try {
future . get ( ) ;
fail ( "Should have thrown ExecutionException" ) ;
}
catch ( ExecutionException ex ) {
assertTrue ( ex . getCause ( ) instanceof IllegalArgumentException ) ;
}
future = asyncTest . returnSomething ( - 1 ) ;
try {
future . get ( ) ;
fail ( "Should have thrown ExecutionException" ) ;
}
catch ( ExecutionException ex ) {
assertTrue ( ex . getCause ( ) instanceof IOException ) ;
}
}
@Test
@ -96,6 +117,7 @@ public class AsyncExecutionTests {
@@ -96,6 +117,7 @@ public class AsyncExecutionTests {
context . registerBeanDefinition ( "e1" , new RootBeanDefinition ( ThreadPoolTaskExecutor . class ) ) ;
context . registerBeanDefinition ( "e2" , new RootBeanDefinition ( ThreadPoolTaskExecutor . class ) ) ;
context . refresh ( ) ;
AsyncMethodWithQualifierBean asyncTest = context . getBean ( "asyncTest" , AsyncMethodWithQualifierBean . class ) ;
asyncTest . doNothing ( 5 ) ;
asyncTest . doSomething ( 10 ) ;
@ -116,6 +138,7 @@ public class AsyncExecutionTests {
@@ -116,6 +138,7 @@ public class AsyncExecutionTests {
context . registerBeanDefinition ( "e1" , new RootBeanDefinition ( ThreadPoolTaskExecutor . class ) ) ;
context . registerBeanDefinition ( "e2" , new RootBeanDefinition ( ThreadPoolTaskExecutor . class ) ) ;
context . refresh ( ) ;
SimpleInterface asyncTest = context . getBean ( "asyncTest" , SimpleInterface . class ) ;
asyncTest . doNothing ( 5 ) ;
asyncTest . doSomething ( 10 ) ;
@ -133,6 +156,7 @@ public class AsyncExecutionTests {
@@ -133,6 +156,7 @@ public class AsyncExecutionTests {
context . registerBeanDefinition ( "autoProxyCreator" , new RootBeanDefinition ( DefaultAdvisorAutoProxyCreator . class ) ) ;
context . registerBeanDefinition ( "asyncAdvisor" , new RootBeanDefinition ( AsyncAnnotationAdvisor . class ) ) ;
context . refresh ( ) ;
AsyncClassBean asyncTest = context . getBean ( "asyncTest" , AsyncClassBean . class ) ;
asyncTest . doSomething ( 10 ) ;
Future < String > future = asyncTest . returnSomething ( 20 ) ;
@ -148,6 +172,7 @@ public class AsyncExecutionTests {
@@ -148,6 +172,7 @@ public class AsyncExecutionTests {
context . registerBeanDefinition ( "asyncTest" , new RootBeanDefinition ( AsyncClassBean . class ) ) ;
context . registerBeanDefinition ( "asyncProcessor" , new RootBeanDefinition ( AsyncAnnotationBeanPostProcessor . class ) ) ;
context . refresh ( ) ;
AsyncClassBean asyncTest = context . getBean ( "asyncTest" , AsyncClassBean . class ) ;
asyncTest . doSomething ( 10 ) ;
Future < String > future = asyncTest . returnSomething ( 20 ) ;
@ -162,6 +187,7 @@ public class AsyncExecutionTests {
@@ -162,6 +187,7 @@ public class AsyncExecutionTests {
context . registerBeanDefinition ( "autoProxyCreator" , new RootBeanDefinition ( DefaultAdvisorAutoProxyCreator . class ) ) ;
context . registerBeanDefinition ( "asyncAdvisor" , new RootBeanDefinition ( AsyncAnnotationAdvisor . class ) ) ;
context . refresh ( ) ;
RegularInterface asyncTest = context . getBean ( "asyncTest" , RegularInterface . class ) ;
asyncTest . doSomething ( 10 ) ;
Future < String > future = asyncTest . returnSomething ( 20 ) ;
@ -175,6 +201,7 @@ public class AsyncExecutionTests {
@@ -175,6 +201,7 @@ public class AsyncExecutionTests {
context . registerBeanDefinition ( "asyncTest" , new RootBeanDefinition ( AsyncClassBeanWithInterface . class ) ) ;
context . registerBeanDefinition ( "asyncProcessor" , new RootBeanDefinition ( AsyncAnnotationBeanPostProcessor . class ) ) ;
context . refresh ( ) ;
RegularInterface asyncTest = context . getBean ( "asyncTest" , RegularInterface . class ) ;
asyncTest . doSomething ( 10 ) ;
Future < String > future = asyncTest . returnSomething ( 20 ) ;
@ -189,6 +216,7 @@ public class AsyncExecutionTests {
@@ -189,6 +216,7 @@ public class AsyncExecutionTests {
context . registerBeanDefinition ( "autoProxyCreator" , new RootBeanDefinition ( DefaultAdvisorAutoProxyCreator . class ) ) ;
context . registerBeanDefinition ( "asyncAdvisor" , new RootBeanDefinition ( AsyncAnnotationAdvisor . class ) ) ;
context . refresh ( ) ;
AsyncInterface asyncTest = context . getBean ( "asyncTest" , AsyncInterface . class ) ;
asyncTest . doSomething ( 10 ) ;
Future < String > future = asyncTest . returnSomething ( 20 ) ;
@ -202,6 +230,7 @@ public class AsyncExecutionTests {
@@ -202,6 +230,7 @@ public class AsyncExecutionTests {
context . registerBeanDefinition ( "asyncTest" , new RootBeanDefinition ( AsyncInterfaceBean . class ) ) ;
context . registerBeanDefinition ( "asyncProcessor" , new RootBeanDefinition ( AsyncAnnotationBeanPostProcessor . class ) ) ;
context . refresh ( ) ;
AsyncInterface asyncTest = context . getBean ( "asyncTest" , AsyncInterface . class ) ;
asyncTest . doSomething ( 10 ) ;
Future < String > future = asyncTest . returnSomething ( 20 ) ;
@ -216,6 +245,7 @@ public class AsyncExecutionTests {
@@ -216,6 +245,7 @@ public class AsyncExecutionTests {
context . registerBeanDefinition ( "autoProxyCreator" , new RootBeanDefinition ( DefaultAdvisorAutoProxyCreator . class ) ) ;
context . registerBeanDefinition ( "asyncAdvisor" , new RootBeanDefinition ( AsyncAnnotationAdvisor . class ) ) ;
context . refresh ( ) ;
AsyncInterface asyncTest = context . getBean ( "asyncTest" , AsyncInterface . class ) ;
asyncTest . doSomething ( 10 ) ;
Future < String > future = asyncTest . returnSomething ( 20 ) ;
@ -229,6 +259,7 @@ public class AsyncExecutionTests {
@@ -229,6 +259,7 @@ public class AsyncExecutionTests {
context . registerBeanDefinition ( "asyncTest" , new RootBeanDefinition ( DynamicAsyncInterfaceBean . class ) ) ;
context . registerBeanDefinition ( "asyncProcessor" , new RootBeanDefinition ( AsyncAnnotationBeanPostProcessor . class ) ) ;
context . refresh ( ) ;
AsyncInterface asyncTest = context . getBean ( "asyncTest" , AsyncInterface . class ) ;
asyncTest . doSomething ( 10 ) ;
Future < String > future = asyncTest . returnSomething ( 20 ) ;
@ -243,6 +274,7 @@ public class AsyncExecutionTests {
@@ -243,6 +274,7 @@ public class AsyncExecutionTests {
context . registerBeanDefinition ( "autoProxyCreator" , new RootBeanDefinition ( DefaultAdvisorAutoProxyCreator . class ) ) ;
context . registerBeanDefinition ( "asyncAdvisor" , new RootBeanDefinition ( AsyncAnnotationAdvisor . class ) ) ;
context . refresh ( ) ;
AsyncMethodsInterface asyncTest = context . getBean ( "asyncTest" , AsyncMethodsInterface . class ) ;
asyncTest . doNothing ( 5 ) ;
asyncTest . doSomething ( 10 ) ;
@ -257,6 +289,7 @@ public class AsyncExecutionTests {
@@ -257,6 +289,7 @@ public class AsyncExecutionTests {
context . registerBeanDefinition ( "asyncTest" , new RootBeanDefinition ( AsyncMethodsInterfaceBean . class ) ) ;
context . registerBeanDefinition ( "asyncProcessor" , new RootBeanDefinition ( AsyncAnnotationBeanPostProcessor . class ) ) ;
context . refresh ( ) ;
AsyncMethodsInterface asyncTest = context . getBean ( "asyncTest" , AsyncMethodsInterface . class ) ;
asyncTest . doNothing ( 5 ) ;
asyncTest . doSomething ( 10 ) ;
@ -271,6 +304,7 @@ public class AsyncExecutionTests {
@@ -271,6 +304,7 @@ public class AsyncExecutionTests {
context . registerBeanDefinition ( "asyncTest" , new RootBeanDefinition ( DynamicAsyncMethodsInterfaceBean . class ) ) ;
context . registerBeanDefinition ( "asyncProcessor" , new RootBeanDefinition ( AsyncAnnotationBeanPostProcessor . class ) ) ;
context . refresh ( ) ;
AsyncMethodsInterface asyncTest = context . getBean ( "asyncTest" , AsyncMethodsInterface . class ) ;
asyncTest . doSomething ( 10 ) ;
Future < String > future = asyncTest . returnSomething ( 20 ) ;
@ -351,7 +385,13 @@ public class AsyncExecutionTests {
@@ -351,7 +385,13 @@ public class AsyncExecutionTests {
@Async
public Future < String > returnSomething ( int i ) {
assertTrue ( ! Thread . currentThread ( ) . getName ( ) . equals ( originalThreadName ) ) ;
return new AsyncResult < String > ( Integer . toString ( i ) ) ;
if ( i = = 0 ) {
throw new IllegalArgumentException ( ) ;
}
else if ( i < 0 ) {
return AsyncResult . forExecutionException ( new IOException ( ) ) ;
}
return AsyncResult . forValue ( Integer . toString ( i ) ) ;
}
@Async