I use this code to run Spring tasks:
@Scheduled(fixedRate = 90000)
public void myScheduler() throws Exception {
ZonedDateTime zonedDateTime = ZonedDateTime.now(zone);
DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a");
String time = zonedDateTime.format(format);
System.out.printf("Scheduler exectuted (%s, (%s))\n", time, zone);
TaskLogs task = new TaskLogs();
task.setStatus("completed");
task.setCreatedAt(LocalDateTime.now());
task.setLog("Executing Notification Job");
task.setTask_name("Executing Notification Job at " + time + " (" + zone + ")");
taskLogsService.save(task);
}
But sometimes I get SQL error. What is the best way to intercept errors? Should I use classical try-catch block or there is a listener for the task?