1.定时任务的实现,比如有个任务是要晚上2点10分的时候要去触发的,先定义这个任务类RskBookFilterInitDiningService.cs,这里其实有两种实现,一种是需要继承QuartzJobObject任务类的,比如下面这个,还有一种直接指定执行某个service的某个方法,先说第一种 namespace Bll.ExtFunc { public class RskBookFilterInitDiningService : QuartzJobObject { public string UserName { get; set; } private Spring.Context.IApplicationContext _ctx; protected Spring.Context.IApplicationContext ctx { get { if (_ctx == null) _ctx = Spring.Context.Support.ContextRegistry.GetContext(); return _ctx; } } private IRskBookIFlterInitService _rskBookFilterInitService; public IRskBookIFlterInitService RskBookFilterInitService { get { if (_rskBookFilterInitService == null) { _rskBookFilterInitService = (IRskBookIFlterInitService)ctx["RskBookFilterInitService"]; } return _rskBookFilterInitService; } } protected override void ExecuteInternal(JobExecutionContext context) { //定时任务每天晚上2点左右执行,全量执行 RskBookFilterInitService.SaveAutoInitAll(); } } } 2.需要继承QuartzJobObject的配置文件 2.1任务分发配置,JobType 执行任务的类型定义描述 <object name="Dining" type="Spring.Scheduling.Quartz.JobDetailObject, Spring.Scheduling.Quartz"> <!--RskBookFilter全量初始化--> <property name="JobType" value="Bll.ExtFunc.RskBookFilterInitDiningService, RSKBLL"/> <!-- 注入被调度的对象 --> <property name="JobDataAsMap"> <dictionary> <entry key="UserName" value="admin"/> </dictionary> </property> </object> 2.2 任务分发好了,接下就是选择触发器的触发类型了,这里是CronTriggerObject的实现,每天晚上2点10分启动任务 <object id="cronTrigger" type="Spring.Scheduling.Quartz.CronTriggerObject, Spring.Scheduling.Quartz"> <property name="jobDetail" ref="Dining"/> <!--晚上2:10分的时候自己启动RskBookFilterInitDiningService这个服务 全量初始化--> <property name="cronExpressionString" value="0 10 2 * * ?"/> </object> 2.3 触发器的装载 <object type="Spring.Scheduling.Quartz.SchedulerFactoryObject, Spring.Scheduling.Quartz"> <!--触发器--> <property name="triggers"> <list> <ref object="cronTrigger"/> </list> </property> </object> 好了接下来说另外一种timer,可以指定程序启动多久后开始执行startDelay,还有执行间隔repeatInterval,如我下面演示的会在程序启动120s后,自己启动,每隔10800s也就是每隔3小时循环执行 不说了上配置文件 3.配置文件 3.1 不需继承,可直接指定执行任务的service,以及执行哪个方法 <object id="jobDetail" type="Spring.Scheduling.Quartz.MethodInvokingJobDetailFactoryObject, Spring.Scheduling.Quartz"> <!-- 不必继承IJob接口 --> <property name="TargetObject" ref="RskBookFilterInitService"/> <!--RskBookFilter增量初始化--> <property name="TargetMethod" value="SaveAutoInitIncrement"/> </object> <object id="jobDetailNonConcurrent" type="Spring.Scheduling.Quartz.MethodInvokingJobDetailFactoryObject, Spring.Scheduling.Quartz"> <property name="targetObject" ref="RskBookFilterInitService"/> <property name="targetMethod" value="SaveAutoInitIncrement"/> <!-- 并发等待 --> <property name="concurrent" value="false"/> </object> 3.2 定义触发类型 <object id="simpleTrigger" type="Spring.Scheduling.Quartz.SimpleTriggerObject, Spring.Scheduling.Quartz"> <property name="jobDetail" ref="jobDetail"/> <!-- 120s后启动 --> <property name="startDelay" value="120s"/> <!-- 每3个小时更新一次 增量初始化--> <property name="repeatInterval" value="10800s"/> </object> 3.3 装载触发器,可以装载多个触发器 <object type="Spring.Scheduling.Quartz.SchedulerFactoryObject, Spring.Scheduling.Quartz"> <!--触发器--> <property name="triggers"> <list> <ref object="simpleTrigger"/> <ref object="cronTrigger"/> </list> </property> </object>