使用简单的xml来驱动Quartz Scheduler

时间:2012-09-15 09:56:54

标签: quartz-scheduler quartz.net

有人可以查看我对Quartz xml的简单测试(每秒触发),并告诉我为什么没有工作添加到sheduler?基本上我期待'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.在作业数据图部分,我可以添加多个键/值,但值是什么?我是否添加了可执行文件,或者是否将键/值对用于另一个类,我猜是

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);
            }
        }
    }

}

1 个答案:

答案 0 :(得分: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 * * * *?并触发执行名为my_job1的作业,该作业属于作业组myJobGroup。

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

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

关于第二个问题,JobDataMap会保存您希望在作业实例执行时可用的任何可序列化数据。

  

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

我希望这会有所帮助。