2

I am using cron expression for spring scheduler and the value of expression is dynamically provided by the spring bean using spel.

@Autowired
 private PurgeProperties purgeProperties;

@Scheduled(cron = "#{@purgeProperties.cronExpression}", zone = "#{@purgeProperties.zone}")
public void purgeData() throws UnknownHostException
{
           startPurge();
}

Everything is working properly only thing is I want to log when scheduler will trigger because cron expression is provided by another bean at runtime. So just want to know whether the correct expression is mapped what is been provided through file to property bean.

pvjhs
  • 549
  • 1
  • 9
  • 24
  • so log in inside the method purgeData, and log the expression , what is the issue ? – surya Apr 08 '18 at 16:46
  • https://stackoverflow.com/a/10938322/3003337 – surya Apr 08 '18 at 16:57
  • https://plumbr.io/blog/monitoring/monitoring-scheduled-jobs – surya Apr 08 '18 at 17:14
  • i dont want to log in purgeData as it will get logged when it will run. What i want is to log the time scheduler will run (local time zone based on cron provided) during the start up of the web app . something like in @postConstruct – pvjhs Apr 08 '18 at 18:52

2 Answers2

1

Try following code

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.support.CronSequenceGenerator;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;

''''
''''
@PostConstruct
public void init() {
    CronSequenceGenerator cronTrigger = new CronSequenceGenerator(purgeProperties.cronExpression);
    Date next = cronTrigger.next(new Date());

    System.out.println("Next Execution Time: " + next);
}
surya
  • 2,581
  • 3
  • 22
  • 34
0

CronSequenceGenerator is deprecetated now. I have used this:

CronExpression cronTrigger = CronExpression.parse(cronExpression);
var next = cronTrigger.next(LocalDateTime.now());
log.info("Next scheduled start planed at {}", next);
walter33
  • 776
  • 5
  • 12