Spring Task Scheduler Quartz Cron Trigger

Source: Internet
Author: User

This article address: http://blog.csdn.net/kongxx/article/details/6751326

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

1. The first is a task class, which does not implement any interfaces, and contains a run method to run the task, as follows:

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 the Quartzjobbean class of spring to show that the current class is a Quartz job class that contains an object instance of a task class, and each time the job is scheduled, The Executeinternal method will run with the following code:

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, as follows

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "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=" timer Factory "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.garb Agecan.springstudy.schedule.quartz.MyJob "/> <property name=" Jobdataasmap "> <map> <entry key=" my Task "value-ref=" MyTask "/> </map> </property> </bean> <bean id=" MyTask "class=" Org.garba Gecan.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 of beans, this is to adapt to the quartz jobdetail, which can define name,group,description information, which is mainly to distinguish with other jobs There is also a Jobclass property that defines the instance of the job class, where the Myjob class is created using the second step, and a property called Jobdataasmap, which is a map set provided by the 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, which uses the cron Trigger provided by quartz, in spring, using Crontriggerbean to map, primarily to define cron expressions, and refer to Quartz's official documentation. This is defined as running every 10 seconds and, of course, the most important specific instance of the job class;
3.4 The final definition of a schedulerfactorybean bean, which defines the specific scheduling of those triggers;

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

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");
	}
Run the test class to see that a job called the My task starts and runs every 10 seconds.




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.