Quartz.net 配置和调度

Quartz.net configuration and scheduling

我有一个 WPF 应用程序,用户可以在其中创建数据库中的实体。每个实体都有一些元数据和一个间隔字段。对于每个实体,我想用提供的时间间隔创建一个作业并将它们存储在 AdoJobStore 中。 现在,由于 WPF 应用程序不会总是 运行ning,我想创建一个 Windows 服务,从 AdoJobStore 和 运行 读取作业数据。

所以基本上有这两层。现在我已经在现有数据库中设置了 Quartz 表。我的问题是:

我已经阅读了很多博客,但是这两个主要问题对我来说有点不清楚。我真的很感激一些关于如何实现并可能构建我的解决方案的示例代码。

谢谢

您使用零线程计划程序来安排作业。示例调度程序初始化代码:

var properties = new NameValueCollection();
properties["quartz.scheduler.instanceId"] = "AUTO";
properties["quartz.threadPool.type"] = "Quartz.Simpl.ZeroSizeThreadPool, Quartz";
properties["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz";
properties["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz";
properties["quartz.jobStore.useProperties"] = "true";
properties["quartz.jobStore.dataSource"] = "default";
properties["quartz.jobStore.tablePrefix"] = tablePrefix;
properties["quartz.jobStore.clustered"] = "false";
properties["quartz.dataSource.default.connectionString"] = connectionString;
properties["quartz.dataSource.default.provider"] = "SqlServer-20";
schedFactory = new StdSchedulerFactory(properties);
BaseScheduler = schedFactory.GetScheduler();

示例调度函数:

    protected ITrigger CreateSimpleTrigger(string tName, string tGroup, IJobDetail jd, DateTime startTimeUtc,
        DateTime? endTimeUtc, int repeatCount, TimeSpan repeatInterval, Dictionary<string, string> dataMap, 
        string description = "")
    {
        if (BaseScheduler.GetTrigger(new TriggerKey(tName, tGroup)) != null) return null;

        var st = TriggerBuilder.Create().
            WithIdentity(tName, tGroup).
            UsingJobData(new JobDataMap(dataMap)).
            StartAt(startTimeUtc).
            EndAt(endTimeUtc).
            WithSimpleSchedule(x => x.WithInterval(repeatInterval).WithRepeatCount(repeatCount)).
            WithDescription(description).
            ForJob(jd).
            Build();               
        return st;
    }

显然,您需要在 UI 中提供所有相关字段,并将这些字段的值传递到函数中。一些必填字段的示例屏幕截图:

您的 Windows 服务将在 OnStart() 方法中初始化多线程调度程序,其方式与上面初始化零线程调度程序的方式非常相似,多线程调度程序将监视您的所有触发器数据库并按照这些触发器中的指定启动您的作业。 Quartz.net 将在这方面完成所有繁重的工作。一旦你安排了你的作业和触发器在数据库中,你需要做的就是初始化多线程调度程序,将它连接到包含触发器的数据库,它会继续触发这些作业并执行你的代码,只要服务是 运行.