c# - What happens when quartz sheduler becomes out of scope? -
what happens when quartz sheduler becomes out of scope? garbage collector kills or keep running? samle code:
namespace quartz_test { public class simplejob : ijob { public void execute(ijobexecutioncontext context) { console.writeline("hello there"); } } class program { static void scheduletask() { // construct scheduler factory ischedulerfactory schedfact = new stdschedulerfactory(); // scheduler ischeduler scheduler = schedfact.getscheduler(); scheduler.start(); ijobdetail job = jobbuilder.create<simplejob>() .withidentity("myjob", "group1") .build(); itrigger trigger = triggerbuilder.create() .withsimpleschedule(x => x.withintervalinseconds(10).withrepeatcount(3)) .build(); scheduler.schedulejob(job, trigger); } static void main(string[] args) { scheduletask(); //link scheduler lost here //how stop simplejob execution? //console.readline(); } }
this code outputs:
hello there hello there hello there
all seems ok, why program never finish if comment last console.readline();
?
and last question: how stop ijob execution if lost links sceduler
there shutdown method in library.
void shutdown( bool waitforjobstocomplete);
you can use it.
ischeduler scheduler = schedfact.getdefaultscheduler(); scheduler.shutdown(false);
you can use either unschedulejob or deletejob method.
scheduler.unschedulejob(new triggerkey("myjob", "group1"));
or
scheduler.deletejob(new jobkey("myjob", "group1"));
here program debugging information. borrowed code here
using quartz; using quartz.impl; using quartz.impl.matchers; using system; using system.collections.generic; using system.linq; using system.text; using system.threading; using system.threading.tasks; namespace quartz_test { public class simplejob : ijob { public void execute(ijobexecutioncontext context) { console.writeline("hello there"); } } class program { private static void getalljobs(ischeduler scheduler) { ilist<string> jobgroups = scheduler.getjobgroupnames(); // ilist<string> triggergroups = scheduler.gettriggergroupnames(); console.write("start ===== \n start? " + scheduler.isstarted); console.writeline("is stop? " + scheduler.isshutdown); foreach (string group in jobgroups) { var groupmatcher = groupmatcher<jobkey>.groupcontains(group); var jobkeys = scheduler.getjobkeys(groupmatcher); console.writeline(); foreach (var jobkey in jobkeys) { var detail = scheduler.getjobdetail(jobkey); var triggers = scheduler.gettriggersofjob(jobkey); foreach (itrigger trigger in triggers) { console.writeline("group : " + group); console.writeline("name : " + jobkey.name); console.writeline("desc : " + detail.description); console.writeline("trg name : " + trigger.key.name); console.writeline("trg group : " + trigger.key.group); console.writeline("trg type : " + trigger.gettype().name); console.writeline("trg state : " + scheduler.gettriggerstate(trigger.key)); datetimeoffset? nextfiretime = trigger.getnextfiretimeutc(); if (nextfiretime.hasvalue) { console.writeline("next trigger? : " + nextfiretime.value.localdatetime.tostring()); } datetimeoffset? previousfiretime = trigger.getpreviousfiretimeutc(); if (previousfiretime.hasvalue) { console.writeline("previous trigger? : " + previousfiretime.value.localdatetime.tostring()); } } } console.writeline(); } console.writeline("===== end \n"); } static void scheduletask() { ischeduler scheduler = null; ijobdetail job = null; itrigger trigger = null; try { // construct scheduler factory ischedulerfactory schedfact = new stdschedulerfactory(); // scheduler scheduler = schedfact.getscheduler(); scheduler.start(); job = jobbuilder.create<simplejob>() .withidentity("myjob", "group1") .build(); trigger = triggerbuilder.create() .withsimpleschedule(x => x.withintervalinseconds(10).withrepeatcount(3)) .build(); scheduler.schedulejob(job, trigger); getalljobs(scheduler); } catch (exception e) { console.writeline(e.innerexception.tostring()); } { getalljobs(scheduler); timespan spn = timespan.fromseconds(100l); thread.sleep(spn); scheduler.shutdown(true); } } static void main(string[] args) { scheduletask(); //link scheduler lost here //how stop simplejob execution? //console.readline(); } } }
best regards, yaros
wiki
Comments
Post a Comment