【发布时间】:2019-11-05 07:42:38
【问题描述】:
我最近开始使用quartz.net,我有一个关于线程安全的问题。
public class QuartzService
{
public async Task Start()
{
// construct a scheduler factory
NameValueCollection props = new NameValueCollection
{
{ "quartz.serializer.type", "binary" }
};
StdSchedulerFactory factory = new StdSchedulerFactory(props);
// get a scheduler
IScheduler sched = await factory.GetScheduler();
// define the job and tie it to our HelloJob class
IJobDetail job = JobBuilder.Create<TestJob>().StoreDurably()
.WithIdentity("myJob", "jobGroup1")
.Build();
await sched.AddJob(job, true);
// Trigger the job to run now, and then every 40 seconds
ITrigger trigger1 = TriggerBuilder.Create()
.WithIdentity("myTrigger1", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(5)
.RepeatForever())
.ForJob(job)
.Build();
// Trigger the job to run now, and then every 40 seconds
ITrigger trigger2 = TriggerBuilder.Create()
.WithIdentity("myTrigger2", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(5)
.RepeatForever())
.ForJob(job)
.Build();
await sched.ScheduleJob(trigger1);
await sched.ScheduleJob(trigger2);
await sched.Start();
}
}
public class TestJob : IJob
{
public async Task Execute(IJobExecutionContext context)
{
await Console.Out.WriteLineAsync($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
}
}
在上面的例子中,我有一个有两个触发器的工作。我的问题是这两个触发器在运行时是否共享同一个作业实例?或者每次触发器运行时都会创建一个新的 IJobDetail 实例。我曾尝试阅读quartz.net 的文档,但它非常混乱,因为它将 JobDetail 实例与作业实例混合在一起,而且我不太清楚这里的情况。
【问题讨论】:
标签: c# .net .net-core quartz-scheduler quartz.net