Right now I have one bean with a @Scheduled method working fine; it’s declared in my applicationContext.xml.
<!-- some JPA stuff -->
<bean id="aWorkingBean" class="some.package.WorkingBean">
<property name="someDAO" ref="someDAO" />
</bean>
<task:annotation-driven scheduler="myScheduler" />
<task:scheduler id="myScheduler" pool-size="10" />
What I’m trying to do is programmatically schedule another method (e.g. loading some annotated class and inject its dependencies) upon request. Something like:
WebApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(NonWorkingBean.class);
// add DAO references...
ctx.registerBeanDefinition("nonWorkingBean", builder.getBeanDefinition()); // <-- this doesn't work
Obviously it doesn’t work because the XmlWebApplicationContext is read-only and has no registerBeanDefinition method. Is there any other way to achieve this?
I’m using Tomcat 6.0.29 and Spring 3.0.4
<task:scheduler>and@Scheduledis really just a convenience approach to scheduling static tasks. It’s not really suitable for dynamic scheduling. Yes, you can make it work, but it’s going to be awkward.When you put
<task:scheduler id="myScheduler">into your config, Spring creates aTaskSchedulerbean calledmyScheduler. This can be injected into your own beans, and can be invoked programmatically in order to schedule new tasks. You’ll need to create aRunnableto pass to theTaskScheduler, but that should be simple enough.