ScheduledExecutorFactoryBean忽略异常继续执行

程序中有一个定时任务,每10分钟把满足条件的任务从一个表迁移到另一张表,程序启动的时候数据库异常了一段时间,之后数据库恢复了。但是通过观察,发现此定时任务挂掉了,再也没有重启起来。

解决此问题的办法是要在任务线程的run方法中中捕获runtime异常,如果使用ScheduledExecutorFactoryBean,只要配置continueScheduledExecutionAfterException属性为true即可。Spring文档描述的很清楚:

setsup a JDK 1.5 ScheduledExecutorService (bydefault: ScheduledThreadPoolExecutor asimplementation) and exposes it for bean references.

Allowsfor registration of ScheduledExecutorTasks,automatically starting the ScheduledExecutorService oninitialization and cancelling it on destruction of the context. Inscenarios that just require static registration of tasks at startup,there is no need to access the ScheduledExecutorService instanceitself in application code.

Notethat ScheduledExecutorService usesa Runnable instancethat is shared between repeated executions, in contrast to Quartzwhich instantiates a new Job for each execution.

WARNING: Runnables submittedvia a native ScheduledExecutorService areremoved from the execution schedule once they throw an exception. Ifyou would prefer to continue execution after such an exception,switch thisFactoryBean's"continueScheduledExecutionAfterException" propertyto "true

对于定时任务,java原生支持有Timer,和ScheduledExecutor,前文中已经有介绍,参见:

http://blog.csdn.net/xiaojianpitt/article/details/7659422

如果使用spring的话,可以使用org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean,完全超越ScheduledExecutor。

ScheduledExecutorFactoryBean使用ScheduledThreadPoolExecutor作为内部实现,定时调用使用ScheduledThreadPoolExecutor的方法.

但是continueScheduledExecutionAfterException如何让异常任务继续运行的呢?代码是最好的说明:我们可以跟踪其源代码来找出:

protected Runnable getRunnableToSchedule(ScheduledExecutorTasktask) {

return this.continueScheduledExecutionAfterException

? new DelegatingErrorHandlingRunnable(task.getRunnable(),TaskUtils.LOG_AND_SUPPRESS_ERROR_HANDLER)

: newDelegatingErrorHandlingRunnable(task.getRunnable(),TaskUtils.LOG_AND_PROPAGATE_ERROR_HANDLER);

}

我们可以看到,如果我们设置了在异常之后继续执行任务,那么在实现中对Runnable进行了封装,封装类为DelegatingErrorHandlingRunnable,我们继续追踪其源代码:

publicclass DelegatingErrorHandlingRunnable implements Runnable{

public void run(){

try{

this.delegate.run();

}catch(Throwable ex){

this.errorHandler.handleError(ex):

}

}

}

注意这个DelegatingErrorHandlingRunnable实现了Runnable接口,作为原始任务的代理,需要注意的是run里面的trycatch 对异常的处理,在这里catch了Throwable异常(具体可见java异常结构),这java异常的基类,包括Error和RuntimeException所有的java异常都被捕获,之后交给了errorHandler处理,ErrorHandler可以继续抛出异常或者不抛出,其中TaskUtils.LOG_AND_SUPPRESS_ERROR_HANDLER只是log打印异常,TaskUtils.LOG_AND_PROPAGATE_ERROR_HANDLER要继续抛出runtimeException。

TiskUtils代码如下:

public static final ErrorHandlerLOG_AND_SUPPRESS_ERROR_HANDLER = newLoggingErrorHandler();

.

static class LoggingErrorHandler implements ErrorHandler {

private final Log logger =LogFactory.getLog(LoggingErrorHandler.class);

public void handleError(Throwable t) {

if (logger.isErrorEnabled()) {

logger.error("Unexpected error occurred in scheduledtask.", t);

}

}

}

….

static class PropagatingErrorHandler extends LoggingErrorHandler {

public void handleError(Throwable t) {

super.handleError(t);

ReflectionUtils.rethrowRuntimeException(t);

}

}

ReflectionUtils代码如下:

public static void rethrowRuntimeException(Throwable ex) {

if (ex instanceof RuntimeException) {

throw (RuntimeException) ex;

}

if (ex instanceof Error) {

throw (Error) ex;

}

handleUnexpectedException(ex);

}

ScheduledExecutorFactoryBean忽略异常继续执行的更多相关文章

  1. Effective Java 第三版——77. 不要忽略异常

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...

  2. 异常依然执行{try..catch语句块..}的后续代码

    测试异常依然执行{try..catch语句块..}的后续代码: private static Integer testThrows() throws Exception{ Integer result ...

  3. 第二讲shiro异常及执行流程

    在认证过程中,有一个父异常为:AuthenticationException 该异常有几个子类,分别对应不同的异常情况: (1)DisabledAccountException:账户失效异常 (2)E ...

  4. python 如何跳过异常继续执行

    使用try...except...语句,类似于if...else...,可以跳过异常继续执行程序,这是Python的优势 用法如下: 1 2 3 4 5 6 try:            # 可能会 ...

  5. promise抛异常,执行队列

    //promise抛出异常 new Promise((resolve,reject)=>{ resolve("成功") }).then(res=>{ if(res != ...

  6. YAML_07 有报错信息,告诉你错误忽略,继续执行下面的命令

    ansible]# vim user5.yml --- - hosts: cache   remote_user: root   vars:     user: bb   tasks:    - sh ...

  7. PHPDocumentor2.8.5 安装,使用及快速上手

    PHPDocumentor当前版本是phpDocumentor-2.8.5.tgz 关于PHPDocumentor有什么用,还有其历史,我就不介绍了,直接进入正题.老版本的叫PHPDoc,从1.0开始 ...

  8. java实训 :异常(try-catch执行顺序与自定义异常)

    关键字: try:执行可能产生异常的代码 catch:捕获异常 finally:无论是否发生异常代码总能执行 throws:声明方法可能要抛出的各种异常 throw:手动抛出自定义异常 用 try-c ...

  9. C#WinForm程序异常退出的捕获、继续执行与自动重启

    本文参考网上搜索的信息,并做了适当修改可以让捕捉到异常之后阻止程序退出. 另给出了通过命令行自动重启的方法. 如果一个线程里运行下面的代码 ; / a; 将会导致程序自动结束,而且没有任何提示信息 但 ...

随机推荐

  1. PPTPD/L2TP/IPSec VPN一键安装包 For CentOS 6

    一.一键安装PPTPD VPN 本教程适用于Openv VPS.Xen VPS或者KVM VPS. 1.首先运行如下命令: cat /dev/net/tun 返回的必须是: cat: /dev/net ...

  2. 解决Matlab启动时 " Can't check 9.0 VCRTs The application has failed to start because……" 的错误

    注:转载或引用请注明出处 今天在winserver 2012 r2 上安装matlab 2016b , 安装完成运行时提示: ERROR: Cnn't check 9.0 VCRTs <star ...

  3. [转]关于MYSQL Innodb 锁行还是锁表

    关于mysql的锁行还是锁表,这个问题,今天算是有了一点头绪,mysql 中 innodb是锁行的,但是项目中居然出现了死锁,锁表的情况.为什么呢?先看一下这篇文章. 目时由于业务逻辑的需要,必须对数 ...

  4. Burp Suite教程(英文版)

    In this article, we are going to see another powerful framework that is used widely in pen-testing. ...

  5. Asp.Net中的三种分页方式

    Asp.Net中的三种分页方式 通常分页有3种方法,分别是asp.net自带的数据显示空间如GridView等自带的分页,第三方分页控件如aspnetpager,存储过程分页等. 第一种:使用Grid ...

  6. easyui源码翻译1.32--Tree(树)

    前言 使用$.fn.tree.defaults重写默认值对象.下载该插件翻译源码 树控件在web页面中一个将分层数据以树形结构进行显示.它提供用户展开.折叠.拖拽.编辑和异步加载等功能. 源码 /** ...

  7. comm命令——

    comm命令 :对已经有序的文件进行比较——第一列只在文件1中出现的文件,第二列只在文件2中出现的文件,第三列在文件1和文件2中同事出现的文件 请注意前提条件:             comm对文件 ...

  8. ANDROID_MARS学习笔记_S01原始版_009_下载文件

    一.代码1.xml(1)main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...

  9. 优化 Android 线程和后台任务开发

    在 Android 开发中,你不应该做任何阻碍主线程的事情.但这究竟意味着什么呢?在这次海湾 Android 开发者大会讲座中,Ari Lacenski 认为对于长时间运行或潜在的复杂任务要特别小心. ...

  10. 2013年最受欢迎的16个HTML5 WordPress主题

    本文中,我整理了16个最新.最受欢迎的WordPress主题,它们一致的特征是支持HTML5, 它们秉承最佳设计理念以及最新的流行趋势,将它们归纳与此,希望对喜欢HTML5,准备用WordPress做 ...