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?
- Package Org.garbagecan.springstudy.schedule.quartz;
- Public class MyTask {
- private String name;
- public Void Run () {
- System.out.println ("Run task:" + name + ".");
- }
- Public String GetName () {
- return name;
- }
- public void SetName (String name) {
- this.name = name;
- }
- }
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?
- Package Org.garbagecan.springstudy.schedule.quartz;
- Import Org.quartz.JobExecutionContext;
- Import org.quartz.JobExecutionException;
- Import Org.springframework.scheduling.quartz.QuartzJobBean;
- Public class MyJob extends Quartzjobbean {
- private MyTask MyTask;
- protected void Executeinternal (Jobexecutioncontext context)
- throws Jobexecutionexception {
- Mytask.run ();
- }
- Public MyTask Getmytask () {
- return mytask;
- }
- public void Setmytask (MyTask mytask) {
- this.mytask = MyTask;
- }
- }
3. Spring configuration file with the following content
[HTML]View Plaincopyprint?
- <? XML version= "1.0" encoding="UTF-8"?>
- <beans xmlns="Http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/ Beans/spring-beans-2.5.xsd "
- default-lazy-init="true">
- <Bean id= "timerfactory" class=" Org.springframework.scheduling.quartz.SchedulerFactoryBean " lazy-init="false ">
- <property name="triggers">
- <list>
- <ref local= "" Crontrigger ""/>
- </list>
- </Property>
- </Bean>
- <Bean id= "crontrigger" class="Org.springframework.scheduling.quartz.CronTriggerBean" >
- <property name= "jobdetail" ref="MyJob"/>
- <property name="cronexpression" value= "0/10 * * * *?" ></Property>
- </Bean>
- <Bean id= "myJob" class="Org.springframework.scheduling.quartz.JobDetailBean">
- <property name="name" value="MyJob"/>
- <property name="group" value="MyGroup"/>
- <property name= "description" value="MyJob"/>
- <property name= "jobclass" value="Org.garbagecan.springstudy.schedule.quartz.MyJob" />
- <property name="Jobdataasmap">
- <map>
- <entry key="MyTask" value-ref="MyTask" />
- </Map>
- </Property>
- </Bean>
- <Bean id= "mytask" class="Org.garbagecan.springstudy.schedule.quartz.MyTask">
- <property name="name" value="My task"/>
- </Bean>
- </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?
- Package Org.garbagecan.springstudy.schedule.quartz;
- Import Org.springframework.context.support.ClassPathXmlApplicationContext;
- Public class Test {
- public static void Main (string[] args) throws Exception {
- New Classpathxmlapplicationcontext ("/org/garbagecan/springstudy/schedule/quartz/spring.xml");
- }
- }
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