石英作业调度程序在Windows服务 [英] Quartz Job Scheduler in Windows Service

查看:152
本文介绍了石英作业调度程序在Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的窗口服务项目,该项目的的OnStart 方法看起来像这样

I have this windows service project which's OnStart method looks like this

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();
}



我用Visual Studio开发人员命令提示符来安装服务。该命令是 installutil.exe 。现在,当服务被安装然后我去任务管理器,并启动它。有 Thread.sleep代码(10000)的OnStart 方法的顶部,所以我可以设法连接到服务与调试。因此,当它连接我通过代码,没有什么特别的情况,我的意思是没有发生异常。我甚至看到在工作时,应执行和它的正确的时间。当我坐在调试模式,等待作业的执行的方法来得到执行,它没有。我的意思是到时候Visual Studio是加载符号,但工作本身没有得到执行。可以采取什么问题?还有一件事我创造这个的OnStart 方法两项工作。对于该代码是相同的。它可以是问题的原因是什么?第二个作业有时会执行有时不。我的意思是,如果它执行一次它会每隔3分钟之后,执行,但如果没有在第一预定时间执行,它永远不会被执行。

I use Visual Studio Developer Command Prompt to install the service. The command is 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. 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. I mean when the time comes visual studio is loading symbols but the job itself does not get executed. What can be the problem? and one more thing I am creating two jobs in this OnStart method. 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.

推荐答案

你的代码的问题是,参考了调度超出范围的OnStart完成运行后。 SomeScheduler应该某处以便它不会垃圾收集的功能之外来限定。作为一个例子,这是Quartz.Net服务器项目是怎么做的(使用Topshelf但我认为你的想法。这是主要的程序,安装服务,注意它返回到服务器的引用使主机能够保持。对它的引用

The problem with your code is that the reference to the scheduler falls out of scope after OnStart finishes running. SomeScheduler should be defined somewhere outside of the function so that it doesn't get garbage collected. 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.

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;
                                              });
                            });
    }
}

在QuartzServer类调度是一个实例变量:

In the QuartzServer class the scheduler is an instance variable:

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

}



由于@granadaCoder点出,它可能更容易简单地重新使用所提供的服务器。

As @granadaCoder points out, it might be easier to simply re-use the server that is provided.

下面是链接到的 QuartzServer 和的 Program.cs的

这篇关于石英作业调度程序在Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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