2

有人可以看看我对 Quartz xml 的简单测试(每秒触发一次),并给我一个线索,为什么没有工作被添加到调度程序中?基本上我希望'SimpleJob'类每秒被触发一次,我可以确定正在传递哪个作业以及以键的形式传递哪些参数 - 老实说,我很困惑,因为没有足够的文档

<job>
  <name>jobName1</name>
  <group>jobGroup1</group>
  <description>jobDesciption1</description>      
  <job-type>Quartz.Job.NoOpJob, Quartz</job-type>      
  <durable>true</durable>
  <recover>false</recover>
  <job-data-map>
    <entry>
      <key>key0</key>
      <value>value0</value>
    </entry>
    <entry>
      <key>key1</key>
      <value>value1</value>
    </entry>
    <entry>
      <key>key2</key>
      <value>value2</value>
    </entry>
  </job-data-map>
</job>

<trigger>
  <cron>
    <name>simpleName</name>
    <group>simpleGroup</group>
    <description>SimpleTriggerDescription</description>
    <job-name>jobName1</job-name>
    <job-group>jobGroup1</job-group>
    <cron-expression>1 * * * * ?</cron-expression>
    <time-zone></time-zone>
  </cron>
</trigger>

几个问题: 1. 我想启动一个带有 2 个参数的控制台应用程序 - 我将如何实现?2. 在 job-data-map 部分,我可以添加多个键/值,但值是什么?我是添加可执行文件还是在另一个类中使用键/值对,我猜是

class Program
{
    static void Main(string[] args)
    {          

        // First we must get a reference to a scheduler
        NameValueCollection properties = new NameValueCollection();
        properties["quartz.scheduler.instanceName"] = "XmlConfiguredInstance";

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

        // job initialization plugin handles our xml reading, without it defaults are used
        properties["quartz.plugin.xml.type"] = "Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz";
        properties["quartz.plugin.xml.fileNames"] = @"c:\users\paul\documents\visual studio 2010\Projects\ShedulerService\ShedulerApplication\quartz_jobs.xml";  //"~/quartz_jobs.xml";
        ISchedulerFactory sf = new StdSchedulerFactory(properties);
        IScheduler sched = sf.GetScheduler();

        // we need to add calendars manually, lets create a silly sample calendar
        //var dailyCalendar = new DailyCalendar("00:01", "23:59");
        //dailyCalendar.InvertTimeRange = true;
        //sched.AddCalendar("cal1", dailyCalendar, false, false);


        // all jobs and triggers are now in scheduler


        // Start up the scheduler (nothing can actually run until the 
        // scheduler has been started)
        sched.Start();


        // wait long enough so that the scheduler as an opportunity to 
        // fire the triggers           

        try
        {
            Thread.Sleep(30 * 1000);
        }
        catch (ThreadInterruptedException)
        {

        }

        sched.Shutdown(true);
        SchedulerMetaData metaData = sched.GetMetaData();
        Console.WriteLine("Executed " + metaData.NumberOfJobsExecuted + " jobs.");
        Console.Read();
    }



public class SimpleJob : IJob
{
    public virtual void Execute(IJobExecutionContext context)
    {

        // This job simply prints out its job name and the
        // date and time that it is running
        JobKey jobKey = context.JobDetail.Key;

        if (context.MergedJobDataMap.Count > 0)
        {
            ICollection<string> keys = context.MergedJobDataMap.Keys;
            foreach (string key in keys)
            {
                String val = context.MergedJobDataMap.GetString(key);
                //log.InfoFormat(" - jobDataMap entry: {0} = {1}", key, val);
                Console.WriteLine("jobDataMap entry: {0} = {1}", key, val);
            }
        }
    }

}

4

1 回答 1

2

根据您的 XML 配置,您的源代码中应该有一个名为jobName1的作业,它应该实现作业接口。

您可以在下面找到示例 XML 配置:

<?xml version="1.0" encoding="utf-8"?>
<job-scheduling-data version="1.8" xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd">
    <schedule>
        <job>
            <name>my_job1</name>
            <group>myJobGroup</group>
            <description>Example Job</description>
            <job-class>com.example.my_jobs</job-class>
            <job-data-map>
                <entry>
                    <key>key0</key>
                    <value>value0</value>
                </entry>
            </job-data-map>
        </job>
        <trigger>
            <cron>
                <name>my_trigger1</name>
                <group>myTriggerGroup</group>
                <job-name>my_job1</job-name>
                <job-group>myJobGroup</job-group>
                <cron-expression>1 * * * * ?</cron-expression>
            </cron>
        </trigger>
    </schedule>
</job-scheduling-data>

上面的配置代表了一个名为 my_trigger1 的 Cron Trigger,它属于一个名为 myTriggerGroup 的触发器组,有一个 cron 表达式 1 * * * * ? 并触发属于作业组 myJobGroup 的名为 my_job1 的作业的执行。

作业 my_job1 属于一个组 myJobGroup,作业类是 com.example.my_jobs 包,并且有一个 JobDataMap,其中包含键:key0 和值:value0 的数据对。

请注意位于作业元素内的作业元素。它应该填写您的作业类的包名称。

关于您的第二个问题, JobDataMap 包含您希望在作业实例执行时提供给它的任何可序列化数据。

您可以在 Quartz jar 文件中,特别是在文件夹 org\quartz\xml 中找到指示 Quartz Scheduler XML 配置文件的 XSD。quartz-2.1.6.jar 包含 XSDS:job_scheduling_data_1_8.xsd 和 job_scheduling_data_2_0.xsd

我希望这有帮助。

于 2012-09-16T10:19:08.173 回答