zoukankan      html  css  js  c++  java
  • 作业引擎quartz.net --- 监听链

    针对多个作业:如何描述各个跑批任务之间的顺序,紧前、紧后关系,实现灵活调度。例如:A完成则B开始,B完成C开始。

    对quartz.net 进行了查阅,能实现如上业务,如下图:

    测试代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Quartz;
    using Quartz.Impl;
    using Quartz.Impl.Matchers;

    using Quartz.Listener;

    namespace ConsoleApp
    {
    public class quartznetTest
    {
    public static void Run()
    {

    ISchedulerFactory factory = new StdSchedulerFactory();

    // Get scheduler and add object
    IScheduler scheduler = factory.GetScheduler();

    JobKey firstJobKey = JobKey.Create("FirstJob", "Pipeline");
    JobKey secondJobKey = JobKey.Create("SecondJob", "Pipeline");
    JobKey thirdJobKey = JobKey.Create("ThirdJob", "Pipeline");

    // Create job and trigger
    IJobDetail firstJob = JobBuilder.Create<SimpleJob1>()
    .WithIdentity(firstJobKey)
    //.StoreDurably(true)
    .Build();

    IJobDetail secondJob = JobBuilder.Create<SimpleJob2>()
    .WithIdentity(secondJobKey)
    .StoreDurably(true)
    .Build();

    IJobDetail thirdJob = JobBuilder.Create<SimpleJob3>()
    .WithIdentity(thirdJobKey)
    .StoreDurably(true)
    .Build();

    ITrigger firstJobTrigger = TriggerBuilder.Create()
    .WithIdentity("Trigger", "Pipeline")
    .WithSimpleSchedule(x => x
    .WithMisfireHandlingInstructionFireNow()
    .WithIntervalInSeconds(5)
    .RepeatForever())
    .Build();

    JobChainingJobListener listener = new JobChainingJobListener("Pipeline Chain");
    listener.AddJobChainLink(firstJobKey, secondJobKey);
    listener.AddJobChainLink(secondJobKey, thirdJobKey);

    scheduler.ListenerManager.AddJobListener(listener, GroupMatcher<JobKey>.GroupEquals("Pipeline"));

    // Run it all in chain
    scheduler.Start();
    scheduler.ScheduleJob(firstJob, firstJobTrigger);
    scheduler.AddJob(secondJob, false, true);
    scheduler.AddJob(thirdJob, false, true);

    //Console.ReadLine();
    //scheduler.Shutdown();
    //Console.WriteLine("Scheduler shutdown.");
    //Console.WriteLine(history);
    //Console.ReadLine();


    }
    }
    public class SimpleJob1 : IJob
    {
    public virtual void Execute(IJobExecutionContext context)
    {


    JobKey jobKey = context.JobDetail.Key;
    //log.InfoFormat("SimpleJob1 says: {0} executing at {1}", jobKey, DateTime.Now.ToString("r"));
    Console.WriteLine("作业1: {0} executing at {1}", jobKey, DateTime.Now.ToString("r"));
    System.Threading.Thread.Sleep(1000);

    }
    }
    public class SimpleJob2 : IJob
    {

    public virtual void Execute(IJobExecutionContext context)
    {

    // This job simply prints out its job name and the
    // date and time that it is running
    JobKey jobKey = context.JobDetail.Key;
    Console.WriteLine("作业2: {0} executing at {1}", jobKey, System.DateTime.Now.ToString("r"));
    System.Threading.Thread.Sleep(2000);
    }
    }
    public class SimpleJob3 : IJob
    {

    public virtual void Execute(IJobExecutionContext context)
    {
    // This job simply prints out its job name and the
    // date and time that it is running
    JobKey jobKey = context.JobDetail.Key;
    Console.WriteLine("作业3: {0} executing at {1}", jobKey, System.DateTime.Now.ToString("r"));

    }
    }
    }

    注意:需要引用Quartz.dll,Common.Logging.dll,Common.Logging.Core.dll

  • 相关阅读:
    英语语法总结---二、英语中的从句是怎么回事
    【Cocos得知】技术要点通常的积累
    政府采购清单应包括“问题” 积
    Ubuntu通过使用PyCharm 进行调试 Odoo 8.0 可能出现的问题
    Android自己定义组件系列【8】——面膜文字动画
    手机新闻网站,手持移动新闻,手机报client,jQuery Mobile手机新闻网站,手机新闻网站demo,新闻阅读器开发
    OS和android游戏纹理优化和内存优化(cocos2d-x)
    删除重复数据
    MyEclipse2014 设备 checkstyle、PMD、findbugs 最简单的方法 详细说明
    hdu5044 Tree 树链拆分,点细分,刚,非递归版本
  • 原文地址:https://www.cnblogs.com/zhangzhi19861216/p/4756610.html
Copyright © 2011-2022 走看看