You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.5 KiB
C#

11 months ago
using log4net;
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
namespace JobScheduler
{
public partial class JobService : ServiceBase
{
private IScheduler scheduler;
private ILog log = LogManager.GetLogger(typeof(JobService));
public JobService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
log.Debug("=========================启动服务=========================");
scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.ListenerManager.AddSchedulerListener(new SchedulerListener());
scheduler.Start();
}
protected override void OnStop()
{
log.Debug("=========================停止服务=========================");
scheduler.Shutdown();
scheduler.Clear();
scheduler = null;
}
}
class SchedulerListener : ISchedulerListener
{
ILog log = LogManager.GetLogger(typeof(SchedulerListener));
public void JobAdded(IJobDetail jobDetail)
{
log.Debug($"JobAdded:{jobDetail.Key}");
}
public void JobDeleted(JobKey jobKey)
{
log.Debug($"JobDeleted:{jobKey}");
}
public void JobPaused(JobKey jobKey)
{
log.Debug($"JobPaused:{jobKey}");
}
public void JobResumed(JobKey jobKey)
{
log.Debug($"JobResumed:{jobKey}");
}
public void JobScheduled(ITrigger trigger)
{
log.Debug($"JobScheduled:{trigger.Key.Name}");
}
public void JobsPaused(string jobGroup)
{
}
public void JobsResumed(string jobGroup)
{
}
public void JobUnscheduled(TriggerKey triggerKey)
{
log.Debug($"JobUnscheduled:{triggerKey.Name}");
}
public void SchedulerError(string msg, SchedulerException cause)
{
log.Error($"SchedulerError:{msg}");
log.Error($"{cause.Message}");
log.Error($"{cause.StackTrace}");
if (cause.InnerException != null)
{
log.Error(cause.InnerException.Message);
log.Error(cause.InnerException.StackTrace);
}
}
public void SchedulerInStandbyMode()
{
}
public void SchedulerShutdown()
{
}
public void SchedulerShuttingdown()
{
}
public void SchedulerStarted()
{
log.Debug($"SchedulerStarted");
}
public void SchedulerStarting()
{
log.Debug($"SchedulerStarting");
}
public void SchedulingDataCleared()
{
log.Debug($"SchedulingDataCleared");
}
public void TriggerFinalized(ITrigger trigger)
{
log.Debug($"TriggerFinalized:{trigger.Key}");
}
public void TriggerPaused(TriggerKey triggerKey)
{
log.Debug($"TriggerPaused:{triggerKey}");
}
public void TriggerResumed(TriggerKey triggerKey)
{
log.Debug($"TriggerResumed:{triggerKey}");
}
public void TriggersPaused(string triggerGroup)
{
}
public void TriggersResumed(string triggerGroup)
{
}
}
}