使用Spring定时任务并且通过AOP监控任务执行情况
原文:http://www.open-open.com/code/view/1426250803279
本文讲的是通过Spring注解的方式实现任务调度。只要引入了spring-context包就能够在项目中使用注解方式的任务调度。
下面看具体配置
需要在Spring配置文件中加入task的schema。
<xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd"
然后在启用注解支持
<task:annotation-driven />
然后在代码中就可以直接用了,要定时执行的方法必须是void的,并且没有任何参数的。
@Override
@Scheduled(cron="0 0 2 * * ?")
@Transactional(rollbackFor=Exception.class)
public void excute() throws DXPException
cron表达式请自行问百度,下面只列出几个从网上找的例子
CRON表达式 含义
“0 0 12 * * ?” 每天中午十二点触发
“0 15 10 ? * *” 每天早上10:15触发
“0 15 10 * * ?” 每天早上10:15触发
“0 15 10 * * ? *” 每天早上10:15触发
“0 15 10 * * ? 2005” 2005年的每天早上10:15触发
“0 * 14 * * ?” 每天从下午2点开始到2点59分每分钟一次触发
“0 0/5 14 * * ?” 每天从下午2点开始到2:55分结束每5分钟一次触发
“0 0/5 14,18 * * ?” 每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发
“0 0-5 14 * * ?” 每天14:00至14:05每分钟一次触发
“0 10,44 14 ? 3 WED” 三月的每周三的14:10和14:44触发
“0 15 10 ? * MON-FRI” 每个周一、周二、周三、周四、周五的10:15触发
通过AOP监控方法的执行情况,并保存到数据库中
package com.tiamaes.gjds.dxp.aop; import java.util.Date; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired; import com.tiamaes.gjds.dxp.annotation.Task;
import com.tiamaes.gjds.dxp.bean.TbScheduledExcuteLog;
import com.tiamaes.gjds.dxp.repository.TbScheduledExcuteLogRepository;
import com.tiamaes.gjds.dxp.task.DxpScheduled; /**
* <p>类描述:通过AOP监控方法的执行情况,并保存到数据库中</p>
* <p>创建人:王成委 </p>
* <p>创建时间:2015年2月28日 上午9:40:18 </p>
* <p>版权说明: © 2015 Tiamaes </p>
*/
@Aspect
public class ScheduledStatisticsHandler { @Autowired
private TbScheduledExcuteLogRepository tbScheduledExcuteLogRepository; @Pointcut("@annotation(org.springframework.scheduling.annotation.Scheduled)")
public void proxyAspect() { } @Around("proxyAspect()")
public Object doInvoke(ProceedingJoinPoint joinPoint) throws Throwable{
Date date = new Date();
long start = System.currentTimeMillis();
Object result = joinPoint.proceed();
long end = System.currentTimeMillis();
Object target = joinPoint.getTarget(); TbScheduledExcuteLog log = new TbScheduledExcuteLog();
log.setClassName(joinPoint.getTarget().getClass().getName());
log.setConsum(end-start);
log.setExcuteDate(date);
log.setExcuteTime(date);
log.setIsError(false);
if (target instanceof DxpScheduled) {
DxpScheduled scheduled = (DxpScheduled) target;
Task task = scheduled.getClass().getAnnotation(Task.class);
log.setContentName(task.value());
log.setRemark(scheduled.getTaskExcuteInfo());
log.setGetRecCount(scheduled.getRemoteCount());
log.setSyncRecCount(scheduled.getSyncCount());
} this.tbScheduledExcuteLogRepository.save(log); return result;
}
}
通过AOP记录执行时发生异常的任务
package com.tiamaes.gjds.dxp.aop; import java.util.Date; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired; import com.tiamaes.gjds.dxp.annotation.Task;
import com.tiamaes.gjds.dxp.bean.TbScheduledExcuteLog;
import com.tiamaes.gjds.dxp.dao.TbScheduledExcuteLogDao;
import com.tiamaes.gjds.dxp.exception.DXPException;
import com.tiamaes.gjds.dxp.task.DxpScheduled;
import com.tiamaes.gjds.util.ExceptionTools; /**
* <p>类描述: 处理任务执行时法伤的异常 </p>
* <p>创建人:王成委 </p>
* <p>创建时间:2015年2月28日 下午4:24:54 </p>
* <p>版权说明: © 2015 Tiamaes </p>
*/
@Aspect
public class ScheduleExceptionHandler { @Autowired
private TbScheduledExcuteLogDao tbScheduledExcuteLogDao; @Pointcut("@annotation(org.springframework.scheduling.annotation.Scheduled)")
public void proxyAspect() { } @AfterThrowing(pointcut="proxyAspect()",throwing="ex")
public void doException(JoinPoint joinPoint,Exception ex){
Object target = joinPoint.getTarget();
this.saveException(target, ex);
} public void saveException(Object target,Exception ex){
try {
Date date = new Date();
TbScheduledExcuteLog log = new TbScheduledExcuteLog();
log.setClassName(target.getClass().getName());
log.setExcuteDate(date);
log.setExcuteTime(date);
log.setIsError(true);
log.setErrorInfo(ExceptionTools.getExceptionDetails(ex).toString());
if (target instanceof DxpScheduled) {
DxpScheduled scheduled = (DxpScheduled) target;
Task task = scheduled.getClass().getAnnotation(Task.class);
log.setContentName(task.value());
}
this.tbScheduledExcuteLogDao.saveAndCommit(log);
} catch (DXPException e) {
e.printStackTrace();
}
}
}
其中Task注解为标注任务的名称,方便在数据库中保存。
使用Spring定时任务并且通过AOP监控任务执行情况的更多相关文章
- spring定时任务配置,以及不执行的解决办法
前几天,同事问了我一个问题,我告诉他用spring的定时任务解决,并给他配置了spring的定时任务.当时随便找了一个bean写了一段代码,验证定时任务正确执行后,就没再管,昨天下午,同事写代码的时候 ...
- ASP.NET 多线程 监控任务执行情况,并显示进度条
关于多线程的基本概念和知识在本文中不多讲,而且我懂的也不是很透,说的太多误人子弟...对于我来说,做本文提到的功能够用就行,等实现其他效果不够用的时候,再深入研究 推荐看园子里的两篇博客应该就有个基本 ...
- Spring整合Quartz定时任务执行2次,Spring定时任务执行2次
Spring整合Quartz定时任务执行2次,Spring定时任务执行2次 >>>>>>>>>>>>>>>&g ...
- 解决spring定时任务执行2次和tomcat部署缓慢的问题
spring定时任务执行2次 问题重现和解析 最近使用quartz定时任务框架,结果发现开发环境执行无任何问题,部署到服务器上后,发现同一时间任务执行了多次.经过搜索发现是服务器上tomcat的配置文 ...
- spring多个AOP执行先后顺序(面试问题:怎么控制多个aop的执行循序)
转载:spring多个AOP执行先后顺序(面试问题:怎么控制多个aop的执行循序) 众所周知,spring声明式事务是基于AOP实现的,那么,如果我们在同一个方法自定义多个AOP,我们如何指定他们的执 ...
- spring定时任务执行两次的原因与解决方法
spring定时任务,本地执行一次,放到服务器上后,每次执行时会执行两次,原因及解决办法. http://blog.csdn.net/yaobengen/article/details/7031266 ...
- 如何在Spring Boot 中动态设定与执行定时任务
本篇文章的目的是记录并实现在Spring Boot中,动态设定与执行定时任务. 我的开发项目是 Maven 项目,所以首先需要在 pom.xml 文件中加入相关的依赖.依赖代码如下所示: <de ...
- Spring Aop的执行顺序
Spring Aop的执行顺序 首先回忆一下 AOP 的常用注解 @Before:前置通知:目标方法之前执行 @After:后置通知:目标方法之后执行 @AfterReturning:返回后通知:执行 ...
- Spring Boot 创建定时任务(配合数据库动态执行)
序言:创建定时任务非常简单,主要有两种创建方式:一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer). 前者相信大家都很熟悉,但是实际使用中我们往往想从数据库 ...
随机推荐
- SQLite与MySQL、SQLServer等异构数据库之间的数据同步
SQLite DBSync是开源嵌入式数据库SQLite的数据同步引擎,实现了SQLite与SQLite数据库之间以及SQLite与异构数据库(Oracle.MySQL.SQLServer)之间的增量 ...
- HashMap详解 基于jdk1.7
转载自:http://zhangshixi.iteye.com/blog/672697 1. HashMap概述: HashMap是基于哈希表的Map接口的非同步实现.此实现提供所有可选的映射操 ...
- UGUI世界坐标转换为UI本地坐标
以下是实现hud跟随3D物体的脚本,只是测试用,不是开发中的代码,脚本挂在任意游戏物体上 demo下载 using UnityEngine; public class SceneFollowUI : ...
- edquota - 编辑用户配额
SYNOPSIS(总览) edquota [ -p proto-username ] [ -u | -g ] username... edquota [ -u | -g ] -t DESCRIPTIO ...
- DROP CONVERSION - 删除一个用户定义的编码转换
SYNOPSIS DROP CONVERSION name [ CASCADE | RESTRICT ] DESCRIPTION 描述 DROP CONVERSION 删除一个以前定义的编码转换. 要 ...
- DLL动态链接库的创建
dll的创建主要有两种方法:一是使用 __declspec(dllexport) 创建dll,二是使用模块定义(.def)文件创建dll. 使用 __declspec(dllexport) 创建dll ...
- C-基础:数组名与取地址符&
指出下面代码的输出,并解释为什么.(不错,对地址掌握的深入挖潜) main() { ]={,,,,}; ); printf(),*(ptr-)); } 输出:2,5 *(a+1)就是a[1], ...
- 记一次Linux系统被入侵的过程
记一次Linux系统被入侵的过程 1. 前期现象 前期现象,宋组那边反应开发环境192.161.14.98这台机器通过公网下载文件,很慢,ping百度丢包严重.因为这台机器是通过楼下adsl拨号上网, ...
- node.js从入门到放弃(一)
以下内容全是我个人理解写出,如有不对,请立刻练习本人进行更改.以免被刚入门的被我带入坑里. —node是什么?我想大家应该都知道. node是前端未来干掉后端的一种语言,是用JavaScript来编写 ...
- FastNet C++/Python 网络通信库之 协议
协议可以使用的基础数据类型: UInt8,UInt16,UInt32,UInt64Int8,Int16,Int32,Int64Float,Double,Bool,String [T] 数组,T代表元 ...