Dynamic job scheduling for Spring + quartz task scheduling

Source: Internet
Author: User

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

I've written a few articles about the integration job scheduling feature in spring, which you can refer to

Spring Task Scheduler Timer for actual combat Spring Task Scheduling combat quartz Simple Trigger Spring Task Scheduler quartz Cron Trigger But those examples are examples of static job scheduling, where the so-called static job refers to job information and scheduling information that is written to die in spring's configuration file. However, many applications of the real situation are the need for dynamic job scheduling, such as dynamic add or delete jobs, dynamic setting of the job trigger. Let's take a look at how to implement the scheduling of dynamic jobs in spring.

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: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= "Scheduler" class= " Org.springframework.scheduling.quartz.SchedulerFactoryBean "lazy-init=" false ">
		<property name=" Triggers ' >
			<list>
			</list>
		</property>
	</bean>

</beans>
Only one instance of the schedule class is configured in the configuration, where the list of triggers is empty. This will not schedule any jobs when the schedule is started.

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

Package org.garbagecan.springstudy.schedule.dynamic;
Import Org.quartz.CronTrigger;
Import Org.quartz.JobDetail;
Import Org.quartz.Scheduler;
Import org.quartz.SchedulerException;
Import Org.springframework.context.ApplicationContext;

Import Org.springframework.context.support.ClassPathXmlApplicationContext;

Import java.text.ParseException; public class Test {public static void main (string[] args) {ApplicationContext ctx = new Classpathxmlapplicationcontex
		T ("/org/garbagecan/springstudy/schedule/dynamic/spring.xml");

		Scheduler Scheduler = (Scheduler) ctx.getbean ("Scheduler");
		System.out.println ("Scheduling to run tasks.");
				for (int i = 0; i < 5; i++) {try {jobdetail jobdetail = new Jobdetail ();
				Jobdetail.setname ("job_" + i);
				MyTask MyTask = new MyTask ();
				Mytask.setname ("task_" + i);
				Jobdetail.getjobdatamap (). Put ("MyTask", MyTask);
				Jobdetail.setjobclass (Myjob.class);

				Scheduler.addjob (Jobdetail, true); Crontrigger Crontrigger =new CroNtrigger ("Cron_" + I, Scheduler.default_group, Jobdetail.getname (), scheduler.default_group);

				Crontrigger.setcronexpression ("0/10 * * * *");
			Scheduler.schedulejob (Crontrigger);
			catch (ParseException e) {e.printstacktrace ();
			catch (Schedulerexception e) {e.printstacktrace ();
		} try {Thread.Sleep (60 * 1000);
		catch (Interruptedexception e) {e.printstacktrace ();
		} System.out.println ("Un-scheduling to run tasks.");
			for (int i = 0; i < 5; i++) {try {scheduler.unschedulejob ("cron_" + I, scheduler.default_group);
			catch (Schedulerexception e) {e.printstacktrace (); }
		}
	}
}
The 4.1 test class first created 5 Jobdetail and Crontrigger, each jobdetail corresponding to one of our myjob and MyTask class instances, each crontrigger a cron expression that runs 10 seconds. The specific API can refer to Quartz's official documentation;
4.2 For each created Jobdetail and Crontrigger, use Quartz Scheduler to do the scheduling;
4.3 Wait 60 seconds, let the above created the job run several times, at this time in the background can see the message output;
4.4 Un-schedule The Jobdetail and Crontrigger created above, after this code, all the jobs were stopped.















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.