【问题标题】:Decouple roles in Quartz.netQuartz.net 中的解耦角色
【发布时间】:2013-06-09 14:41:21
【问题描述】:

我目前正在研究使用 Quartz.NET 在我的系统中安排任务。作为我如何使用 Quartz.NET 的示例,下面是一个非常简单的示例,演示了我如何调度任务:

class Program
{
    static void Main(string[] args)
    {
        var properties = new NameValueCollection();
        properties["quartz.scheduler.instanceName"] = "TestScheduler";
        properties["quartz.scheduler.instanceId"] = "instance_one";
        properties["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz";
        properties["quartz.jobStore.useProperties"] = "true";
        properties["quartz.jobStore.dataSource"] = "default";
        properties["quartz.jobStore.tablePrefix"] = "QRTZ_";
        properties["quartz.jobStore.lockHandler.type"] = "Quartz.Impl.AdoJobStore.UpdateLockRowSemaphore, Quartz";
        properties["quartz.dataSource.default.connectionString"] = "Server=.\\SqlExpress;Database=quartz;Trusted_Connection=True;";
        properties["quartz.dataSource.default.provider"] = "SqlServer-20";

        var scheduler = new StdSchedulerFactory(properties).GetScheduler();

        scheduler.Start();

        TriggerSimpleJob(scheduler);

        Console.WriteLine("Waiting For Job");
        Console.ReadLine();
    }

    private static void TriggerSimpleJob(IScheduler scheduler)
    {
        ITrigger trigger = TriggerBuilder.Create()
                              .WithIdentity("trigger1", "group1")
                              .StartAt(DateBuilder.EvenSecondDateAfterNow())
                              .UsingJobData("myTriggerParameter", "myTriggerValue")
                              .UsingJobData("myParameter", "triggerParameter")
                              .Build();

        IJobDetail jobDetail = JobBuilder.Create<SimpleJob>().WithIdentity("job1", "group1")
            .UsingJobData("myParameter", "myValue")
            .Build();

        scheduler.ScheduleJob(jobDetail, trigger);
    }
}

public class SimpleJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {
       Console.WriteLine("Job completed");
    }
}

我的问题是这样的:

我想将作业的调度与作业的执行分离。

在上面的例子中,在作业被调度之后,如果在调度时间到达时进程仍在运行,那么作业就在这个进程中执行。理想情况下,我希望能够拥有一个运行 Quartz.NET 调度程序实例的专用服务器,该实例专用于执行作业,并且能够在知道作业将在此专用服务器上执行的其他进程中安排作业。

我尝试在调度作业的进程上简单地将属性“quartz.threadPool.threadCount”设置为“0”,但这会引发异常。调度程序上是否有任何配置属性可以实现我想要做的事情?

【问题讨论】:

    标签: c# scheduled-tasks scheduling quartz.net


    【解决方案1】:

    早上好,

    你可以阅读我的回答here

    我建议使用ADO.NET Job Store(看来您正在使用它)。 应配置负责调度作业的应用程序,将属性 threadPool 设置为 ZeroSizeThreadPool

    properties["quartz.threadPool.type"] = "Quartz.Simpl.ZeroSizeThreadPool, Quartz";
    

    你可以阅读更多关于这种类型的线程池here

    应使用以下设置配置负责执行作业的应用程序:

    properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
    properties["quartz.threadPool.threadCount"] = "10";
    properties["quartz.threadPool.threadPriority"] = "Normal";
    

    【讨论】:

    • 完美——正是我想要的。谢谢。
    【解决方案2】:

    删除线

     scheduler.Start();
    

    【讨论】:

      猜你喜欢
      • 2018-12-30
      • 2015-02-03
      • 1970-01-01
      • 2021-03-30
      • 1970-01-01
      • 2013-09-14
      • 1970-01-01
      • 1970-01-01
      • 2013-04-04
      相关资源
      最近更新 更多