0

我刚刚开始使用 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));让工作休眠五分钟。问题是,我不希望进程的多个实例同时运行。在当前设置中,我仍然Executing PROCESSING每 60 秒收到一条消息。

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

4

1 回答 1

4

这是你应该用[DisallowConcurrentExecution]属性标记你的工作的地方。这里解释了这背后的推理/行为Quartz.net 调度程序和 IStatefulJob(IStatefulJob 标记接口是 2.0 之前的方式)。

于 2013-02-15T21:13:16.957 回答