使用Spring定时任务并且通过AOP监控任务执行情况
发布时间:2020-05-24 18:32:58 所属栏目:Java 来源:互联网
导读:使用Spring定时任务并且通过AOP监控任务执行情况
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 <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 /> @Override @Scheduled(cron="0 0 2 * * ?") @Transactional(rollbackFor=Exception.class) public void excute() throws DXPException 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;
}
}
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();
}
}
}
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
