|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2015 the original author or authors. |
|
|
|
* Copyright 2002-2018 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -23,8 +23,10 @@ import java.util.concurrent.Executor; |
|
|
|
import java.util.concurrent.Future; |
|
|
|
import java.util.concurrent.Future; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.aopalliance.intercept.MethodInterceptor; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.aop.framework.ProxyFactory; |
|
|
|
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; |
|
|
|
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; |
|
|
|
import org.springframework.aop.support.AopUtils; |
|
|
|
import org.springframework.aop.support.AopUtils; |
|
|
|
import org.springframework.beans.factory.config.BeanDefinition; |
|
|
|
import org.springframework.beans.factory.config.BeanDefinition; |
|
|
|
@ -62,6 +64,26 @@ public class AsyncAnnotationBeanPostProcessorTests { |
|
|
|
public void invokedAsynchronously() { |
|
|
|
public void invokedAsynchronously() { |
|
|
|
ConfigurableApplicationContext context = initContext( |
|
|
|
ConfigurableApplicationContext context = initContext( |
|
|
|
new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class)); |
|
|
|
new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ITestBean testBean = context.getBean("target", ITestBean.class); |
|
|
|
|
|
|
|
testBean.test(); |
|
|
|
|
|
|
|
Thread mainThread = Thread.currentThread(); |
|
|
|
|
|
|
|
testBean.await(3000); |
|
|
|
|
|
|
|
Thread asyncThread = testBean.getThread(); |
|
|
|
|
|
|
|
assertNotSame(mainThread, asyncThread); |
|
|
|
|
|
|
|
context.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void invokedAsynchronouslyOnProxyTarget() { |
|
|
|
|
|
|
|
StaticApplicationContext context = new StaticApplicationContext(); |
|
|
|
|
|
|
|
context.registerBeanDefinition("postProcessor", new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class)); |
|
|
|
|
|
|
|
TestBean tb = new TestBean(); |
|
|
|
|
|
|
|
ProxyFactory pf = new ProxyFactory(ITestBean.class, |
|
|
|
|
|
|
|
(MethodInterceptor) invocation -> invocation.getMethod().invoke(tb, invocation.getArguments())); |
|
|
|
|
|
|
|
context.registerBean("target", ITestBean.class, () -> (ITestBean) pf.getProxy()); |
|
|
|
|
|
|
|
context.refresh(); |
|
|
|
|
|
|
|
|
|
|
|
ITestBean testBean = context.getBean("target", ITestBean.class); |
|
|
|
ITestBean testBean = context.getBean("target", ITestBean.class); |
|
|
|
testBean.test(); |
|
|
|
testBean.test(); |
|
|
|
Thread mainThread = Thread.currentThread(); |
|
|
|
Thread mainThread = Thread.currentThread(); |
|
|
|
@ -79,6 +101,7 @@ public class AsyncAnnotationBeanPostProcessorTests { |
|
|
|
executor.afterPropertiesSet(); |
|
|
|
executor.afterPropertiesSet(); |
|
|
|
processorDefinition.getPropertyValues().add("executor", executor); |
|
|
|
processorDefinition.getPropertyValues().add("executor", executor); |
|
|
|
ConfigurableApplicationContext context = initContext(processorDefinition); |
|
|
|
ConfigurableApplicationContext context = initContext(processorDefinition); |
|
|
|
|
|
|
|
|
|
|
|
ITestBean testBean = context.getBean("target", ITestBean.class); |
|
|
|
ITestBean testBean = context.getBean("target", ITestBean.class); |
|
|
|
testBean.test(); |
|
|
|
testBean.test(); |
|
|
|
testBean.await(3000); |
|
|
|
testBean.await(3000); |
|
|
|
@ -246,8 +269,7 @@ public class AsyncAnnotationBeanPostProcessorTests { |
|
|
|
|
|
|
|
|
|
|
|
private ConfigurableApplicationContext initContext(BeanDefinition asyncAnnotationBeanPostProcessorDefinition) { |
|
|
|
private ConfigurableApplicationContext initContext(BeanDefinition asyncAnnotationBeanPostProcessorDefinition) { |
|
|
|
StaticApplicationContext context = new StaticApplicationContext(); |
|
|
|
StaticApplicationContext context = new StaticApplicationContext(); |
|
|
|
BeanDefinition targetDefinition = |
|
|
|
BeanDefinition targetDefinition = new RootBeanDefinition(TestBean.class); |
|
|
|
new RootBeanDefinition(AsyncAnnotationBeanPostProcessorTests.TestBean.class); |
|
|
|
|
|
|
|
context.registerBeanDefinition("postProcessor", asyncAnnotationBeanPostProcessorDefinition); |
|
|
|
context.registerBeanDefinition("postProcessor", asyncAnnotationBeanPostProcessorDefinition); |
|
|
|
context.registerBeanDefinition("target", targetDefinition); |
|
|
|
context.registerBeanDefinition("target", targetDefinition); |
|
|
|
context.refresh(); |
|
|
|
context.refresh(); |
|
|
|
@ -259,6 +281,7 @@ public class AsyncAnnotationBeanPostProcessorTests { |
|
|
|
|
|
|
|
|
|
|
|
Thread getThread(); |
|
|
|
Thread getThread(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Async |
|
|
|
void test(); |
|
|
|
void test(); |
|
|
|
|
|
|
|
|
|
|
|
Future<Object> failWithFuture(); |
|
|
|
Future<Object> failWithFuture(); |
|
|
|
|