[Spring]-Quartz timed task-Annotation

Source: Internet
Author: User

Spring + quartz can be used in Annoation mode:

1, Appjob class:

 Packagecom.my.quartz.testquartz1;Importorg.springframework.scheduling.annotation.Scheduled;Importorg.springframework.stereotype.Component; @Component Public classAppjob {//@Scheduled (fixeddelay=1000)//The first way//Fixeddelay Delay How many milliseconds, how many milliseconds to execute once    /*1 Seconds (0-59) 2 Minutes (0-59) 3 Hours (0-23) 4 day of Month (1-31) 5 month (1- or JAN-DEC) 6 day of week (1-7 or Sun-sat) 7 year (1970-2099) value: Can be a single value, such as 6, or it can be a range, such as 9- 12 can also be a list, such as 9,11,13 can also be any value, using **/    //0 * * * * * represents one time per minute    /*2011-09-07 09:23:00 2011-09-07 09:24:00 2011-09-07 09:25:00 2011-09-07 09:26:00 */@Scheduled (Cron= "0/1 * * * *?")     Public voidExecute () {Longms =System.currenttimemillis ();    System.out.println (MS); }}

The cron notation can be found in:

Http://www.cnblogs.com/HD/p/4205937.html

2. Spring configuration file:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:tx= "Http://www.springframework.org/schema/tx"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:task= "Http://www.springframework.org/schema/task"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd Http://www.springframework.org/schema/tx Http://www.springframework.org/schema/tx/sprin G-tx-3.0.xsd Http://www.springframework.org/schema/context HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/CONTEXT/SPR Ing-context-3.0.xsd Http://www.springframework.org/schema/mvc Http://www.springframework.org/schema/mvc/spri Ng-mvc-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema/aop/spring-ao P-3.0.xsd Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task- 3.0.xsd ">    <!--Annotation Description -    <Context:annotation-config/>    <!--Spring Auto Scan Package -    <Context:component-scanBase-package= "com.my" />        <!--Scheduled Tasks -    <Task:executorID= "Executor"pool-size= "5" />    <Task:schedulerID= "Scheduler"pool-size= "Ten" />    <Task:annotation-drivenExecutor= "Executor"Scheduler= "Scheduler" />    </Beans>

3. Running class:

 Packagecom.my.quartz.testquartz1;ImportOrg.quartz.Scheduler;Importorg.quartz.SchedulerException;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classApp {@SuppressWarnings ("Resource")     Public Static voidMain (string[] args)throwsinterruptedexception, schedulerexception {applicationcontext context=NewClasspathxmlapplicationcontext ("Spring.xml"); System.out.println ("Job-end"); //Scheduler Scheduler = (Scheduler) context.getbean ("Schedulerbean"); //Scheduler.shutdown ();    }}

Operation Result:

Of course, Spring's configuration file can also be written like this:

    <!--Timer Switch -    <Task:annotation-driven/>    <!--Managed Beans -    <BeanID= "Appjob"class= "COM.MY.QUARTZ.TESTQUARTZ1" />    <Task:scheduled-tasksScheduler= "task">        <!--executes once every three seconds -        <task:scheduledref= "Appjob"Method= "Execute"Cron= "0/1 * * * *?" />    </Task:scheduled-tasks>    <Task:schedulerID= "task"pool-size= "Ten" />

This write is configurable and is compiled using the annotation method.

[Spring]-Quartz timed task-Annotation

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.