使用Quartz调度作业-需要意见 [英] Scheduling Jobs using Quartz - Need Opinion

查看:145
本文介绍了使用Quartz调度作业-需要意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的要求.

主要作业:Job1(计划每5分钟运行一次) 子作业:Job2,Job3,Job4,Job5等(所有子作业将具有相同的定义,但只有JobData会有所不同)

Main Job: Job1 (Scheduled to run every 5 mins) Subsidiary Jobs: Job2, Job3, Job4, Job5, etc., (All subsidiary jobs will have same definitions but only JobData will differ)

在每次执行作业1"期间,一个子作业将被安排为仅执行一次. 例子: Job1的第一次执行:Job"Job2"将安排在DateTime.2分钟后执行 Job2的第二次执行:Job"Job3"将从DateTime.Now 2分钟安排 ...

During every execution of "Job1" one subsidiary job will be scheduled to be executed only once. Example: 1st Execution of Job1: Job "Job2" will be scheduled 2 mins from DateTime.Now 2nd Execution of Job2: Job "Job3" will be scheduled 2 mins from DateTime.Now ...

现在,我希望所有作业都使用相同的Scheduled. 我的问题是Quartz是否对所有工作都使用相同的调度程序,还是应该以编程方式设置它??

Now, I want the same Scheduled to be used by all the jobs. My Question is whether Quartz utilizes the same scheduler for all the jobs or should we programmatically set that??

我正在使用Windows服务来启动和停止整个过程. 这是Windows服务的代码.

I'm using a Windows Service to Start and Stop the whole process. Here is the code of the Windows Service.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;

namespace TestScheduling
{
    class TestMainClass : ServiceBase
    {
        TestJobScheduler scheduler = new TestJobScheduler();

        public TestMainClass()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            try
            {
            scheduler.StartScheduler();
                scheduler.ScheduleMainJob();
            }
            catch (Exception e)
            {
                //Capture Exception
            }
        }

        protected override void OnStop()
    {
        scheduler.StopScheduler();
    }
    }
}

这是TestJobScheduler类的代码.

Here is the Code for the Class TestJobScheduler.

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Data.Odbc;
using Quartz;
using Quartz.Impl;

namespace TestScheduling
{
    class TestJobScheduler : ITaskScheduler
    {
        IScheduler sched;

        public void ScheduleMainJob()
        {
            ....
            sched = getScheduler();
            sched.ScheduleJob(job1,trig);
            ....
            ScheduleSubsidiaryJob("job2")
        }

        public void ScheduleSubsidiaryJob(String jobname)
        {
            ...
            /*Create New Trigger and Associate Subsidiary Job with new JobData*/
            sched = getScheduler();
            sched.ScheduleJob(trig);
        }

        public IScheduler getScheduler()
        {
            ISchedulerFactory sf = new StdSchedulerFactory();
            return sf.GetScheduler();
        }

        public void StartScheduler(IJobDetail job, ISimpleTrigger trigger)
        {
            sched = getScheduler();
            sched.Start();
        }

        public void StopScheduler()
        {
            if (sched != null)
            {
                sched.Shutdown();
            }
        }
    }
}

还有一点是,我将AdoJobStore与OracleDelegate结合使用.

One More point is that I'm using AdoJobStore with OracleDelegate.

有人可以告诉我我是否做事正确吗?

Can someone tell me whether I'm doing things in a right way??

推荐答案

您可以查看Quartz.NET zip发行版随附的Quartz.Server示例.也可以直接在GitHub上获得:

You could have a look at the Quartz.Server sample that comes with the Quartz.NET zip distribution. Also available straight on GitHub:

https://github.com/quartznet/quartznet/tree/主服务器/服务器/Quartz.Server

您应该共享同一调度程序.仅创建一次调度程序工厂,然后从那里获取单例调度程序.

You should share the same scheduler. Only create scheduler factory once and get the singleton scheduler from there.

您可以将您的作业仅添加一次到持久性中,并通过触发作业数据映射对其进行参数化.然后,您可以访问合并的作业,并从作业执行上下文随附的MergedJobDataMap触发数据.

You could have your job added just once to scheduler as durable and parameterize it with trigger job data map. You can then access the combined job and trigger data from MergedJobDataMap that comes with job execution context.

每次触发都会实例化作业的新实例,因此作业详细信息在此处充当模板.

Every triggering instantiates a new instance of the job so job detail acts as an template here.

请注意,DateTime.Now可能不是您想要的.对于Quartz.NET,现在"是DateTime.UtcNow(甚至更好的是DateTimeOffset.UtcNow).

Please note that DateTime.Now might not be what you want. For Quartz.NET 'now' is DateTime.UtcNow (or even better DateTimeOffset.UtcNow).

这篇关于使用Quartz调度作业-需要意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆