Quartz.Net 刪除一個Job


Quartz.Net 刪除Job

來博客園的第一篇文章先寫個簡單的,希望能幫助到大家。

步入正題:

     Quartz.Net有三個重要的概念,分別是 Scheduler 、Job 、Trigger。

     Scheduler包含JobTrigger。

     如果要刪除一個正在運行的Job,需要在Schedule中將其移除。

     調用的是IScheduler 中的DeleteJob 方法

上代碼:

    

 public class JobController
    {
        private static JobController _jobController = new JobController();

        private JobController()
        {

        }

        public static JobController Instance()
        {
            return _jobController;
        }

        public void Start()
        {
            LogProvider.SetCurrentLogProvider(new ConsoleLogProvider());

            RunProgramRunExample().GetAwaiter().GetResult();

            Console.WriteLine("Press any key to close the application");
            Console.ReadKey();


        }


        private static async Task RunProgramRunExample()
        {
            try
            {
               
                NameValueCollection props = new NameValueCollection
                {
                    { "quartz.serializer.type", "binary" }
                };
                StdSchedulerFactory factory = new StdSchedulerFactory(props);
                IScheduler scheduler = await factory.GetScheduler();

               
                await scheduler.Start();
               
                for (int i = 0; i < 10; i++)
                {
                   
                    IJobDetail job = JobBuilder.Create<AutoJob>()
                         .WithIdentity("計算作業" + i.ToString(), "組1")
                        .UsingJobData("jobSays", $"Hello {i}!")
                        .UsingJobData("myFloatValue", i)
                        .Build();

                   
                    ITrigger trigger = TriggerBuilder.Create()
                        .WithIdentity($"trigger{i}", "group1")
                        .StartNow()
                        .WithSimpleSchedule(x => x
                            .WithIntervalInSeconds(10)
                            .RepeatForever())
                        .Build();

                    // Tell quartz to schedule the job using our trigger
                    await scheduler.ScheduleJob(job, trigger);
                }

                JobKey jk = new JobKey("計算作業1", "組1");
                await scheduler.DeleteJob(jk);//移除一個job

               
                await Task.Delay(-1);
              
                await scheduler.Shutdown();

            }
            catch (SchedulerException se)
            {
                Console.WriteLine(se);
            }
        }

    }

寫在最后:

    第一次寫沒什么經驗,歡迎大家批評指教!

    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM