The <task:annotation-driven/> element now registers the post-processor for @Scheduled in addition to the already existing @Async support. Both "scheduler" and "executor" attributes are available.
This commit is contained in:
parent
c218b6c6c7
commit
df6ba69bc5
|
|
@ -42,6 +42,12 @@ public class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParse
|
|||
public static final String ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME =
|
||||
"org.springframework.scheduling.annotation.internalAsyncAnnotationProcessor";
|
||||
|
||||
/**
|
||||
* The bean name of the internally managed scheduled annotation processor.
|
||||
*/
|
||||
public static final String SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME =
|
||||
"org.springframework.scheduling.annotation.internalScheduledAnnotationProcessor";
|
||||
|
||||
|
||||
public BeanDefinition parse(Element element, ParserContext parserContext) {
|
||||
Object source = parserContext.extractSource(element);
|
||||
|
|
@ -64,8 +70,21 @@ public class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParse
|
|||
if (StringUtils.hasText(executor)) {
|
||||
builder.addPropertyReference("executor", executor);
|
||||
}
|
||||
BeanDefinitionHolder holder = registerPostProcessor(registry, builder, ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME);
|
||||
parserContext.registerComponent(new BeanComponentDefinition(holder));
|
||||
registerPostProcessor(parserContext, builder, ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME);
|
||||
}
|
||||
if (registry.containsBeanDefinition(SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
|
||||
parserContext.getReaderContext().error(
|
||||
"Only one ScheduledAnnotationBeanPostProcessor may exist within the context.", source);
|
||||
}
|
||||
else {
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(
|
||||
"org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor");
|
||||
builder.getRawBeanDefinition().setSource(source);
|
||||
String scheduler = element.getAttribute("scheduler");
|
||||
if (StringUtils.hasText(scheduler)) {
|
||||
builder.addPropertyReference("scheduler", scheduler);
|
||||
}
|
||||
registerPostProcessor(parserContext, builder, SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME);
|
||||
}
|
||||
|
||||
// Finally register the composite component.
|
||||
|
|
@ -74,12 +93,13 @@ public class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParse
|
|||
return null;
|
||||
}
|
||||
|
||||
private static BeanDefinitionHolder registerPostProcessor(
|
||||
BeanDefinitionRegistry registry, BeanDefinitionBuilder builder, String beanName) {
|
||||
private static void registerPostProcessor(
|
||||
ParserContext parserContext, BeanDefinitionBuilder builder, String beanName) {
|
||||
|
||||
builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
|
||||
registry.registerBeanDefinition(beanName, builder.getBeanDefinition());
|
||||
return new BeanDefinitionHolder(builder.getBeanDefinition(), beanName);
|
||||
parserContext.getRegistry().registerBeanDefinition(beanName, builder.getBeanDefinition());
|
||||
BeanDefinitionHolder holder = new BeanDefinitionHolder(builder.getBeanDefinition(), beanName);
|
||||
parserContext.registerComponent(new BeanComponentDefinition(holder));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,6 +34,16 @@
|
|||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="scheduler" type="xsd:string" use="optional">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
Specifies the org.springframework.scheduling.TaskScheduler or
|
||||
java.util.ScheduledExecutorService instance to use when invoking scheduled
|
||||
methods. If no reference is provided, a TaskScheduler backed by a single
|
||||
thread scheduled executor will be used.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* Copyright 2002-2009 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.config;
|
||||
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class AnnotationDrivenBeanDefinitionParserTests {
|
||||
|
||||
private ApplicationContext context;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.context = new ClassPathXmlApplicationContext(
|
||||
"annotationDrivenContext.xml", AnnotationDrivenBeanDefinitionParserTests.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asyncPostProcessorRegistered() {
|
||||
assertTrue(context.containsBean(
|
||||
AnnotationDrivenBeanDefinitionParser.ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void scheduledPostProcessorRegistered() {
|
||||
assertTrue(context.containsBean(
|
||||
AnnotationDrivenBeanDefinitionParser.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asyncPostProcessorExecutorReference() {
|
||||
Object executor = context.getBean("testExecutor");
|
||||
Object postProcessor = context.getBean(AnnotationDrivenBeanDefinitionParser.ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME);
|
||||
assertSame(executor, new DirectFieldAccessor(postProcessor).getPropertyValue("executor"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void scheduledPostProcessorSchedulerReference() {
|
||||
Object scheduler = context.getBean("testScheduler");
|
||||
Object postProcessor = context.getBean(AnnotationDrivenBeanDefinitionParser.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME);
|
||||
assertSame(scheduler, new DirectFieldAccessor(postProcessor).getPropertyValue("scheduler"));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:task="http://www.springframework.org/schema/task"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/task
|
||||
http://www.springframework.org/schema/task/spring-task.xsd">
|
||||
|
||||
<task:annotation-driven executor="testExecutor" scheduler="testScheduler"/>
|
||||
|
||||
<task:executor id="testExecutor"/>
|
||||
|
||||
<task:scheduler id="testScheduler"/>
|
||||
|
||||
</beans>
|
||||
Loading…
Reference in New Issue