Use spring timed tasks and monitor task execution through AOP

Source: Internet
Author: User

This article is about the implementation of task scheduling through spring annotations. As long as the Spring-context package is introduced, task scheduling can be used in the project annotated way.

See below for specific configuration
The schema of the task needs to be added to the spring configuration file.

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"

Then, when enable annotation support

<task:annotation-driven />

It can then be used directly in the code, and the method to be executed periodically must be void and without any arguments.

  @Override   @Scheduled  (cron=" 0 0 2 * *? ")  @Transactional     (Rollbackfor=exception.class) public  void  excute  () throws  dxpexception  

Cron expression Please ask Baidu yourself, here are just a few examples from the Internet to find
Cron expression meaning
"0 0 12 * *?" trigger 12 o'clock noon every day.
"0 15 10?" * * "trigger 10:15 every day"
"0 15 10 * *?" Daily 10:15 Trigger
"0 15 10 * *?" * "10:15 per day" trigger
"0 15 10 * *?" 2005 "2005-year daily 10:15 Trigger
"0 * 14 * *?" daily from 2 o'clock in the afternoon to 2:59 per minute trigger
"0 0/5 14 * *?" daily from 2 o'clock in the afternoon to 2:55 minutes end every 5 minute trigger
"0 0/5 14,18 * *?" daily from 2 o'clock in the afternoon to 2:55 and 6 to 6:55 every 5 minutes.
"0 0-5 14 * *?" daily from 14:00 to 14:05 per minute trigger
"0 10,44 14?" 3 WED "March of 14:10 and 14:44 triggers per Wednesday
"0 15 10?" * MON-FRI "10:15 triggers per Monday, Tuesday, Wednesday, Thursday, Friday

The implementation of the method is monitored through AOP and saved to the database

Packagecom. 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;Importcom. Tiamaes. Gjds. DXP. Annotation. Task;Importcom. Tiamaes. Gjds. DXP. Bean. Tbscheduledexcutelog;Importcom. Tiamaes. Gjds. DXP. Repository. Tbscheduledexcutelogrepository;Importcom. Tiamaes. Gjds. DXP. Task. Dxpscheduled;/** * <p> Description: Monitor the implementation of the method through AOP and save it to the database </p> * <p> Creator: Wang Cheng </p> * <p> created: February 28, 2015 AM 9: 40:18 </p> * <p> copyright note:? Tiamaes </p> * *@Aspectpublic class Scheduledstatisticshandler {@Autowired private tbscheduledexcutelogrepository Tbscheduledexcute Logrepository;@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;}}

Tasks that occur when an exception is performed through AOP Records

Packagecom. 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;Importcom. Tiamaes. Gjds. DXP. Annotation. Task;Importcom. Tiamaes. Gjds. DXP. Bean. Tbscheduledexcutelog;Importcom. Tiamaes. Gjds. DXP. DAO. Tbscheduledexcutelogdao;Importcom. Tiamaes. Gjds. DXP. Exception. Dxpexception;Importcom. Tiamaes. Gjds. DXP. Task. Dxpscheduled;Importcom. Tiamaes. Gjds. Util. Exceptiontools;/** * <p> class Description: Handling the exception of a normal injury during task execution </p> * <p> Creator: Wang Cheng </p> * <p> created: February 28, 2015 PM 4:24:54 < ;/p> * <p> Copyright note:? Tiamaes </p> * *@Aspectpublic 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();}    }}

Where task annotations are the name of the callout task and are easy to save in the database

Use spring timed tasks and monitor task execution through AOP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.