Spring @Scheduled: problems in getting cron expression from properties file
Hi all
First of all I wish you and your beloved all the best for this Christmass and a wonderful happy new year
Now...In my project I'm using spring 3.1.1 and I need to execute some scheduled jobs
I used spring-batch for the job stuffs then I used the spring's @Scheduled annotation in order to execute them and all works pretty good
Next step is that I want to configure the @Scheduled cron expression in a properties file and I'm having some problems
The project is composed by several sub-modules; each sub-module is a separate maven artifact (and, obviously, a separate eclipse project)
So in my case I have a sub-module where I configure the propertyplaceholder (taking inspiration by this link http://forum.springsource.org/showth...ron-expression)
Now in this sub-module I have this configuration:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:batch="http://www.springframework.org/schema/batch"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch.xsd">
<context:annotation-config />
<tx:annotation-driven transaction-manager="jtaTransactionManager" />
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name="ignoreResourceNotFound" value="false"/>
<property name="locations">
<list>
<value>classpath:subModuleASchduledCron.properties</value>
</list>
</property>
</bean>
<aop:aspectj-autoproxy />
</beans>
In another project i have a configuration file with this content:
Code:
scheduledCronExpre=15 0 0 * * *
In my class I have:
Code:
@Scheduled(cron = "${scheduledCronExpre}")
public void doJob(){
//do my stuffs
}
Well when I try to execute this code I get this error:
Code:
Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'scheduledCronExpre'
The full stacktrace is:
Code:
09:52:51,653 ERROR [StartMktIntelligenceBatches] Errore: Error creating bean with name 'myScheduledBean' defined in file [/dati/workspaces/indigo/SubModuleA/target/classes/it/schedulers/MyScheduledBean.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'scheduledCronExpre'
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myScheduledBean' defined in file [/dati/workspaces/indigo/SubModuleA/target/classes/it/schedulers/MyScheduledBean.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'scheduledCronExpre'
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
at it.poste.crs.invimall.marketing.intelligent.mktintelligence.batch.main.StartMktIntelligenceBatches.main(StartMktIntelligenceBatches.java:26)
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'scheduledCronExpre'
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:173)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:125)
at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer$PlaceholderResolvingStringValueResolver.resolveStringValue(PropertyPlaceholderConfigurer.java:255)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:748)
at org.springframework.context.support.ApplicationContextAwareProcessor$EmbeddedValueResolver.resolveStringValue(ApplicationContextAwareProcessor.java:136)
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor$1.doWith(ScheduledAnnotationBeanPostProcessor.java:145)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:473)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:451)
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(ScheduledAnnotationBeanPostProcessor.java:114)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:407)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1461)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
... 11 more
By debugging I see that, when the spring context is loaded, the properties file is read by Spring....but then when I try to use a property of this property file the properties stored by the spring context seem to be null....
I tried to search in all my projects if there was another declaration of PropertyPlaceholderConfigurer but I didn't find it...and this problem is killing me...
Do you have any idead on how I can solve this issue?
Thank you
Angelo