Browse Source
Introduce @EnableAsync#order AsyncAnnotationBeanPostProcessor's 'order' property is now mutable; @EnableAsync's 'order()' attribute allows for setting it, but must have a default value, thus uses the new Ordered#NOT_ORDERED constant - a reserved negative number very unlikely to be otherwise used that may be interpreted as 'not ordered', useful in annotation defaulting scenarios where null is not an option. Introduce first working cut of AsyncConfiguration Remove AsyncCapabilitypull/7/head
Chris Beams
14 years ago
11 changed files with 639 additions and 4 deletions
@ -0,0 +1,49 @@
@@ -0,0 +1,49 @@
|
||||
/* |
||||
* Copyright 2002-2011 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.scheduling.aspectj; |
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition; |
||||
import org.springframework.context.annotation.AnnotationConfigUtils; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.context.annotation.Role; |
||||
import org.springframework.scheduling.annotation.AbstractAsyncConfiguration; |
||||
import org.springframework.scheduling.annotation.EnableAsync; |
||||
|
||||
/** |
||||
* Enables AspectJ-based asynchronous method execution. |
||||
* |
||||
* @author Chris Beams |
||||
* @since 3.1 |
||||
* @see EnableAsync |
||||
* @see EnableAsync#mode |
||||
*/ |
||||
@Configuration |
||||
public class AspectJAsyncConfiguration extends AbstractAsyncConfiguration { |
||||
|
||||
@Override |
||||
@Bean(name=AnnotationConfigUtils.ASYNC_EXECUTION_ASPECT_BEAN_NAME) |
||||
@Role(BeanDefinition.ROLE_INFRASTRUCTURE) |
||||
public AnnotationAsyncExecutionAspect asyncAdvisor() { |
||||
AnnotationAsyncExecutionAspect asyncAspect = AnnotationAsyncExecutionAspect.aspectOf(); |
||||
if (this.executor != null) { |
||||
asyncAspect.setExecutor(this.executor); |
||||
} |
||||
return asyncAspect; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,73 @@
@@ -0,0 +1,73 @@
|
||||
/* |
||||
* Copyright 2002-2011 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.scheduling.annotation; |
||||
|
||||
import java.util.Collection; |
||||
import java.util.Map; |
||||
import java.util.concurrent.Executor; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.context.annotation.ImportAware; |
||||
import org.springframework.core.type.AnnotationMetadata; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* Abstract base class providing common structure for enabling Spring's asynchronous |
||||
* method execution capability. |
||||
* |
||||
* @author Chris Beams |
||||
* @since 3.1 |
||||
* @see EnableAsync |
||||
*/ |
||||
@Configuration |
||||
public abstract class AbstractAsyncConfiguration implements ImportAware { |
||||
|
||||
protected Map<String, Object> enableAsync; |
||||
protected Executor executor; |
||||
|
||||
public void setImportMetadata(AnnotationMetadata importMetadata) { |
||||
enableAsync = importMetadata.getAnnotationAttributes(EnableAsync.class.getName(), false); |
||||
Assert.notNull(enableAsync, |
||||
"@EnableAsync is not present on importing class " + |
||||
importMetadata.getClassName()); |
||||
} |
||||
|
||||
/** |
||||
* The component that will apply async execution advice to beans annotated with |
||||
* the async annotation. Subclasses will provide either a BeanPostProcessor in |
||||
* the case of proxy-based advice, or an AspectJ aspect if weaving is preferred. |
||||
*/ |
||||
public abstract Object asyncAdvisor(); |
||||
|
||||
/** |
||||
* Collect any {@link AsyncConfigurer} beans through autowiring. |
||||
*/ |
||||
@Autowired(required = false) |
||||
void setConfigurers(Collection<AsyncConfigurer> configurers) { |
||||
if (configurers == null || configurers.isEmpty()) { |
||||
return; |
||||
} |
||||
|
||||
if (configurers.size() > 1) { |
||||
throw new IllegalStateException("only one AsyncConfigurer may exist"); |
||||
} |
||||
|
||||
AsyncConfigurer configurer = configurers.iterator().next(); |
||||
this.executor = configurer.getExecutor(); |
||||
} |
||||
} |
@ -0,0 +1,58 @@
@@ -0,0 +1,58 @@
|
||||
/* |
||||
* Copyright 2002-2011 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.scheduling.annotation; |
||||
|
||||
import java.util.Map; |
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigUtils; |
||||
import org.springframework.context.annotation.ImportSelector; |
||||
import org.springframework.context.config.AdviceMode; |
||||
import org.springframework.core.type.AnnotationMetadata; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* Select which implementation of {@link AbstractAsyncConfiguration} |
||||
* should be used based on the value of {@link EnableAsync#mode} on the |
||||
* importing @{@link Configuration} class. |
||||
* |
||||
* @author Chris Beams |
||||
* @since 3.1 |
||||
* @see EnableAsync |
||||
* @see AbstractAsyncConfiguration |
||||
* @see ProxyAsyncConfiguration |
||||
* @see AnnotationConfigUtils#ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME |
||||
*/ |
||||
public class AsyncConfigurationSelector implements ImportSelector { |
||||
|
||||
public String[] selectImports(AnnotationMetadata importingClassMetadata) { |
||||
Map<String, Object> enableAsync = |
||||
importingClassMetadata.getAnnotationAttributes(EnableAsync.class.getName()); |
||||
Assert.notNull(enableAsync, |
||||
"@EnableAsync is not present on importing class " + |
||||
importingClassMetadata.getClassName()); |
||||
|
||||
switch ((AdviceMode) enableAsync.get("mode")) { |
||||
case PROXY: |
||||
return new String[] {ProxyAsyncConfiguration.class.getName()}; |
||||
case ASPECTJ: |
||||
return new String[] {AnnotationConfigUtils.ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME}; |
||||
default: |
||||
throw new IllegalArgumentException("Unknown AdviceMode " + enableAsync.get("mode")); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,41 @@
@@ -0,0 +1,41 @@
|
||||
/* |
||||
* Copyright 2002-2011 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.scheduling.annotation; |
||||
|
||||
import java.util.concurrent.Executor; |
||||
|
||||
/** |
||||
* Interface to be implemented by @{@link Configuration} classes |
||||
* annotated with @{@link EnableAsync} that wish to customize the |
||||
* {@link Executor} instance used when processing async method invocations. |
||||
* |
||||
* <p>See {@link EnableAsync} for usage examples. |
||||
* |
||||
* @author Chris Beams |
||||
* @since 3.1 |
||||
* @see AbstractAsyncConfiguration |
||||
* @see EnableAsync |
||||
*/ |
||||
public interface AsyncConfigurer { |
||||
|
||||
/** |
||||
* The {@link Executor} instance to be used when processing async |
||||
* method invocations. |
||||
*/ |
||||
Executor getExecutor(); |
||||
|
||||
} |
@ -0,0 +1,121 @@
@@ -0,0 +1,121 @@
|
||||
/* |
||||
* Copyright 2002-2011 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.scheduling.annotation; |
||||
|
||||
import java.lang.annotation.Annotation; |
||||
import java.lang.annotation.Documented; |
||||
import java.lang.annotation.ElementType; |
||||
import java.lang.annotation.Retention; |
||||
import java.lang.annotation.RetentionPolicy; |
||||
import java.lang.annotation.Target; |
||||
|
||||
import org.springframework.context.annotation.Import; |
||||
import org.springframework.context.config.AdviceMode; |
||||
import org.springframework.core.Ordered; |
||||
|
||||
/** |
||||
* Enables Spring's asynchronous method execution capability. To be used |
||||
* on @{@link Configuration} classes as follows: |
||||
* |
||||
* <pre class="code"> |
||||
* @Configuration |
||||
* @EnableAsync |
||||
* public class AppConfig { |
||||
* @Bean |
||||
* public MyAsyncBean asyncBean() { |
||||
* return new MyAsyncBean(); |
||||
* } |
||||
* }</pre> |
||||
* |
||||
* <p>The various attributes of the annotation control how advice |
||||
* is applied ({@link #mode()}), and if the mode is {@link AdviceMode#PROXY} |
||||
* (the default), the other attributes control the behavior of the proxying. |
||||
* |
||||
* <p>Note that if the {@linkplain #mode} is set to {@link AdviceMode#ASPECTJ} |
||||
* the {@code org.springframework.aspects} module must be present on the classpath. |
||||
* |
||||
* <p>By default, a {@link org.springframework.core.task.SimpleAsyncTaskExecutor |
||||
* SimpleAsyncTaskExecutor} will be used to process async method invocations. To |
||||
* customize this behavior, implement {@link AsyncConfigurer} and |
||||
* provide your own {@link java.util.concurrent.Executor Executor} through the |
||||
* {@link AsyncConfigurer#getExecutor() getExecutor()} method. |
||||
* |
||||
* <pre class="code"> |
||||
* @Configuration |
||||
* @EnableAsync |
||||
* public class AppConfig implements AsyncConfigurer { |
||||
* |
||||
* @Bean |
||||
* public MyAsyncBean asyncBean() { |
||||
* return new MyAsyncBean(); |
||||
* } |
||||
* |
||||
* public Executor getExecutor() { |
||||
* ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); |
||||
* executor.setThreadNamePrefix("Custom-"); |
||||
* executor.initialize(); |
||||
* return executor; |
||||
* } |
||||
* }</pre> |
||||
* |
||||
* @author Chris Beams |
||||
* @since 3.1 |
||||
* @see Async |
||||
* @see AsyncConfigurer |
||||
* @see AsyncConfigurationSelector |
||||
*/ |
||||
@Target(ElementType.TYPE) |
||||
@Retention(RetentionPolicy.RUNTIME) |
||||
@Import(AsyncConfigurationSelector.class) |
||||
@Documented |
||||
public @interface EnableAsync { |
||||
|
||||
/** |
||||
* Indicate the 'async' annotation type to be detected at either class |
||||
* or method level. By default, both the {@link Async} annotation and |
||||
* the EJB 3.1 <code>javax.ejb.Asynchronous</code> annotation will be |
||||
* detected. <p>This setter property exists so that developers can provide |
||||
* their own (non-Spring-specific) annotation type to indicate that a method |
||||
* (or all methods of a given class) should be invoked asynchronously. |
||||
*/ |
||||
Class<? extends Annotation> annotation() default Annotation.class; |
||||
|
||||
/** |
||||
* Indicate whether class-based (CGLIB) proxies are to be created as opposed |
||||
* to standard Java interface-based proxies. The default is {@code false} |
||||
* |
||||
* <p>Note: Class-based proxies require the async {@link #annotation()} |
||||
* to be defined on the concrete class. Annotations in interfaces will |
||||
* not work in that case (they will rather only work with interface-based proxies)! |
||||
*/ |
||||
boolean proxyTargetClass() default false; |
||||
|
||||
/** |
||||
* Indicate how async advice should be applied. |
||||
* The default is {@link AdviceMode#PROXY}. |
||||
*/ |
||||
AdviceMode mode() default AdviceMode.PROXY; |
||||
|
||||
/** |
||||
* Indicate the order in which the |
||||
* {@link org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor} |
||||
* should be applied. Defaults to Order.LOWEST_PRIORITY in order to run |
||||
* after all other post-processors, so that it can add an advisor to |
||||
* existing proxies rather than double-proxy. |
||||
*/ |
||||
int order() default Ordered.LOWEST_PRECEDENCE; |
||||
} |
@ -0,0 +1,66 @@
@@ -0,0 +1,66 @@
|
||||
/* |
||||
* Copyright 2002-2011 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.scheduling.annotation; |
||||
|
||||
import java.lang.annotation.Annotation; |
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition; |
||||
import org.springframework.context.annotation.AnnotationConfigUtils; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.context.annotation.Role; |
||||
import org.springframework.core.annotation.AnnotationUtils; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* Enables proxy-based asynchronous method execution. |
||||
* |
||||
* @author Chris Beams |
||||
* @since 3.1 |
||||
* @see EnableAsync |
||||
* @see EnableAsync#mode |
||||
*/ |
||||
@Configuration |
||||
public class ProxyAsyncConfiguration extends AbstractAsyncConfiguration { |
||||
|
||||
@Override |
||||
@Bean(name=AnnotationConfigUtils.ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME) |
||||
@Role(BeanDefinition.ROLE_INFRASTRUCTURE) |
||||
public AsyncAnnotationBeanPostProcessor asyncAdvisor() { |
||||
Assert.notNull(enableAsync, "@EnableAsync annotation metadata was not injected"); |
||||
|
||||
AsyncAnnotationBeanPostProcessor bpp = new AsyncAnnotationBeanPostProcessor(); |
||||
|
||||
@SuppressWarnings("unchecked") |
||||
Class<? extends Annotation> customAsyncAnnotation = |
||||
(Class<? extends Annotation>) enableAsync.get("annotation"); |
||||
if (customAsyncAnnotation != AnnotationUtils.getDefaultValue(EnableAsync.class, "annotation")) { |
||||
bpp.setAsyncAnnotationType(customAsyncAnnotation); |
||||
} |
||||
|
||||
if (this.executor != null) { |
||||
bpp.setExecutor(this.executor); |
||||
} |
||||
|
||||
bpp.setProxyTargetClass((Boolean) enableAsync.get("proxyTargetClass")); |
||||
|
||||
bpp.setOrder(((Integer) enableAsync.get("order"))); |
||||
|
||||
return bpp; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,211 @@
@@ -0,0 +1,211 @@
|
||||
/* |
||||
* Copyright 2002-2011 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.scheduling.annotation; |
||||
|
||||
import static org.hamcrest.CoreMatchers.is; |
||||
import static org.hamcrest.Matchers.startsWith; |
||||
import static org.junit.Assert.assertThat; |
||||
import static org.junit.Assert.assertTrue; |
||||
|
||||
import java.lang.annotation.ElementType; |
||||
import java.lang.annotation.Retention; |
||||
import java.lang.annotation.RetentionPolicy; |
||||
import java.lang.annotation.Target; |
||||
import java.util.concurrent.Executor; |
||||
|
||||
import org.junit.Test; |
||||
import org.springframework.aop.Advisor; |
||||
import org.springframework.aop.framework.Advised; |
||||
import org.springframework.aop.support.AopUtils; |
||||
import org.springframework.beans.factory.BeanDefinitionStoreException; |
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.context.config.AdviceMode; |
||||
import org.springframework.core.Ordered; |
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
||||
|
||||
/** |
||||
* Tests use of @EnableAsync on @Configuration classes. |
||||
* |
||||
* @author Chris Beams |
||||
* @since 3.1 |
||||
*/ |
||||
public class EnableAsyncTests { |
||||
|
||||
@Test |
||||
public void proxyingOccurs() { |
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
||||
ctx.register(AsyncConfig.class); |
||||
ctx.refresh(); |
||||
|
||||
AsyncBean asyncBean = ctx.getBean(AsyncBean.class); |
||||
assertThat(AopUtils.isAopProxy(asyncBean), is(true)); |
||||
asyncBean.work(); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
@EnableAsync |
||||
static class AsyncConfig { |
||||
@Bean |
||||
public AsyncBean asyncBean() { |
||||
return new AsyncBean(); |
||||
} |
||||
} |
||||
|
||||
|
||||
static class AsyncBean { |
||||
private Thread threadOfExecution; |
||||
|
||||
@Async |
||||
public void work() { |
||||
this.threadOfExecution = Thread.currentThread(); |
||||
} |
||||
|
||||
public Thread getThreadOfExecution() { |
||||
return threadOfExecution; |
||||
} |
||||
} |
||||
|
||||
|
||||
@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) |
||||
static class CustomAsyncAnnotationConfig { |
||||
@Bean |
||||
public CustomAsyncBean asyncBean() { |
||||
return new CustomAsyncBean(); |
||||
} |
||||
} |
||||
|
||||
|
||||
@Target(ElementType.METHOD) |
||||
@Retention(RetentionPolicy.RUNTIME) |
||||
@interface CustomAsync { |
||||
} |
||||
|
||||
|
||||
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(mode=AdviceMode.ASPECTJ) |
||||
static class AspectJAsyncAnnotationConfig { |
||||
@Bean |
||||
public AsyncBean asyncBean() { |
||||
return new AsyncBean(); |
||||
} |
||||
} |
||||
|
||||
|
||||
@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); |
||||
ctx.close(); |
||||
assertThat(asyncBean.getThreadOfExecution().getName(), startsWith("Custom-")); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
@EnableAsync |
||||
static class CustomExecutorAsyncConfig implements AsyncConfigurer { |
||||
@Bean |
||||
public AsyncBean asyncBean() { |
||||
return new AsyncBean(); |
||||
} |
||||
|
||||
public Executor getExecutor() { |
||||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); |
||||
executor.setThreadNamePrefix("Custom-"); |
||||
executor.initialize(); |
||||
return executor; |
||||
} |
||||
|
||||
} |
||||
} |
Loading…
Reference in new issue