【发布时间】:2014-08-14 03:38:46
【问题描述】:
我正在尝试使用 C# 使用 Quartz.NET 和 Topshelf 运行多个作业。
HostFactory.Run(c =>
{
c.ScheduleQuartzJobAsService(q =>
q.WithJob(() => JobBuilder.Create<TypeA>().Build())
.AddTrigger(() => TriggerBuilder.Create().WithSimpleSchedule(builder => builder.WithIntervalInSeconds(ConfigurationSettings.AppFrequencyInSeconds).RepeatForever()).Build())
).StartAutomatically().
ScheduleQuartzJobAsService(r =>
r.WithJob(() => JobBuilder.Create<TypeB>().Build())
.AddTrigger(() => TriggerBuilder.Create().WithSimpleSchedule(builder => builder.
WithIntervalInSeconds(ConfigurationSettings.AppFrequencyInSeconds).RepeatForever()).Build())
).StartAutomatically();
c.StartAutomatically();
c.SetServiceName("ServiceName");
});
使用上面的代码,只有TypeB中的execute方法被执行。我也试过:
HostFactory.Run(c =>
{
c.ScheduleQuartzJobAsService(q =>
q.WithJob(() => JobBuilder.Create<TypeA>().Build())
.AddTrigger(() => TriggerBuilder.Create().WithSimpleSchedule(builder => builder.
WithIntervalInSeconds(ConfigurationSettings.AppFrequencyInSeconds).RepeatForever()).Build())
).StartAutomatically();
c.StartAutomatically();
c.SetServiceName("Service1");
c.ScheduleQuartzJobAsService(r =>
r.WithJob(() => JobBuilder.Create<TypeB>().Build())
.AddTrigger(() => TriggerBuilder.Create().WithSimpleSchedule(builder => builder.
WithIntervalInSeconds(ConfigurationSettings.AppFrequencyInSeconds).RepeatForever()).Build())
).StartAutomatically();
c.StartAutomatically();
c.SetServiceName("Service2");
});
使用此代码,仅调用 TypeB 中的 execute 方法。我的 TypeA 和 TypeB 类都有“执行”方法,它们是每个类的入口点(如果它们本身是作业的一部分,它们会被调用)。似乎第二个服务代码总是被调用 - 如果我交换这两个 ScheduleQuartzJobAsService 调用的顺序,它总是执行第二个调用中的类。
如何编写我的 HostFactory.Run 方法以便同时执行两个作业?
【问题讨论】:
-
我发现第二种方法永远行不通,就像"You can only have ONE service! As of 3.x Topshelf the base product no longer support hosting multiple services."。因此,我想我需要知道的是如何在一个服务中运行多个作业。
标签: c# quartz-scheduler quartz.net topshelf