Performing Multiple Quartz.NET Jobs Using Topshelf

I am trying to run several jobs with Quartz.NET and Topshelf using C #.

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

Using the code above, only the execute method in TypeB is executed. I also tried:

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

With this code, only the execute method in TypeB is called. Both of my TypeA and TypeB classes have β€œExecute” methods, which are the entry points for each class (which are called if they are part of the job on their own). It seems that whatever service code is second, it is the one that is always called - if I replace the order of these two calls to the ScheduleQuartzJobAsService, it is always a class in the second call made.

How can I write my HostFactory.Run method so that both jobs run simultaneously?

+6
source share
1 answer
 HostFactory.Run(c => { c.Service<ContainerService>(s => { s.ConstructUsing(name => new ContainerService()); s.WhenStarted((service, control) => service.Start()); s.WhenStopped((service, control) => service.Stop()); s.ScheduleQuartzJob<ContainerService>(q => q.WithJob(() => JobBuilder.Create<TypeA>().Build()) .AddTrigger(() => TriggerBuilder.Create() .WithSimpleSchedule(builder => builder .WithIntervalInSeconds(20) .RepeatForever()) .Build()) ); s.ScheduleQuartzJob<ContainerService>(q => q.WithJob(() => JobBuilder.Create<TypeB>().Build()) .AddTrigger(() => TriggerBuilder.Create() .WithSimpleSchedule(builder => builder .WithIntervalInSeconds(60) .RepeatForever()) .Build()) ); }); }); 

...

 public class ContainerService { public bool Start() { return true; } public bool Stop() { return true; } } 

My problem was that I was mixing service and work class concepts. As soon as I introduced my ContainerService methods with Start() and Stop() bool, which returned true, and I called ScheduleQuartzJob and not ScheduleQuartzJobAsService , the above code worked for me as my TypeA and TypeB already implemented IJob .

+12
source

Source: https://habr.com/ru/post/973793/


All Articles