public void Run(List <JobList> jobLists) { //LogProvider.SetCurrentLogProvider(new QuartzLogProvider(LogHelper.Quartz)); try { var props = new NameValueCollection { { "quartz.serializer", "binary" } }; var factory = new StdSchedulerFactory(props); var scheduler = factory.GetScheduler(); scheduler.Start(); foreach (var jobList in jobLists) { var job = JobBuilder.Create(jobList.JobType.GetType()).WithIdentity(jobList.Name, jobList.Group).Build(); var trigger = new Quartz.Impl.Triggers.CronTriggerImpl(jobList.Name, jobList.Group, jobList.CronExpression); scheduler.ScheduleJob(job, trigger); } } catch (SchedulerException se) { LogHelper.Quartz(se.Message, se); } }
public void StartQuartz() { if (!scheduler.IsStarted) { scheduler.Start(); Quartz.IJobDetail job1 = new Quartz.Impl.JobDetailImpl("计算用户最新收益", "EarningsJob", typeof(EarningsJob)); Quartz.ICronTrigger trigger = new Quartz.Impl.Triggers.CronTriggerImpl("计算用户最新收益触发器", "triggergroup", "计算用户最新收益", "EarningsJob", "0 0 6 * * ?");//每天早上六点执行 scheduler.ScheduleJob(job1, trigger); Common.CustomLog.WriteLog("成功启动任务!" + scheduler.IsStarted); } }
public bool StartJob(string jobName, IJobExecutor handler, string cron) { lock (joblocker) { var existjob = Jobs.Exists(x => x.JobName == jobName); if (existjob) { return(false); } //string InnerJobId = Guid.NewGuid().ToString(); JobDetailImpl jobdetail = new JobDetailImpl(jobName, typeof(RuanalInnerJob)); jobdetail.Description = "内部调度任务"; Quartz.ITrigger triger = null; var isrunonce = cron.ToLower() == "runonce"; if (isrunonce) { var ttriger = new Quartz.Impl.Triggers.SimpleTriggerImpl("trigger_" + jobName); ttriger.RepeatCount = 0; ttriger.RepeatInterval = TimeSpan.FromSeconds(1); triger = ttriger; } else { var ttriger = new Quartz.Impl.Triggers.CronTriggerImpl("trigger_" + jobName); ttriger.CronExpressionString = cron; triger = ttriger; } JobContext jobitem = new JobContext() { // InnerJobId = InnerJobId, IsRunOnce = isrunonce, JobName = jobName, JobDetail = jobdetail, LastRunTime = null, OnInvoke = handler }; jobitem.Triggers.Add(triger); Jobs.Add(jobitem); Scheduler.ScheduleJob(jobdetail, triger); return(true); } }
public void TestSchedulerFactoryObjectWithPlainQuartzObjects() { IJobFactory jobFactory = new AdaptableJobFactory(); TestObject tb = new TestObject("tb", 99); JobDetail jobDetail0 = new JobDetail(); jobDetail0.JobType = typeof (IJob); jobDetail0.Name = ("myJob0"); jobDetail0.Group = (SchedulerConstants.DefaultGroup); jobDetail0.JobDataMap.Add("testObject", tb); Assert.AreEqual(tb, jobDetail0.JobDataMap.Get("testObject")); CronTrigger trigger0 = new CronTrigger(); trigger0.Name = ("myTrigger0"); trigger0.Group = SchedulerConstants.DefaultGroup; trigger0.JobName = "myJob0"; trigger0.JobGroup = SchedulerConstants.DefaultGroup; trigger0.StartTimeUtc = (DateTime.UtcNow); trigger0.CronExpressionString = ("0/1 * * * * ?"); TestMethodInvokingTask task1 = new TestMethodInvokingTask(); MethodInvokingJobDetailFactoryObject mijdfb = new MethodInvokingJobDetailFactoryObject(); mijdfb.Name = ("myJob1"); mijdfb.Group = (SchedulerConstants.DefaultGroup); mijdfb.TargetObject = (task1); mijdfb.TargetMethod = ("doSomething"); mijdfb.AfterPropertiesSet(); JobDetail jobDetail1 = (JobDetail) mijdfb.GetObject(); SimpleTrigger trigger1 = new SimpleTrigger(); trigger1.Name = "myTrigger1"; trigger1.Group = SchedulerConstants.DefaultGroup; trigger1.JobName = "myJob1"; trigger1.JobGroup = SchedulerConstants.DefaultGroup; trigger1.StartTimeUtc = (DateTime.UtcNow); trigger1.RepeatCount = (SimpleTrigger.RepeatIndefinitely); trigger1.RepeatInterval = TimeSpan.FromMilliseconds(20); IScheduler scheduler = MockRepository.GenerateMock<IScheduler>(); SchedulerFactoryObject schedulerFactoryObject = new TestSchedulerFactoryObject(scheduler); schedulerFactoryObject.JobFactory = (jobFactory); schedulerFactoryObject.JobDetails = (new JobDetail[] {jobDetail0, jobDetail1}); schedulerFactoryObject.Triggers = (new Trigger[] {trigger0, trigger1}); try { schedulerFactoryObject.AfterPropertiesSet(); schedulerFactoryObject.Start(); } finally { schedulerFactoryObject.Dispose(); } scheduler.AssertWasCalled(x => x.JobFactory = jobFactory); scheduler.AssertWasCalled(x => x.AddJob(jobDetail0, true)); scheduler.AssertWasCalled(x => x.AddJob(jobDetail1, true)); #if QUARTZ_2_0 scheduler.AssertWasCalled(x => x.GetJobDetail(new JobKey("myJob0", SchedulerConstants.DefaultGroup))); scheduler.AssertWasCalled(x => x.GetJobDetail(new JobKey("myJob1", SchedulerConstants.DefaultGroup))); scheduler.AssertWasCalled(x => x.GetTrigger(new TriggerKey("myTrigger0", SchedulerConstants.DefaultGroup))); scheduler.AssertWasCalled(x => x.GetTrigger(new TriggerKey("myTrigger1", SchedulerConstants.DefaultGroup))); #else scheduler.AssertWasCalled(x => x.GetJobDetail("myJob0", SchedulerConstants.DefaultGroup)); scheduler.AssertWasCalled(x => x.GetJobDetail("myJob1", SchedulerConstants.DefaultGroup)); scheduler.AssertWasCalled(x => x.GetTrigger("myTrigger0", SchedulerConstants.DefaultGroup)); scheduler.AssertWasCalled(x => x.GetTrigger("myTrigger1", SchedulerConstants.DefaultGroup)); #endif }