Executing Quartz.NET jobs from a Windows Service

僤鯓⒐⒋嵵緔 提交于 2019-12-10 19:32:20

问题


I got a ASP.NET MVC 4 web application and quartz.net running as a windows service and common logging configured according to these sources :

http://geekswithblogs.net/TarunArora/archive/2012/11/16/install-quartz.net-as-a-windows-service-and-test-installation.aspx

http://geekswithblogs.net/TarunArora/archive/2012/11/17/quartz.net-windows-service-configure-logging.aspx

and with this code in Global.asax in place:

        var properties = new NameValueCollection();
        properties["quartz.scheduler.instanceName"] = "ServerScheduler";

        // set thread pool info
        properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
        properties["quartz.threadPool.threadCount"] = "5";
        properties["quartz.threadPool.threadPriority"] = "Normal";

        // set remoting expoter
        properties["quartz.scheduler.proxy"] = "true";
        properties["quartz.scheduler.proxy.address"] = "tcp://localhost:555/QuartzScheduler";
        // construct a scheduler factory
        ISchedulerFactory schedFact = new StdSchedulerFactory(properties);

        // get a scheduler
        IScheduler sched = schedFact.GetScheduler();
        sched.Start();

        IJobDetail jobDetail = JobBuilder.Create<SimpleJob>()
            .WithIdentity("simpleJob", "simpleJobs")
            .RequestRecovery()
            .Build();

        ITrigger trigger = TriggerBuilder.Create()
            .WithIdentity("simpleTrigger", "simpleTriggers")
            .StartNow()
            .WithSimpleSchedule(x => x.WithRepeatCount(4).WithIntervalInSeconds(10))
            .Build();

        sched.ScheduleJob(jobDetail, trigger);

and the job:

    public class SimpleJob : IJob
    {
        public SimpleJob()
    { 

    }

    public void Execute(IJobExecutionContext context)
    {
        Debug.WriteLine("I Executed at " + DateTime.Now.ToString());
    }
}

and now if I run the app the log produces something like this 5 times

    19:35:23 [ServerScheduler_QuartzSchedulerThread] DEBUG    Quartz.Core.QuartzSchedulerThread - Batch acquisition of 1 triggers
    19:35:23 [ServerScheduler_QuartzSchedulerThread] DEBUG Quartz.Simpl.SimpleJobFactory - Producing instance of Job 'simpleJobs.simpleJob', class=Navigate.Quartz.Jobs.SimpleJob
    19:35:23 [ServerScheduler_QuartzSchedulerThread] DEBUG Quartz.Core.QuartzSchedulerThread - Batch acquisition of 1 triggers
    19:35:23 [ServerScheduler_Worker-1] DEBUG Quartz.Core.JobRunShell - Calling Execute on job simpleJobs.simpleJob
    19:35:23 [ServerScheduler_Worker-1] DEBUG Quartz.Core.JobRunShell - Trigger instruction : NoInstruction

Then deletes the trigger and carries on, but no job is executed and no lines are written to debug output

if I run the scheduler embedded in the app, however, by not passing the NameValueCollection to the StdSchedulerFactory

    ISchedulerFactory schedFact = new StdSchedulerFactory();

everything works fine and I get the lines outputted 5 times every 10 seconds

    I Executed at 28.05.2013. 19:47:48
    I Executed at 28.05.2013. 19:47:58
    I Executed at 28.05.2013. 19:48:08
    I Executed at 28.05.2013. 19:48:18
    I Executed at 28.05.2013. 19:48:28

What am I missing, why isnt the windows service actually executing the code, the service is running as Local System, nothing changes if I change it to administrator account tho. Any help will be appreciated.

Chris


回答1:


I think that the service may actually be executing the code but you are not seeing the output. Try changing the Debug.WriteLine() to use Common.Logging so that the output is included in the same log that Quartz is using for its log output. For code examples see http://netcommon.sourceforge.net/docs/1.2.0/reference/html/logging.html.

I also looked at the code that we are using in our implementation, and I see that we are not doing a .Start() after .GetScheduler(). Since you are working with a service running Quartz, the scheduler should already be started, and you should just work with the scheduler that is returned from .GetScheduler(). Try removing the .Start() from your code.



来源:https://stackoverflow.com/questions/16797414/executing-quartz-net-jobs-from-a-windows-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!