简体   繁体   English

Windows Service中的Quartz Job Scheduler

[英]Quartz Job Scheduler in Windows Service

I have this windows service project which's OnStart method looks like this 我有这个Windows服务项目,它的OnStart方法看起来像这样

protect void OnStart(string[] args)
{
    IScheduler someScheduler = _schedFactory.GetScheduler(); // _schedFactory is a priva field of the service class

    IJobDetail someJob = JobBuilder.Create<SomeJob>()
        .WithIdentity("SomeJob")
        .Build();

    ITrigger someTrigger = TriggerBuilder.Create()
        .StartAt(new DateTimeOffset(DateTime.UtcNow.AddSeconds(30)))
        .WithSimpleSchedule(schedule => scheduler.WithIntervalInMinutes(3).RepeatForever())
        .Build();

    someScheduler.SchedulerJob(someJob, someTrigger);

    someScheduler.Start();
}

I use Visual Studio Developer Command Prompt to install the service. 我使用Visual Studio Developer命令提示符来安装服务。 The command is installutil.exe . 命令是installutil.exe now when the service is install then I go to task manager and start it. 现在在安装服务时,我进入任务管理器并启动它。 there is Thread.Sleep(10000) in the top of the OnStart method so I could manage to attach to the service with the debugger. OnStart方法的顶部有Thread.Sleep(10000) ,因此我可以使用调试器设法附加到服务。 So when it's attached I go through the code, nothing special happens, I mean no exception happens. 因此,附加它后,我会遍历代码,没有什么特别的事情发生,我的意思是没有例外发生。 I even see the time when the job should be executed and its correct. 我什至看到应该执行作业的时间及其正确性。 while I am sitting in a debug mode and waiting for job's Execute method to get executed, it does not. 当我坐在调试模式下并等待作业的Execute方法执行时,它没有。 I mean when the time comes visual studio is loading symbols but the job itself does not get executed. 我的意思是,当时间到了时,Visual Studio正在加载符号,但作业本身无法执行。 What can be the problem? 可能是什么问题? and one more thing I am creating two jobs in this OnStart method. 还有一件事,我正在此OnStart方法中创建两个作业。 the code for that is the same. 的代码是相同的。 Can it be the cause of the problem ? 可能是问题的原因吗? The second job sometimes get executed sometimes not. 第二项工作有时会执行,有时却无法执行。 I mean if it executes once it will execute after every 3 minute but if it does not executed at the first scheduled time, it never executes. 我的意思是,如果它执行一次,它将每隔3分钟执行一次,但是如果它没有在预定的第一时间执行,则它将永远不会执行。

The problem with your code is that the reference to the scheduler falls out of scope after OnStart finishes running. 您的代码的问题是OnStart完成运行后,对调度程序的引用超出了范围。 SomeScheduler should be defined somewhere outside of the function so that it doesn't get garbage collected. 应该在函数外部的某个位置定义SomeScheduler,以免收集垃圾。 As an example, this is how the Quartz.Net server project does it (using Topshelf but I think you get the idea. This is the main program, that installs the service. Notice it returns a reference to the server so the host can keep a reference to it. 例如,这就是Quartz.Net服务器项目的工作方式(使用Topshelf,但我想您已经明白了。这是安装服务的主程序。请注意,它返回对服务器的引用,以便主机可以保持对它的引用。

public static class Program
{
    /// <summary>
    /// Main.
    /// </summary>
    public static void Main()
    {
        HostFactory.Run(x =>
                            {
                                x.RunAsLocalSystem();

                                x.SetDescription(Configuration.ServiceDescription);
                                x.SetDisplayName(Configuration.ServiceDisplayName);
                                x.SetServiceName(Configuration.ServiceName);

                                x.Service(factory =>
                                              {
                                                  QuartzServer server = new QuartzServer();
                                                  server.Initialize();
                                                  return server;
                                              });
                            });
    }
}

In the QuartzServer class the scheduler is an instance variable: 在QuartzServer类中,调度程序是一个实例变量:

public class QuartzServer : ServiceControl, IQuartzServer
{
    private readonly ILog logger;
    private ISchedulerFactory schedulerFactory;
    private IScheduler scheduler; // code snipped....

}

As @granadaCoder points out, it might be easier to simply re-use the server that is provided. 正如@granadaCoder指出的那样,简单地重用提供的服务器可能会更容易。

Here are links to QuartzServer and Program.cs 这是QuartzServerProgram.cs的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM