DoubleZ
DoubleZ

Reputation: 155

Executing Quartz.NET jobs from a Windows Service

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

Upvotes: 3

Views: 4111

Answers (1)

Thierry
Thierry

Reputation: 1030

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.

Upvotes: 1

Related Questions