Ejemplo n.º 1
0
 static void Main(string[] args)
 {
     QuartzScheduleJobManager.RegisterTask();
     HostFactory.Run(x =>
     {
         x.Service <QuartzScheduleJobManager>(s =>
         {
             s.ConstructUsing(name => new QuartzScheduleJobManager());
             s.WhenStarted(tc => tc.Start());
             s.WhenStopped(tc => tc.Stop(true));
         });
         x.RunAsLocalSystem();          //表示以本地系统账号运行,可选的还有网络服务和本地服务账号
         x.StartAutomatically();        //自动启动
         x.SetDescription("服务测试描述");    //描述
         x.SetDisplayName("服务测试-显示名称"); //显示名称
         x.SetServiceName("服务测试-服务名称"); //服务名称
     });
 }
        /// <summary>
        /// 注册定时任务
        /// </summary>
        public static void RegisterTask()
        {
            var typeFinder = new TypeFinder();
            //查找定时任务
            var taskTypes     = typeFinder.FindClassesOfType <ITask>();
            var taskInstances = new List <ITask>();

            foreach (var taskType in taskTypes)
            {
                taskInstances.Add((ITask)Activator.CreateInstance(taskType));
            }

            var jobManager = new QuartzScheduleJobManager();

            foreach (var item in taskInstances)
            {
                var        type    = item.GetType();
                IJobDetail job     = JobBuilder.Create(type).WithIdentity("job_" + type.Name, "group_" + type.Name).Build();
                ITrigger   trigger = item.GetTrigger()
                                     .WithIdentity("trigger_" + type.Name, "group_" + type.Name)
                                     .Build();
                jobManager.Scheduler.ScheduleJob(job, trigger);
            }
        }