I have a job which I am scheduling, with the cron value in a jpa repository. When getCronValue() is called in @Scheduled(cron="#{@getCronValue}") , it successfully returns the cron value from the database. When the application is live and I update the value in the repository, it does correctly print the value I have updated it to in the logger I have below.
@Scheduled(cron="#{@getCronValue}")
public void testCronJob()
{
logger.info("CRON EXPRESSION: " + getCronValue());
}
The issue is that while it is returning the correct value, the schedule is not actually changing. Once the application is running, if the value was originally 1 seconds, and I change the schedule to every 10 seconds, it continues to run every 1 seconds, while printing the new value of 10 seconds which is now in the repository.
Example output:
Application starts
1 second passes
CRON EXPRESSION: * * * * * *
Now I manually go into the repository and change the cron expression to once every 10 seconds
1 second passes
CRON EXPRESSION: */10 * * * * *
1 second passes
CRON EXPRESSION: */10 * * * * *
Here is the rest of the job class for reference. I was investigating if @RefreshScope would help but I lack the understanding, and the research I've found tends to be referencing local property files, which this is avoiding since the attribute is in a repository.
@RefreshScope
@Component
@ConditionalOnProperty("cron.config.job.enabled")
public class CronConfigJob
{
ServiceA service;
@Autowired
public CronConfigJob(ServiceA service)
{
this.service = service;
}
@Autowired
private RepositoryA repository;
@Bean()
public String getCronValue()
{
String result = repository.findByJobName("CronConfigJob").getJobCronExpression();
return result;
}
@Scheduled(cron="#{@getCronValue}")
public void testCronJob()
{
logger.info("CRON EXPRESSION: " + getCronValue());
}