Quartz Scheduler
程序员文章站
2022-09-26 22:01:10
using Quartz;public static class SchedulerProgram { public static void Start(IScheduler scheduler = null) { try { Assembly asm = typeof(SchedulerProgr ......
using quartz;
public static class schedulerprogram { public static void start(ischeduler scheduler = null) { try { assembly asm = typeof(schedulerprogram).gettypeinfo().assembly; type[] types = asm.gettypes(); idictionary<int, type> typemap = new dictionary<int, type>(); list<type> typelist = new list<type>(); foreach (type t in types) { if (new list<type>(t.getinterfaces()).contains(typeof(ijobscheduler))) { typelist.add(t); } } foreach (type t in typelist) { ijobscheduler schedule = objectutils.instantiatetype<ijobscheduler>(t); if (scheduler == null) { schedule.run().configureawait(false); } else { schedule.run(scheduler).configureawait(false); } } } catch (exception ex) { throw ex; } } }
public class testjobscheduler : ijobscheduler { public async task run(ischeduler scheduler) { logger.writelog("call job scheduler"); //创建任务 ijobdetail job = jobbuilder.create<testjob>(). withidentity("entityrelationshiptestjob", "crmjobgroup"). usingjobdata("jobschedulename", "entityrelationshiptestjob"). build(); //创建触发器 itrigger trigger = triggerbuilder.create().withidentity("contacttrigger", "timegroup").withsimpleschedule(t => t.withintervalinminutes(30).withrepeatcount(0)).build();// test trigger //添加任务及触发器至调度器中 await scheduler.schedulejob(job, trigger); //启动 logger.writelog("start job scheduler"); await scheduler.start(); } public task run() { throw new notimplementedexception(); } }
public interface ijobscheduler { /// <summary> /// /// </summary> /// <returns></returns> task run(); /// <summary> /// 外部传入scheduler /// </summary> /// <param name="scheduler"></param> /// <returns></returns> task run(ischeduler scheduler); }
public class testjob : ijob { public task execute(ijobexecutioncontext context) {//具体的方法 return task.fromresult(0); } }