java 任务监控_Java Spring注解任务调度并实现AOP监控任务执行情况

本文讲的是通过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"

然后在启用注解支持

然后在代码中就可以直接用了,要定时执行的方法必须是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;

/**

*

类描述:通过AOP监控方法的执行情况,并保存到数据库中

*

创建人:王成委

*

创建时间:2015年2月28日 上午9:40:18

*

版权说明: © 2015 Tiamaes

*/

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

/**

*

类描述: 处理任务执行时法伤的异常

*

创建人:王成委

*

创建时间:2015年2月28日 下午4:24:54

*

版权说明: © 2015 Tiamaes

*/

@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注解为标注任务的名称,方便在数据库中保存。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值