如何在Quartz.NET 2.0中使用cron misfire-instruction FireOnceNow和AdoJobStore?

时间:2012-10-26 11:18:01

标签: c# quartz.net crontrigger quartz.net-2.0 misfire-instruction

我在使用AdoJobStore设置并使用XMLSchedulingDataProcessorPlugin时,使用带有错误指令设置的cron触发器设置为FireOnceNow。

cron-expression已设置,因此作业将每1分钟触发一次:0 0/1 * * * ?

作业计划程序设置为从Global.asax.cs开始。

预期行为:如果服务器在作业应该触发但是在下一次触发之前重新启动时停止,那么它应该立即触发一次。

示例:

第一个作业在 00:01:00触发

我在 00:02:00 可以触发之前停止服务器,但在应该启动它几秒后,让我们说 00:02:10

当我重新启动服务器时(在 00:02:10 ),我希望 00:02:00 失误的工作会触发一次然后正常触发行为将继续。

真正发生的是没有任何触发因素。它只是从 00:03:00 继续触发。

我需要做些什么才能让它按预期工作?

感谢您的帮助!

@ Global.asax.cs中:

private IScheduler scheduler;

protected void Application_Start(object sender, EventArgs e)
{
    var factory = new StdSchedulerFactory();
    scheduler = factory.GetScheduler();
    scheduler.Start();
}

@ quartz_jobs.xml:

<?xml version="1.0" encoding="UTF-8"?>
<job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData"
                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                     version="2.0">
  <processing-directives>
    <overwrite-existing-data>true</overwrite-existing-data>
  </processing-directives>
  <schedule>
    <job>
      <name>TestJob</name>
      <group>Default</group>
      <job-type>BO.TestJob, BO</job-type>
      <durable>true</durable>
      <recover>true</recover>
    </job>
    <trigger>
      <cron>
        <name>TestCron</name>
        <group>Default</group>
        <job-name>TestJob</job-name>
        <job-group>Default</job-group>
        <misfire-instruction>FireOnceNow</misfire-instruction>
        <cron-expression>0 0/1 * * * ?</cron-expression>
      </cron>
    </trigger>
  </schedule>
</job-scheduling-data>

@ web.config中:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
    </sectionGroup>
  </configSections>
  <appSettings>
    <add key="log4net.Internal.Debug" value="false"/>
  </appSettings>
  <common>
    <logging>
      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
        <arg key="configType" value="INLINE"/>
        <arg key="configFile" value="Trace/application.log.txt"/>
        <arg key="level" value="ALL" />
      </factoryAdapter>
    </logging>
  </common>
  <log4net>
    <appender name="GeneralLog" type="log4net.Appender.RollingFileAppender">
      <file value="Trace/application.log.txt"/>
      <appendToFile value="true"/>
      <maximumFileSize value="1024KB"/>
      <rollingStyle value="Size"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d{HH:mm:ss} [%t] %-5p %c - %m%n"/>
      </layout>
    </appender>
    <root>
      <level value="ALL"/>
      <appender-ref ref="GeneralLog"/>
    </root>
  </log4net>
  <quartz>
    <add key="quartz.scheduler.instanceName" value="TestQuartzServer" />
    <add key="quartz.scheduler.instanceId" value="instance_one" />
    <add key="quartz.threadPool.threadCount" value="10" />
    <add key="quartz.threadPool.threadPriority" value="Normal" />
    <add key="quartz.jobStore.misfireThreshold" value="3000" />
    <add key="quartz.jobStore.type" value="Quartz.Impl.AdoJobStore.JobStoreTX, Quartz" />
    <add key="quartz.jobStore.driverDelegateType" value="Quartz.Impl.AdoJobStore.StdAdoDelegate, Quartz" />
    <add key="quartz.jobStore.useProperties" value="false" />
    <add key="quartz.jobStore.dataSource" value="default" />
    <add key="quartz.jobStore.tablePrefix" value="qrtz_" />
    <add key="quartz.jobStore.lockHandler.type" value="Quartz.Impl.AdoJobStore.SimpleSemaphore, Quartz" />
    <add key="quartz.dataSource.default.connectionStringName" value="PostgreSqlDb" />
    <add key="quartz.dataSource.default.provider" value="Npgsql-20" />
    <add key="quartz.plugin.xml.type" value="Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz" />
    <add key="quartz.plugin.xml.fileNames" value="~/quartz_jobs.xml" />
    <add key="quartz.plugin.xml.scanInterval" value="10" />
  </quartz>
  <connectionStrings>
    <add name="PostgreSqlDb" connectionString="Server=localhost;database=quartz_net;User ID=*****;Password=*****;" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
</configuration>

@ TestJob.cs:

using System;
using System.Diagnostics;
using Quartz;

namespace BO
{
    public class TestJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Debug.WriteLine("@{0}: {1}", DateTime.Now, context.JobDetail.Key);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

问题在于,quartz_jobs.xml的每次自动处理都会覆盖失火行动正常工作所需的触发数据。

因此,在失火后触发器需要将overwrite-existing-data设置为false:

<processing-directives>
  <overwrite-existing-data>false</overwrite-existing-data>
  <ignore-duplicates>true</ignore-duplicates>
</processing-directives>