Quartz.net使用入门(二)

Windows服务+Quartz.net

App.config配置文件

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="JobList">
      <section name="Job" type="Quartz入门Service.MyConfigHandler,Quartz入门Service"/>    
    </sectionGroup>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>

  </startup>
  <JobList>
    <Job>
      <!--这里是一个任务节点-->
      <add key="Url" value="http://www.baidu.com"/>
      <!--需要访问的Url-->
      <add key="Hour" value="10"/>
      <!--开始时间小时-->
      <add key="Minute" value="30"/>
      <!--开始时间分钟-->
    </Job>
  </JobList>
</configuration>

MyService.cs主服务程序:

public partial class MyService : ServiceBase
    {
        public MyService()
        {
            InitializeComponent();
            SystemScheduler _systemScheduler = SystemScheduler.CreateInstance();
            _systemScheduler.StartScheduler();
        }

        protected override void OnStart(string[] args)
        {
            SystemScheduler _systemScheduler = SystemScheduler.CreateInstance();
            _systemScheduler.StartScheduler();
        }

        protected override void OnStop()
        {
        }
    }

SystemScheduler.cs调度程序

  public class SystemScheduler
    {
        private SystemScheduler()
        {
        }

        public static SystemScheduler CreateInstance()
        {
            return new SystemScheduler();
        }

        private IScheduler _scheduler;

        public void StartScheduler()
        {
            //这里读取配置文件中的任务开始时间
            int hour = int.Parse(((NameValueCollection)ConfigurationManager.GetSection("JobList/Job"))["Hour"]);
            int minute = int.Parse(((NameValueCollection)ConfigurationManager.GetSection("JobList/Job"))["Minute"]);

            ISchedulerFactory schedulerFactory = new StdSchedulerFactory();//内存调度
            _scheduler = schedulerFactory.GetScheduler();

            //创建一个Job来执行特定的任务
            IJobDetail synchronousData = new JobDetailImpl("SynchronousData", typeof(SynchronousData));
            //创建并定义触发器的规则(每天执行一次时间为:时:分)
            ITrigger trigger =
                TriggerBuilder.Create()
                    .WithDailyTimeIntervalSchedule(
                        a => a.WithIntervalInHours(24).OnEveryDay().StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(hour, minute))).Build();

            //将创建好的任务和触发规则加入到Quartz中
            _scheduler.ScheduleJob(synchronousData, trigger);
            //开始
            _scheduler.Start();
        }

        public void StopScheduler()
        {
            _scheduler.Shutdown();
        }
    }

SynchronousData.cs同步数据

 public class SynchronousData : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            string Url = ((NameValueCollection)ConfigurationSettings.GetConfig("JobList/Job"))["Url"];
            WebClient wc = new WebClient();
            WebRequest wr = WebRequest.Create(new Uri(Url));
            using (StreamWriter sw = File.AppendText(@"d:\SchedulerService.txt"))
            {
                sw.WriteLine("------------------" + "MyService服务在:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 执行了一次任务" + "------------------");
                sw.Flush();
            }
        }
    }

你可能感兴趣的:(windows,定时器,C#,Quartz-net)