gpt4 book ai didi

quartz-scheduler - 使用简单的xml来驱动Quartz Scheduler

转载 作者:行者123 更新时间:2023-12-05 04:18:40 31 4
gpt4 key购买 nike

有人可以看看我对 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. 在 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);
}
}
}

最佳答案

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

您可以在下面找到示例 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 触发器,它属于一个名为 myTriggerGroup 的触发器组,具有一个 cron 表达式 1 * * * * ?并触发名为 my_job1 的作业的执行,该作业属于作业组 myJobGroup。

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

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

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

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

希望对您有所帮助。

关于quartz-scheduler - 使用简单的xml来驱动Quartz Scheduler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12436369/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com