问题
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>
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);
}
}
}
}
回答1:
According your XML configuration you should have inside your source code, a Job named jobName1 which should implement the Job Interface.
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>
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.
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.
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.
Regarding your second question, the JobDataMap holds any serializable data which you want to make available to the job instance when it executes.
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
I hope this helps.
来源:https://stackoverflow.com/questions/12436369/use-simple-xml-to-drive-the-quartz-sheduler