使用Spring定時任務並且通過AOP監控任務執行情況
阿新 • • 發佈:2019-02-20
本文講的是通過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();
}
}
}