zoukankan      html  css  js  c++  java
  • quartz.net 使用(一)-执行定时计划任务

    一、使用nuget安装所需要的包

    Install-Package Quartz

    二、实现自己的job继承IJob

    public class FirstJob : IJob
        {
         
            public void Execute(IJobExecutionContext context)
            {
               
                try
                {               
                    Common.LogHelper.WriteLog(DateTime.Now.ToString() + ":执行计划开始");//写log
                     //你需要执行的计划任务               
                }
                catch (Exception ex)
                {             
                    Common.LogHelper.WriteError(DateTime.Now.ToString() + ":执行计划出现了错误;", ex);               
                    throw;
                }
            }
    

     三、启动计划任务

     public void Start()
            {
                try
                {
                    IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();//获取默认的scheduler
                    string quartzExperssion= ConfigurationManager.AppSettings["QuartzExpression"];
                    if (string.IsNullOrWhiteSpace(quartzExperssion))
                    {
                        quartzExpression = "0 0 2 * * ?";//默认成凌晨两点
                    }
                    scheduler.Start();
                    IJobDetail job = JobBuilder.Create<HistoryDataMoveJob>()
                       .WithIdentity("我的计划", "计划")
                       .Build();
    
                    ITrigger trigger = TriggerBuilder.Create()
                        .WithIdentity("我的计划", "计划")
                        .StartNow()
                        .WithCronSchedule(clocktimeExpression)//0 0 12 * * ?
                        .Build();
    
                    scheduler.ScheduleJob(job, trigger);
                    Common.LogHelper.WriteLog(DateTime.Now.ToString() + ":启动任务计划");
                }
                catch (Exception ex)
                {
                    Common.LogHelper.WriteError(DateTime.Now.ToString() + ":启动计划任务失败", ex);
                }
            }
    

     四、本来是想寄宿在IIS上的,结果发现,IIS设置了闲置回收,quartz进程被IIS回收杀掉了,并没有执行,系统原因不能关掉闲置回收功能,所以将quartz又改造,寄宿到windows服务上

  • 相关阅读:
    Ubuntu安装后root 用户无法使用的解决方法
    struts2 上传 文件 注意问题
    WinSCP无法连接 ubuntu 的解决方法
    TOMCAT 6 中配置HTTPS
    Linux上安装ImageMagick和JMagick
    linux 下 Nginx 0.8.40的安装
    Displaying icons in a Flex List control
    Styling the Alert control’s message text
    Displaying icons in a Flex ComboBox control
    Styling the Alert control’s title bar
  • 原文地址:https://www.cnblogs.com/huanglin101/p/5780684.html
Copyright © 2011-2022 走看看