Quartz.net 工作似乎在 windows 服务结束后仍然存在
Quartz.net jobs seem to persist after windows service ended
我是 Quartz.net 的新手,目前只是对它有所了解。我正在从我的 Windows 服务中为 运行 设置一个 Quartz.net 作业。
我的 Windows 服务有两种启动模式 a) 如果项目 运行 作为 windows 服务,它将 运行 作为普通服务。 b) 如果项目在 Visual Studio 中处于调试模式 运行,它将 运行 处于交互模式(这样我就可以将调试信息输出到控制台而不是记录器,具体取决于上下文)。它在 Main() 中按如下方式执行此操作(只是摘录):
if (Environment.UserInteractive && System.Diagnostics.Debugger.IsAttached) {
System.Reflection.MethodInfo onStartMethod = typeof(ServiceBase).GetMethod("OnStart", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
onStartMethod.Invoke(myService, new object[] { new string[] { } });
Console.WriteLine("Service started.");
Console.WriteLine("Press a key to stop service and finish process...");
Console.ReadKey();
System.Reflection.MethodInfo onStopMethod = typeof(ServiceBase).GetMethod("OnStop", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
onStopMethod.Invoke(myService, null);
Console.WriteLine("Service stopped.");
} else {
ServiceBase.Run(myService);
}
现在在 MyService 中我有以下工作,并且这项工作似乎如预期的那样 运行 正确:
protected override void OnStart(string[] args)
{
_schedulerFactory = new StdSchedulerFactory();
IScheduler scheduler = _schedulerFactory.GetScheduler();
scheduler.Start();
IJobDetail syncJob = JobBuilder.Create<MySyncJob>()
.WithIdentity("syncJob")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.StartAt(new DateTimeOffset(DateTime.UtcNow.AddSeconds(5)))
.WithSimpleSchedule(x => x.WithIntervalInSeconds(5).RepeatForever())
.Build();
scheduler.ScheduleJob(syncJob, trigger);
}
然而,当我 运行 我的项目在 Visual Studio 处于调试模式时,当我停止服务时,控制台显示 "Service stopped",但 Quartz.net syncJob 似乎保持 运行ning(控制台输出继续)为什么会这样?
您需要更新 OnStop
方法以在服务停止时关闭调度程序。
private ISchedulerFactory _schedulerFactory;
private IScheduler _scheduler;
protected override void OnStart(string[] args)
{
_schedulerFactory = new StdSchedulerFactory();
_scheduler = _schedulerFactory.GetScheduler();
_scheduler.Start();
IJobDetail syncJob = JobBuilder.Create<MySyncJob>()
.WithIdentity("syncJob")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.StartAt(new DateTimeOffset(DateTime.UtcNow.AddSeconds(5)))
.WithSimpleSchedule(x => x.WithIntervalInSeconds(5).RepeatForever())
.Build();
scheduler.ScheduleJob(syncJob, trigger);
}
protected override void OnStop()
{
// true parameter indicates whether to wait for running jobs
// to complete before completely tearing down the scheduler
// change to false to force running jobs to abort.
_scheduler.Shutdown(true);
}
作为旁注,您可能需要考虑使用 Topshelf 来帮助处理 windows 服务的拆卸和设置,而无需在问题开头显示所有样板位。
我是 Quartz.net 的新手,目前只是对它有所了解。我正在从我的 Windows 服务中为 运行 设置一个 Quartz.net 作业。
我的 Windows 服务有两种启动模式 a) 如果项目 运行 作为 windows 服务,它将 运行 作为普通服务。 b) 如果项目在 Visual Studio 中处于调试模式 运行,它将 运行 处于交互模式(这样我就可以将调试信息输出到控制台而不是记录器,具体取决于上下文)。它在 Main() 中按如下方式执行此操作(只是摘录):
if (Environment.UserInteractive && System.Diagnostics.Debugger.IsAttached) {
System.Reflection.MethodInfo onStartMethod = typeof(ServiceBase).GetMethod("OnStart", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
onStartMethod.Invoke(myService, new object[] { new string[] { } });
Console.WriteLine("Service started.");
Console.WriteLine("Press a key to stop service and finish process...");
Console.ReadKey();
System.Reflection.MethodInfo onStopMethod = typeof(ServiceBase).GetMethod("OnStop", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
onStopMethod.Invoke(myService, null);
Console.WriteLine("Service stopped.");
} else {
ServiceBase.Run(myService);
}
现在在 MyService 中我有以下工作,并且这项工作似乎如预期的那样 运行 正确:
protected override void OnStart(string[] args)
{
_schedulerFactory = new StdSchedulerFactory();
IScheduler scheduler = _schedulerFactory.GetScheduler();
scheduler.Start();
IJobDetail syncJob = JobBuilder.Create<MySyncJob>()
.WithIdentity("syncJob")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.StartAt(new DateTimeOffset(DateTime.UtcNow.AddSeconds(5)))
.WithSimpleSchedule(x => x.WithIntervalInSeconds(5).RepeatForever())
.Build();
scheduler.ScheduleJob(syncJob, trigger);
}
然而,当我 运行 我的项目在 Visual Studio 处于调试模式时,当我停止服务时,控制台显示 "Service stopped",但 Quartz.net syncJob 似乎保持 运行ning(控制台输出继续)为什么会这样?
您需要更新 OnStop
方法以在服务停止时关闭调度程序。
private ISchedulerFactory _schedulerFactory;
private IScheduler _scheduler;
protected override void OnStart(string[] args)
{
_schedulerFactory = new StdSchedulerFactory();
_scheduler = _schedulerFactory.GetScheduler();
_scheduler.Start();
IJobDetail syncJob = JobBuilder.Create<MySyncJob>()
.WithIdentity("syncJob")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.StartAt(new DateTimeOffset(DateTime.UtcNow.AddSeconds(5)))
.WithSimpleSchedule(x => x.WithIntervalInSeconds(5).RepeatForever())
.Build();
scheduler.ScheduleJob(syncJob, trigger);
}
protected override void OnStop()
{
// true parameter indicates whether to wait for running jobs
// to complete before completely tearing down the scheduler
// change to false to force running jobs to abort.
_scheduler.Shutdown(true);
}
作为旁注,您可能需要考虑使用 Topshelf 来帮助处理 windows 服务的拆卸和设置,而无需在问题开头显示所有样板位。