Quartz Job Set property parameters, dynamic execution jar__ task scheduling

Source: Internet
Author: User
Tags uuid

Dynamically invoke the jar, configure the start time execution times, and so on.

The scheduled task class (invokes the specified jar with the reflection mechanism):

Package Sy.quartz;

Import Java.lang.reflect.Method;
Import Org.apache.log4j.Logger;
Import Org.quartz.Job;
Import Org.quartz.JobDataMap;
Import Org.quartz.JobExecutionContext;

Import org.quartz.JobExecutionException;

Import Sy.jar.loader.JarLoader;

	public class Myquartzjob implements Job {private static final Logger Logger = Logger.getlogger (Myquartzjob.class); public void execute (jobexecutioncontext context) throws Jobexecutionexception {//Get parameters Jobdatamap JDM = Context.get
		Jobdetail (). Getjobdatamap ();
		String Path = jdm.getstring ("path");
		String C = jdm.getstring ("class");
		String m = jdm.getstring ("method");
		String p = jdm.getstring ("parameter");
		String t = jdm.getstring ("types"); Path = Path = null?
		"": path; c = c = null?
		"": C; m = m = = null?
		"": M.; p = p = = null?
		"": P; t = t = = null?
		"": t;
		string[] ts = t.split (",");
			try {jarloader loader = jarloader.getinstance (); try {//Load jar Loader.addurl ("jar:file:///"+ path +"!/");
				Load Class Class QC = Class.forName (c, true, loader);
				Gets the parameter type//class[] classs = gettypes (ts);
				Method ms = NULL;
					No parameters do not set the parameter if (p.length () = = 0) {ms = Qc.getdeclaredmethod (M, new class<?>[] {});
				Ms.invoke (qc.newinstance (), NULL);
					else {ms = Qc.getdeclaredmethod (M, object[].class);
				Ms.invoke (Qc.newinstance (), P.split (","));
			}//object[] Objs = Getvaluesbytype (P.split (","), classs);
			catch (Exception e) {e.printstacktrace ();
			The catch (Exception e) {logger.error ("path:" + path + "/class:" + C + "/method:" + M, E.getcause ());
		E.printstacktrace (); }

	}
}

Data storage class:

Package sy.model.base;
Import java.io.Serializable;
Import Java.util.Date;

Import Java.util.UUID;
Import Javax.persistence.Column;
Import javax.persistence.Entity;
Import Javax.persistence.Id;
Import javax.persistence.Table;
Import javax.persistence.Temporal;

Import Javax.persistence.TemporalType;
Import Org.apache.commons.lang3.StringUtils;
Import Org.hibernate.annotations.DynamicInsert;

Import Org.hibernate.annotations.DynamicUpdate;  @Entity @Table (name = "Syquartz", schema = "") @DynamicInsert (True) @DynamicUpdate (true) public class Syquartzs implements
	Serializable {/** * * */private static final long serialversionuid = 1L;
	Private String ID;
	Private String Executepath;
	Private String Executeclass;
	Private String Executemethod;
	Private String parameter;
	Private Date StartTime;
	Private Date Endtime;
	private int repeatcount;

	Private long repeatinterval;
	Public Syquartzs () {super (); Public Syquartzs (string ID, String Executepath, String executeclass, STring Executemethod, String parameter, date starttime, date endtime, int repeatcount, long repeatinterval) {super ();
		This.id = ID;
		This.executepath = Executepath;
		This.executeclass = Executeclass;
		This.executemethod = Executemethod;
		This.parameter = parameter;
		This.starttime = StartTime;
		This.endtime = Endtime;
		This.repeatcount = RepeatCount;
	This.repeatinterval = Repeatinterval; @Id @Column (name = "Id", unique = true, Nullable = false, length = =) public String getId () {if (!
		Stringutils.isblank (this.id)) {return this.id;
	Return Uuid.randomuuid (). toString ();
	@Column (name = "Executepath", nullable = false, length = +) public String Getexecutepath () {return executepath;
	@Column (name = "Executeclass", length = m) public String Getexecuteclass () {return executeclass;
	@Column (name = "Executemethod", length = m) public String Getexecutemethod () {return executemethod;
	@Column (name = "PARAMETER", length = 500)Public String GetParameter () {return parameter; @Temporal (Temporaltype.timestamp) @Column (name = "StartTime", length = 7) public Date GetStartTime () {return star
	Ttime; 
	@Temporal (Temporaltype.timestamp) @Column (name = "Endtime", length = 7) public Date Getendtime () {return endtime;
	@Column (name = "RepeatCount", length = ten) public int getrepeatcount () {return repeatcount;
	@Column (name = "Repeatinterval", length =) public long Getrepeatinterval () {return repeatinterval;
	public void SetId (String id) {this.id = ID;
	} public void Setexecutepath (String executepath) {this.executepath = Executepath;
	} public void Setexecuteclass (String executeclass) {this.executeclass = Executeclass;
	} public void Setexecutemethod (String executemethod) {this.executemethod = Executemethod;
	} public void Setparameter (String parameter) {this.parameter = parameter; } public void Setstarttime (Date starttime) {this.starttime = StarttimE
	The public void Setendtime (Date endtime) {this.endtime = Endtime;
	The public void Setrepeatcount (int repeatcount) {this.repeatcount = RepeatCount;
	} public void Setrepeatinterval (long repeatinterval) {this.repeatinterval = Repeatinterval;
 }

}

Test method:

Package Sy.quartz;
Import java.util.ArrayList;
Import Java.util.Date;
Import java.util.List;

Import Java.util.UUID;
Import Org.quartz.JobDataMap;
Import Org.quartz.JobDetail;
Import Org.quartz.Scheduler;
Import org.quartz.SchedulerException;
Import Org.quartz.SimpleTrigger;
Import Org.quartz.impl.StdSchedulerFactory;

Import Org.springframework.stereotype.Service;

Import Sy.model.base.Syquartzs; @Service public class Quartztest {public static void main (string[] args) {//test data list<syquartzs> QS = Getqu
		Artzslist ();
				for (Syquartzs Q:qs) {try {Scheduler Scheduler = Stdschedulerfactory.getdefaultscheduler ();
				Produces a uuuid that avoids the job name repeating String uid = Uuid.randomuuid (). toString ();
				Jobdetail job = new Jobdetail ("job" + uid, scheduler.default_group, Myquartzjob.class);
				Set the job parameter, the schedule executes with class.newinstance (), so setting the value for the object is meaningless jobdatamap Jobdatamap = new Jobdatamap ();
				Jobdatamap.put ("Path", Q.getexecutepath ()); Jobdatamap.put ("Class", q.getexecUteclass ());
				Jobdatamap.put ("Method", Q.getexecutemethod ());
				Jobdatamap.put ("parameter", Q.getparameter ());
				Job.setjobdatamap (JOBDATAMAP); Create a simple rule simpletrigger trigger = new Simpletrigger ("trig" + uid, Scheduler.default_group, GetDate (Q.getstart
				Time ()), GetDate (Q.getendtime ()), Q.getrepeatcount (), Q.getrepeatinterval ());
				Scheduler.schedulejob (Job, trigger);
			Scheduler.start ();
			catch (Schedulerexception e) {//TODO auto-generated catch block E.printstacktrace ();  }}/** * Determines whether the start time is less than the current time, and is less than the current time begins the call with the current time * * @param DT * @return/private static date getDate (date
		DT) {if (dt!= null && system.currenttimemillis () > Dt.gettime ()) {return new Date ();
	} return DT; /** * Get Test data * * @return/private static list<syquartzs> Getquartzslist () {list<syquartzs>
		QS = new arraylist<syquartzs> ();
			Syquartzs q1 = new Syquartzs ("1", "D:/test/test1.jar",	"Com.ly.test.Test", "Add", "" ", New Date (), NULL,-1, 5000l);
		Syquartzs q2 = new Syquartzs ("2", "D:/test/test1.jar", "Com.ly.test.Test", "Add", "" ", New Date (), NULL,-1, 10000l); 
		Syquartzs q3 = new Syquartzs ("3", "D:/test/test1.jar", "Com.ly.test.Test", "Add", "" ", New Date (), NULL,-1, 15000l); 
		Syquartzs Q4 = new Syquartzs ("4", "D:/test/test1.jar", "Com.ly.test.Test", "Add", "" ", New Date (), NULL, 9, 60000l);
		Qs.add (Q1);
		Qs.add (Q2);
		Qs.add (Q3);
		Qs.add (Q4);
	return QS;
 }
}

Well, execute the schedule successfully, this example uses the Quartz-all-1.6.6.jar

The audit will be accompanied by the download address and the download address of the test item.

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.