在Windows窗体应用程序上使用quartz 3.0.5.0,并遇到无法找到现有触发器的问题

时间:2018-07-02 01:33:46

标签: c# quartz

  • 在按下按钮(添加,删除或更新)时正在调用UpdateSchedules方法的另一个类中创建了quartzUse实例
  • 创建第一个作业并触发第一个计时器后
  • 在添加另一个具有开始和结束时间的触发键并添加到列表作业并调用UpdateSchedules方法之后,创建另一个

但是由于某种原因,无法找到现有触发器来重新安排时间 我是C#的新手

namespace CustomSchedules
{
    public class QuartzUse
    {

        public delegate void EventTriggered(JobModel sender);

        public event EventTriggered QuartzEvent;
        public List<JobModel> Jobs = new List<JobModel>();

        private void SetEvent(JobModel job)
        {
            if (job != null)
                QuartzEvent(job);
        }

        //string mins, string hrs, uint number, object hmm
        //in here every time a newer timer is added previously added timer will also get added again
        public async Task UpdateSchedules()
        {

            //https://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/crontriggers.html
            NameValueCollection props = new NameValueCollection
                {
                    { "quartz.serializer.type", "binary" }
                };
            ISchedulerFactory factory = new StdSchedulerFactory(props);
            IScheduler scheduler = await factory.GetScheduler();
            scheduler.Context.Put("context", this);

            // and start it off

            if (!scheduler.IsStarted)
                await scheduler.Start();


            foreach (var info in Jobs)
            {
                //list gets checked one by one based on trigger name and 
                //check if it exists in schedules or not
                //if exists it gets updated 
                var jobDataMap = new Dictionary<string, object>();
                jobDataMap.Add("job", info);

                var triggerKey = new TriggerKey(info.TriggerName, "group1");
                var existingJob = await scheduler.GetTrigger(triggerKey);


                //everytime timer gets added job with same object name gets added


                if (existingJob == null)
                {
                    IJobDetail job = JobBuilder.Create<SomeJob>()
                   .WithIdentity("job" + info.TriggerName, "group1")
                   .UsingJobData("jobdata", JsonConvert.SerializeObject(info))
                   .Build();

                    ITrigger trigger = TriggerBuilder.Create()
                            .WithIdentity("trigger" + info.TriggerName, "group1")
                            .StartAt(info.StartTime)
                            .EndAt(info.EndTime)
                            .Build();
                    await scheduler.ScheduleJob(job, trigger);

                }
                else if(existingJob!=null && info.state==0)
                {
                    await scheduler.UnscheduleJob(triggerKey);
                }
                else
                {
                    IJobDetail job = JobBuilder.Create<SomeJob>()
                   .WithIdentity("job" + info.TriggerName, "group1")
                   .UsingJobData("jobdata", JsonConvert.SerializeObject(info))
                   .Build();

                    ITrigger trigger = TriggerBuilder.Create()
                            .WithIdentity("trigger" + info.TriggerName, "group1")
                            .StartAt(info.StartTime)
                            .EndAt(info.EndTime)
                            .Build();
                    await scheduler.RescheduleJob(triggerKey, trigger);
                }
            existingJob = await scheduler.GetTrigger(triggerKey);

            var jobKey = new JobKey("job"+triggername, "group1");
            var ja = scheduler.CheckExists(jobKey);
            }

            // and last shut down the scheduler when you are ready to close your program

        }

        public class SomeJob : IJob
        {
            public async Task Execute(IJobExecutionContext context)
            {
                var jobDetail = context.JobDetail as JobDetailImpl;
                var jobDataMap = JsonConvert.DeserializeObject<JobModel>(jobDetail.JobDataMap.GetString("jobdata"));
                var schedulerContext = context.Scheduler.Context.Get("context");

                if (schedulerContext != null)
                {
                    var quartzObject = schedulerContext as QuartzUse;
                    quartzObject.SetEvent(jobDataMap);
                }

                //await Console.Out.WriteLineAsync(jobDetail.Name + " got triggered");
            }
        }

    }
}

0 个答案:

没有答案