Chillax
Chillax

Reputation: 4698

Spring @Scheduled cron details from property file - Exception

I was trying to define the cron details in my spring @Scheduled method

@Service
@PropertySource("classpath:application.properties")
public class CacheRefreshService {

@Scheduled(cron = "${api.refresh.cron}")
     public void refreshJob() throws Exception {
        LOGGER.info("Started Refresh");
        //do something
     }
}

And in my application.properties

#Refresh
api.refresh.cron =0 29 11 * * ?

When I define the cron details along with @Scheduled, it is running fine. But when I do this, it is not able to read the value from the properties file and the below error is thrown.

Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'refreshJob': Cron expression must consist of 6 fields (found 1 in "${api.refresh.cron}")

Any suggestions please?

Upvotes: 4

Views: 10378

Answers (2)

Purushottam
Purushottam

Reputation: 148

factoryBean.setCronExpression("0 0/1 * 1/1 * ? *"); 

you have to set Cron Expresssion bcz in BeanFactory Class the setCronExpression is mandatory

Upvotes: 1

Chillax
Chillax

Reputation: 4698

Adding the below to my ApplicationContext resolved the issue..

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
       return new PropertySourcesPlaceholderConfigurer();
    }

Upvotes: 2

Related Questions