Spring task Scheduling Combat quartz Cron Trigger

Source: Internet
Author: User

In addition to using the simplest simple trigger in quartz, you can run the job in a way similar to the crontrigger of cron jobs on Linux, here's a small example:

1. First is a task class, which does not implement any interface, which contains a run method for running the task, the code is as follows:

[Java]View Plaincopyprint?
  1. Package Org.garbagecan.springstudy.schedule.quartz;
  2. Public class MyTask {
  3. private String name;
  4. public Void Run () {
  5. System.out.println ("Run task:" + name + ".");
  6. }
  7. Public String GetName () {
  8. return name;
  9. }
  10. public void SetName (String name) {
  11. this.name = name;
  12. }
  13. }

2. A job class that needs to inherit spring's Quartzjobbean class to show that the current class is a Quartz job class that contains an object instance of a task class that, when each job is dispatched, The Executeinternal method will be run with the following code:

[Java]View Plaincopyprint?
  1. Package Org.garbagecan.springstudy.schedule.quartz;
  2. Import Org.quartz.JobExecutionContext;
  3. Import org.quartz.JobExecutionException;
  4. Import Org.springframework.scheduling.quartz.QuartzJobBean;
  5. Public class MyJob extends Quartzjobbean {
  6. private MyTask MyTask;
  7. protected void Executeinternal (Jobexecutioncontext context)
  8. throws Jobexecutionexception {
  9. Mytask.run ();
  10. }
  11. Public MyTask Getmytask () {
  12. return mytask;
  13. }
  14. public void Setmytask (MyTask mytask) {
  15. this.mytask = MyTask;
  16. }
  17. }

3. Spring configuration file with the following content

[HTML]View Plaincopyprint?
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <beans xmlns="Http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/ Beans/spring-beans-2.5.xsd "
  5. default-lazy-init="true">
  6. <Bean id= "timerfactory" class=" Org.springframework.scheduling.quartz.SchedulerFactoryBean " lazy-init="false ">
  7. <property name="triggers">
  8. <list>
  9. <ref local= "" Crontrigger ""/>
  10. </list>
  11. </Property>
  12. </Bean>
  13. <Bean id= "crontrigger" class="Org.springframework.scheduling.quartz.CronTriggerBean" >
  14. <property name= "jobdetail" ref="MyJob"/>
  15. <property name="cronexpression" value= "0/10 * * * *?" ></Property>
  16. </Bean>
  17. <Bean id= "myJob" class="Org.springframework.scheduling.quartz.JobDetailBean">
  18. <property name="name" value="MyJob"/>
  19. <property name="group" value="MyGroup"/>
  20. <property name= "description" value="MyJob"/>
  21. <property name= "jobclass" value="Org.garbagecan.springstudy.schedule.quartz.MyJob" />
  22. <property name="Jobdataasmap">
  23. <map>
  24. <entry key="MyTask" value-ref="MyTask" />
  25. </Map>
  26. </Property>
  27. </Bean>
  28. <Bean id= "mytask" class="Org.garbagecan.springstudy.schedule.quartz.MyTask">
  29. <property name="name" value="My task"/>
  30. </Bean>
  31. </Beans>

3.1 First, you need to define a task class, the specific task logic can be written in this class;
3.2 Defines a Jobdetailbean class bean, this is to adapt to the quartz jobdetail, which can define name,group,description and other information, which is mainly for the purpose of distinguishing with other jobs There is also a Jobclass attribute that defines the instance of the job class that is used, the Myjob class created in the second step, and finally a property called Jobdataasmap, which is a set of maps provided by quartz for passing parameters. The classes in these collections are automatically injected into the target job class by spring;
3.3 Create a trigger class, here is the cron Trigger provided by quartz, in spring, using the Crontriggerbean to do the mapping, mainly to define cron expressions, specifically refer to Quartz official documents, This is defined as running every 10 seconds and, of course, the most specific instances of job classes;
3.4 Finally, a schedulerfactorybean bean is defined, which defines the triggers to be dispatched;

4. Finally, write a test class to test the above code and configuration

[Java]View Plaincopyprint?
  1. Package Org.garbagecan.springstudy.schedule.quartz;
  2. Import Org.springframework.context.support.ClassPathXmlApplicationContext;
  3. Public class Test {
  4. public static void Main (string[] args) throws Exception {
  5. New Classpathxmlapplicationcontext ("/org/garbagecan/springstudy/schedule/quartz/spring.xml");
  6. }
  7. }

Running the test class, you can see that a job called my task starts and runs every 10 seconds.

Spring task Scheduling Combat quartz Cron Trigger

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.