quartz.net重复计划的任务不运行,除非前一个实例已完成?

时间:2013-02-15 16:56:49

标签: c# scheduling quartz.net

我刚开始使用Quartz.net。我可以通过将以下内容添加到我的app.config

来运行它
<configSections>
    <section name="quartz"
     type="System.Configuration.NameValueSectionHandler, 
         System, Version=1.0.5000.0,Culture=neutral, 
         PublicKeyToken=b77a5c561934e089" />
</configSections>

    

<!-- Configure Thread Pool -->
<add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz" />
<add key="quartz.threadPool.threadCount" value="10" />
<add key="quartz.threadPool.threadPriority" value="Normal" />

<!-- Check for updates to the scheduling every 10 seconds -->
<add key="quartz.plugin.xml.scanInterval" value="10" />

<!-- Configure Job Store -->
<add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz" />
<add key="quartz.plugin.xml.type" value="Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz"/>
<add key="quartz.plugin.xml.fileNames" value="quartz.config" />

我添加了以下Quartz.config:

<?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>ResultProcessor</name>
      <group>Result</group>
      <description>Normalizes results.</description>
      <job-type>TestingNamespace.TestingJob,xxx</job-type>
    </job>

    <trigger>
      <simple>
        <name>ResultProcessorTrigger</name>
        <group>Result</group>
        <description>Trigger for result processor</description>
        <job-name>ResultProcessor</job-name>
        <job-group>Result</job-group>
        <misfire-instruction>SmartPolicy</misfire-instruction>
        <repeat-count>-1</repeat-count>
        <repeat-interval>60000</repeat-interval> <!-- Every 60 seconds -->
      </simple>
    </trigger>
  </schedule>
</job-scheduling-data>

以下类正在执行:

namespace TestingNamespace
{
    class TestingJob: IJob
    {
        protected static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public void Execute(IJobExecutionContext context)
        {
            try
            {
                logger.Info("Executing PROCESSING");
                Thread.Sleep(TimeSpan.FromMinutes(5));
            }
            catch (Exception ex)
            {
                logger.Error("Problem running Execute.", ex);
                throw;
            } // End of catch
        } // End of Run
    } // End of TestingJob
} // End of namespace

正如你在工作中所看到的,我有Thread.Sleep(TimeSpan.FromMinutes(5));让工作睡了五分钟。问题是,我不希望一次运行多个进程实例。在当前设置中,我仍然每60秒收到一条Executing PROCESSING消息。

有没有办法使用Quartz.net让这个工作只在前一个实例完成后运行?

1 个答案:

答案 0 :(得分:4)

这是您应该使用 [DisallowConcurrentExecution] 属性标记作业的地方。这里解释的原因/行为Quartz.net scheduler and IStatefulJob(IStatefulJob标记接口是2.0之前的方法)。