Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将异常捕获到Spring作业中_Java_Spring_Spring Boot - Fatal编程技术网

Java 将异常捕获到Spring作业中

Java 将异常捕获到Spring作业中,java,spring,spring-boot,Java,Spring,Spring Boot,我使用以下代码运行Spring任务: @Scheduled(fixedRate = 90000) public void myScheduler() throws Exception { ZonedDateTime zonedDateTime = ZonedDateTime.now(zone); DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a");

我使用以下代码运行Spring任务:

@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);
    }

但有时我会遇到SQL错误。拦截错误的最佳方法是什么?我应该使用经典的try-catch块还是任务有侦听器?

我建议在运行@Schedule时最好使用try-catch和SQLException,可能您不想破坏它

@Scheduled(fixedRate = 90000)
public void myScheduler() throws SQLException {

 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 + ")");
 try {
  taskLogsService.save(task);
 } catch (SQLException sqle){
 System.out.println(sqle);
 //or you can use slf4j logger to record same in logfile
 }
}

是的,您应该使用try-catch块来获取异常,这将提供有关SQL错误的更多信息。这是典型的编码风格。
try{}catch(SQLException exception){}
使用try-catch,作为开发人员,您可以更好地控制它。或者查看如何配置TaskScheduler并在其上设置ErrorHandler,但当我遇到异常时,我希望使用SQL查询记录它。如何使用注释来实现这一点?我可以将其作为对象发送吗?在定义抛出异常的方法时,最好避免抛出
Exception
,并尝试抛出更具体的东西,如
SQLException
,这将改进调用代码,因为它不必捕获和处理
Exception
的每个子类