c# - How to add job with trigger for running Quartz.Net scheduler instance without restarting server? -
is possible add together job trigger running quartz.net scheduler instance without restarting server?
a robust implementation adojobstore have custom table store jobs , create class inherits ischedulerplugin , ijob create schedules job automatically.
your config this:
<add key="quartz.plugin.sqlquartzjobs.type" value="(jobschedulerplugin assembly path)" /> <add key="quartz.plugin.sqlquartzjobs.rescancronexpression" value="0 0/5 * * * ?" /> //plugin should fire every 5 minutes <add key="quartz.plugin.sqlquartzjobs.connectionstring" value="(your connection string)" /> your plugin/job class can this:
public class jobschedulerplugin : ischedulerplugin, ijob { //entry point plugin, quartz server runs when starts public void initialize(string pluginname, ischeduler sched) { name = pluginname; scheduler = sched; } //runs after initialize() public void start() { //schedule plugin job jobdatamap jobdata = new jobdatamap(); jobdata["connectionstring"] = connectionstring; ijobdetail job = jobbuilder.create(this.gettype()) .withdescription("job rescan jobs sql db") .withidentity(new jobkey(jobinitializationpluginjobname, jobinitializationplugingroup)) .usingjobdata(jobdata) .build(); triggerkey triggerkey = new triggerkey(jobinitializationpluginjobtriggername, jobinitializationplugingroup); itrigger trigger = triggerbuilder.create() .withcronschedule(configfilecronexpression) .startnow() .withdescription("trigger sql job loader") .withidentity(triggerkey) .withpriority(1) .build(); scheduler.schedulejob(job, trigger); } } now jobschedulerplugin has entered trigger qrtz_triggers fire every 5 minutes highest priority. can utilize load jobs custom table (let's phone call quartzjobs). quartzjobs can contain info such jobnames, assembly paths, dates, status, etc, can used help create triggers efficiently. should contain cron look job. can when trigger fires:
//entry point of every job public void execute(ijobexecutioncontext context) { scheduler = context.scheduler; jobcollection jobs = loadjobs(context.jobdetail.jobdatamap["connectionstring"].tostring()); jobswithtriggers jobtriggers = createtriggers(jobs); schedulerjob(jobtriggers); } //you can utilize ado.net or orm here load job info the table //and force class. protected jobcollection loadjobs(string connectionstring); //in class can create jobdetails , itriggers each job //and force them custom class protected jobswithtriggers createtriggers(jobs); //finally here can schedule jobs protected void schedulejobs(jobstriggers) in each of classes above can add together custom validation making sure triggers handled appropriately if status or cron look changes.
with solution server never need restarted. plugin/job class scan table , deed accordingly.
c# c#-4.0 quartz-scheduler quartz.net
No comments:
Post a Comment