使用简单的 xml 驱动 Quartz Sheduler [英] Use simple xml to drive the Quartz Sheduler

查看:131
本文介绍了使用简单的 xml 驱动 Quartz Sheduler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can someone have a look at my simple test of Quartz xml which (fires every second) and give me a clue why no jobs have been added to the sheduler? Basically I'm expecting the 'SimpleJob' class to be fired every second where I can determine which job is being passed and what parameters are being passed in the form of keys - To be honest I'm confused as not enough documentation

<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 部分,我可以添加多个键/值,但值是什么?我是添加可执行文件还是在另一个类中使用键/值对,我猜是

A couple of questions: 1. I want to fire a console app which takes 2 parameters - how would I achieve that? 2. In the job-data-map section I can add multiple key/values but what are the values? Do I add executables or do I use the key/value pair to in another class which I guess is

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

}

推荐答案

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

According your XML configuration you should have inside your source code, a Job named jobName1 which should implement the Job Interface.

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

Below you can find a sample XML configuration:

<?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 触发器,它属于名为 myTriggerGroup 的触发器组,有一个 cron 表达式 1 * * * * ?并触发名为 my_job1 的作业的执行,该作业属于作业组 myJobGroup.

The above configuration represents a Cron Trigger with name my_trigger1 which belongs to a trigger group named myTriggerGroup, has a cron expression 1 * * * * ? and triggers the execution of a job named my_job1 which belongs in the job group myJobGroup.

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

The job my_job1 belongs to a group myJobGroup, the job class is com.example.my_jobs package and has a JobDataMap which holds a data pair with key: key0 and value: value0.

注意位于 job 元素内的 job-class 元素.它应该填写您的作业类的包名称.

Note the job-class element which is located inside the job element. It should be filled with the package name of your job's class.

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

Regarding your second question, the JobDataMap holds any serializable data which you want to make available to the job instance when it executes.

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

You can find the XSDs which instructs the Quartz Scheduler XML configuration files inside the Quartz jar file and specifically in the folder org\quartz\xml. The quartz-2.1.6.jar contains the XSDS: job_scheduling_data_1_8.xsd and job_scheduling_data_2_0.xsd

我希望这会有所帮助.

这篇关于使用简单的 xml 驱动 Quartz Sheduler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆