zoukankan      html  css  js  c++  java
  • Quartz.net 定式调度任务

      再用Quartz 做任务调度作业时,有以下步骤:

    ISchedulerFactory schedFact = new StdSchedulerFactory();

    IScheduler _sched;
    _sched = schedFact.GetScheduler();
    var triggerJobs = new Dictionary<IJobDetail, Quartz.Collection.ISet<ITrigger>>();
    var jobDetail = JobBuilder.Create()
    .WithIdentity("JobMonitorJob", "jobs")
    .OfType(typeof(JobMonitorJob))

    //.OfType(typeof(CommonJob ))

    //.WithDescription("JobMonitorJob")
    //.UsingJobData("type", "QuartzTest.JobMonitorJob")
    //.UsingJobData("method", "Execute")
    .Build();
    var triggers = new Quartz.Collection.HashSet<ITrigger>();
    var trigger = TriggerBuilder.Create()
    .ForJob(jobDetail)
    .StartAt((DateTimeOffset.Now.AddSeconds(10)))
    //.WithSimpleSchedule(x => x.WithIntervalInSeconds(item.Schedule).RepeatForever())
    .WithCronSchedule("0/5 * * * * ?")
    .Build();
    triggers.Add(trigger);

    triggerJobs.Add(jobDetail, triggers);

    _sched.ScheduleJobs(triggerJobs, false);

    _sched.Start();

      

    多任务调度加个中间件

    public class CommonJob : IJob
    {
    private readonly ILog _logger = LogManager.GetLogger(typeof(CommonJob));
    public void Execute(IJobExecutionContext context)
    {
    var map = context.JobDetail.JobDataMap;
    var typeName = (string)map["type"];
    var methodName = (string)map["method"];

    try
    {
    var type = Type.GetType(typeName);
    var method = type.GetMethod(methodName, Type.EmptyTypes);
    method.Invoke(Activator.CreateInstance(type), null);
    }
    catch (Exception ex)
    {
    JobExecutionException e2 = new JobExecutionException(ex);

    e2.RefireImmediately = true;
    throw e2;
    }
    finally
    {
    GC.Collect();
    }
    }
    }

    public class JobMonitorJob :IJob
    {
    private readonly ILog _logger = LogManager.GetLogger(typeof(JobMonitorJob));
    public void Execute(IJobExecutionContext context)
    {
    _logger.Debug("sdfasfdas");
    }
    }

  • 相关阅读:
    python:利用asyncio进行快速抓取
    os.path.exists(path) 和 os.path.lexists(path) 的区别
    isdigit()判断是不是数字
    switf资源
    51cto培训课程
    51cto运维培训课程
    Python: 在Unicode和普通字符串之间转换
    VC++ CopyFile函数使用方法
    Eclipse断点调试
    AFNetworking2.0后 进行Post请求
  • 原文地址:https://www.cnblogs.com/Hobin/p/5976158.html
Copyright © 2011-2022 走看看