zoukankan      html  css  js  c++  java
  • 在ASP.NET MVC4中使用Quartz.NET执行定时任务

    本篇在ASP.NET MVC下实践使用Quartz.NET执行定时任务。

    首先通过NuGet安装Quartz.NET。

    使用Quartz.NET的大致思路是:
    1、实现IJob接口,定义具体要做的事情
    2、使用Quartz.NET的API定义定时任务规则
    3、在Application_Start注册定时任务

    实现IJob接口。

        public class MyJob : IJob
    
        {
    
            public void Execute(IJobExecutionContext context)
    
            {
    
                Debug.WriteLine("Hello at " + DateTime.Now.ToString());
    
            }
    
        }
    

    在Global.asax中定义规则,并在Application_Start中注册。

           protected void Application_Start()
    
            {
    
                AreaRegistration.RegisterAllAreas();
    
                WebApiConfig.Register(GlobalConfiguration.Configuration);
    
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    
                RouteConfig.RegisterRoutes(RouteTable.Routes);
    
                BundleConfig.RegisterBundles(BundleTable.Bundles);
    
                MyJobs();
    
                
    
            }
    
            private static void MyJobs()
    
            {
    
                //工厂
    
                ISchedulerFactory factory = new StdSchedulerFactory();
    
                //启动
    
                IScheduler scheduler = factory.GetScheduler();
    
                scheduler.Start();
    
                //描述工作
    
                IJobDetail jobDetail = new JobDetailImpl("mylittlejob",null, typeof(MyJob));
    
                //触发器
    
                ISimpleTrigger trigger = new SimpleTriggerImpl("mytrigger",
    
                    null,
    
                    DateTime.Now,
    
                    null,
    
                    SimpleTriggerImpl.RepeatIndefinitely,
    
                    TimeSpan.FromSeconds(10));
    
                //执行
    
                scheduler.ScheduleJob(jobDetail, trigger);
    
            }
    

    1

  • 相关阅读:
    CAS配置记录
    线程同步机制
    线程
    异常
    List集合
    数据结构
    泛型+通配符高级使用--受限泛型
    Collection集合+迭代器+foreach循环
    easyui获取日期datebox中的值
    EL表达式与三目运算符
  • 原文地址:https://www.cnblogs.com/darrenji/p/4375003.html
Copyright © 2011-2022 走看看