NET营造友好的动态作业调度器,net的差不离包装

分享1个在先封装的Quartz.net类。

新建3个QuartzClass类库项目。nuget控制台输入

   

   

新建三个QuartzClass类库项目。nuget控制台输入

美高梅开户网址 1

 

 

美高梅开户网址 2

image.png

美高梅开户网址,  在平时的付出中,运营定时职责基本桐月经是很普遍的要求了,能够透过windows服务+timer组件来促成,也得以动用第二方框架来集成,Quartz.NET正是一款从JAVA的Quartz移植过来的1个毋庸置疑的学业调度组件,不过当大家把作业都写好,并配备到位的时候,管理变为了很麻烦的作业,由此作者依据Quartz.NET,又简单做了一晃卷入,来达成作业动态管理。

  在普通的支付中,运营定时职责基本晚春经是很常见的供给了,能够经过windows服务+timer组件来完毕,也足以动用第壹方框架来集成,Quartz.NET正是1款从JAVA的Quartz移植过来的三个正确的学业调度组件,可是当我们把作业都写好,并安排到位的时候,管理变为了很麻烦的事务,由此笔者按照Quartz.NET,又简约做了壹晃封装,来兑现作业动态管理。

image.png

添加Quartz.net的引用。
大家新建一个JobBase.cs文件,里面有3个Run方法。之后所有的Job都需后续那个基类,并促成Run方法。
<pre>
namespace LY.QuartzClass
{

  首先作业动态管理蕴涵以下多少个宗旨点

  首先作业动态管理包含以下多少个宗旨点

 

public abstract class JobBase
{
    public abstract void Run();

}
  1. 应用程序动态加载器
  2. 学业管理(运转)池
  3. 动态运维/结束/卸载作业
  1. 应用程序动态加载器
  2. 作业管理(运维)池
  3. 动态运营/甘休/卸载作业

添加Quartz.net的引用。
笔者们新建一个JobBase.cs文件,里面有一个Run方法。之后有所的Job都需后续这些基类,并实现Run方法。

}
NET营造友好的动态作业调度器,net的差不离包装。</pre>

 

 

namespace LY.QuartzClass
{
public abstract class JobBase
{
    public abstract void Run();

}
}

然后新增三个JobInstance.cs类。那一个类主要达成IJob接口。
<pre>
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

  Quzrtz.NET怎么用自身那里就不再讲解了,百度上无数。

  Quzrtz.NET怎么用自己这里就不再讲解了,百度上诸多。

 

namespace LY.QuartzClass
{
public class JobInstance : IJob
{
public void Execute(IJobExecutionContext context)
{
IDictionary<string, object> jobs = context.JobDetail.JobDataMap;
if (jobs != null)
{
foreach (var item in jobs.Values)
{
try
{
JobBase jobObj = item as JobBase;
jobObj.Run();
}
catch
{
}
}
}
}
}
}
</pre>
在JobInstance类的Execute方法中,我们从JobDataMap中取出JobBase实体,并执行Run方法。

 

 

从此今后新增二个JobInstance.cs类。这一个类重点完成IJob接口。

下边大家新增多个JobManager.cs类,类内唯有一个简短的AddJob方法
<pre>
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

  重要有五个主旨模块,Job,Trigger和Schedule,

  首要有多少个焦点模块,Job,Trigger和Schedule,

using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LY.QuartzClass
{
public class JobInstance : IJob
{
public void Execute(IJobExecutionContext context)
{
IDictionary<string, object> jobs = context.JobDetail.JobDataMap;
if (jobs != null)
{
foreach (var item in jobs.Values)
{
try
{
JobBase jobObj = item as JobBase;
jobObj.Run();
}
catch
{
}
}
}
}
}
}

namespace LY.QuartzClass
{
public class JobManage
{
IScheduler schedudler;
public JobManage()
{
schedudler = StdSchedulerFactory.GetDefaultScheduler();
schedudler.Start();
}

     
Job正是每3个学业,Trigger正是作业执行政策(多短期执行二回等),Schedule则把Job和Tigger装载起来

     
Job就是每3个学业,Trigger就是作业执行策略(多久执行一回等),Schedule则把Job和Tigger装载起来

 

    public void AddJob<T>(int Second) where T : JobBase
    {

        JobBase jbobj = Activator.CreateInstance<T>();
        IDictionary<string, object> jbData = new Dictionary<string, object>();
        jbData.Add("name", jbobj);

        IJobDetail job1 = JobBuilder.Create<JobInstance>()
            .SetJobData(new JobDataMap(jbData)).Build();

        ITrigger trigger1 = TriggerBuilder.Create()
            .StartNow()
            .WithSimpleSchedule(x => x.WithIntervalInSeconds(Second)
            .RepeatForever()).Build();

        schedudler.ScheduleJob(job1, trigger1);

    }

   public void AddJob<T>(string rule) where T : JobBase
    {
        JobBase jbInstance = Activator.CreateInstance<T>();
        IDictionary<string, object> jbData = new Dictionary<string, object>();
        jbData.Add("name", jbInstance);

        IJobDetail job1 = JobBuilder.Create<JobInstance>()
            .SetJobData(new JobDataMap(jbData)).Build();

        ITrigger trigger1 = TriggerBuilder.Create()
            .StartNow()
            .WithCronSchedule(rule).Build();

        schedudler.ScheduleJob(job1, trigger1);
    }
}

  Job和Tigger能够随便搭配装载到Schedule里面运营

  Job和Tigger能够私下搭配装载到Schedule里面运维

在JobInstance类的Execute方法中,我们从JobDataMap中取出JobBase实体,并执行Run方法。

}
</pre>
在AddJob时,将Job传入JobData中。

      

      

上边大家新增3个JobManager.cs类,类内唯有二个大约的AddJob方法

上面我们举办调用Test。
新建2个窗体项目,并累加对LY.QuartzClass项指标引用。
新增多个Job类,继承自JobBase
<pre>
public class FirstJob : JobBase
{
public override void Run()
{
Console.WriteLine(DateTime.Now.ToString());
}
}
</pre>

  接下去讲解完成的思绪

  接下去讲解完结的思绪

using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LY.QuartzClass
{
public class JobManage
{
IScheduler schedudler;
public JobManage()
{
schedudler = StdSchedulerFactory.GetDefaultScheduler();
schedudler.Start();
}
    public void AddJob<T>(int Second) where T : JobBase
    {

        JobBase jbobj = Activator.CreateInstance<T>();
        IDictionary<string, object> jbData = new Dictionary<string, object>();
        jbData.Add("name", jbobj);

        IJobDetail job1 = JobBuilder.Create<JobInstance>()
            .SetJobData(new JobDataMap(jbData)).Build();

        ITrigger trigger1 = TriggerBuilder.Create()
            .StartNow()
            .WithSimpleSchedule(x => x.WithIntervalInSeconds(Second)
            .RepeatForever()).Build();

        schedudler.ScheduleJob(job1, trigger1);

    }

   public void AddJob<T>(string rule) where T : JobBase
    {
        JobBase jbInstance = Activator.CreateInstance<T>();
        IDictionary<string, object> jbData = new Dictionary<string, object>();
        jbData.Add("name", jbInstance);

        IJobDetail job1 = JobBuilder.Create<JobInstance>()
            .SetJobData(new JobDataMap(jbData)).Build();

        ITrigger trigger1 = TriggerBuilder.Create()
            .StartNow()
            .WithCronSchedule(rule).Build();

        schedudler.ScheduleJob(job1, trigger1);
    }
}
}

<pre>
public class SecondJob : JobBase
{
public override void Run()
{
Console.WriteLine(DateTime.Now.ToString() + “:Hello”);
}
}
</pre>

  

  

 

<pre>
public class ThirdJob : JobBase
{
public override void Run()
{
Console.WriteLine(DateTime.Now.ToString() + “:CronTest”);
}
}
</pre>
在窗体中开始展览调用
<pre>
private void button1_Click(object sender, EventArgs e)
{
JobManage job = new JobManage();
job.AddJob<FirstJob>(3);
job.AddJob<SecondJob>(6);
job.AddJob<ThirdJob>(“0/5 0-58 9 * * ?”);
}
</pre>
结果如下:

  先定义3个类库,类库只含有二个类,BaseJob ,里面唯有八个Run()方法

  先定义三个类库,类库只含有一个类,BaseJob ,里面只有三个Run()方法

在AddJob时,将Job传入JobData中。

美高梅开户网址 3

  之后大家落到实处的每1个作业都以一而再自这么些类,达成Run()方法即可(各样作业都当做3个单独的类库,引用那么些唯有二个类的类库)

  之后大家贯彻的每二个学业都是继承自那几个类,达成Run()方法即可(各种作业都看成四个单身的类库,引用这么些唯有3个类的类库)

上边大家进行调用Test。
新建3个窗体项目,并加上对LY.QuartzClass项目标引用。
增产多个Job类,继承自JobBase

image.png

  

  

public class FirstJob : JobBase
{
public override void Run()
{
Console.WriteLine(DateTime.Now.ToString());
}
}
public abstract class BaseJob:MarshalByRefObject,IDisposable
{
        public abstract void Run();
}
public abstract class BaseJob:MarshalByRefObject,IDisposable
{
        public abstract void Run();
}

 

  接下去建立大家的学业管理为主类库Job.Service nuget安装Quartz.NET

  接下去建立大家的作业管理骨干类库Job.Service nuget安装Quartz.NET

public class SecondJob : JobBase
{
public override void Run()
{
Console.WriteLine(DateTime.Now.ToString() + ":Hello");
}
}

  然后新建类JobImplement.cs实现Quartz.NET的IJob接口

  然后新建类JobImplement.cs达成Quartz.NET的IJob接口

 

  那样我们就能够在里面通过大家温馨写的课业调度容器获取到动态加载的Job消息,并运转Job的run方法,来促成动态调度了(作业调度容器里的学业如何装载进去的在小说前边讲解)

  那样我们就足以在当中通过我们协调写的功课调度容器获取到动态加载的Job消息,并运营Job的run方法,来落到实处动态调度了(作业调度容器里的课业怎么着装载进去的在篇章前面讲解)

public class ThirdJob : JobBase
{
public override void Run()
{
Console.WriteLine(DateTime.Now.ToString() + ":CronTest");
}
}

   jobRuntimeInfo是我们协调定义的实体类,里面包括了BaseJob,AppDomain,JobInfo
多少个音讯

   jobRuntimeInfo是大家本身定义的实体类,里面含有了BaseJob,AppDomain,JobInfo
多少个音信

 

  JobInfo是作业在上传到作业动态调度框架时所急需填写的功课基本音讯

  JobInfo是作业在上传到作业动态调度框架时所供给填写的功课基本音讯

在窗体中开始展览调用

    

    

private void button1_Click(object sender, EventArgs e)
{
JobManage job = new JobManage();
job.AddJob<FirstJob>(3);
job.AddJob<SecondJob>(6);
job.AddJob<ThirdJob>("0/5 0-58 9 * * ?");
}

  

  

 

public class JobImplement : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                long jobId = context.JobDetail.JobDataMap.GetLong("JobId");
                //从作业调度容器里查找,如果找到,则运行
                var jobRuntimeInfo =  JobPoolManager.Instance.Get(jobId);
try
                    {
                        jobRuntimeInfo.Job.TryRun();
                    }
                    catch (Exception ex)
                    {
                        //写日志,任务调用失败
                        ConnectionFactory.GetInstance<Provider.JobStateRepository>()
                            .Update(new Provider.Tables.JobState()
                            {
                                JobId = jobId,
                                RunState = (int) Provider.DirectiveType.Stop,
                                UpdateTime = DateTime.Now
                            });
                        Common.Logging.LogManager.GetLogger(this.GetType()).Error(ex.Message, ex);
                    }

            }
            catch (Exception ex)
            {
                Common.Logging.LogManager.GetLogger(this.GetType()).Error(ex.Message, ex);
                //调用的时候失败,写日志,这里错误,属于系统级错误,严重错误
            }
        }
    }
public class JobImplement : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                long jobId = context.JobDetail.JobDataMap.GetLong("JobId");
                //从作业调度容器里查找,如果找到,则运行
                var jobRuntimeInfo =  JobPoolManager.Instance.Get(jobId);
try
                    {
                        jobRuntimeInfo.Job.TryRun();
                    }
                    catch (Exception ex)
                    {
                        //写日志,任务调用失败
                        ConnectionFactory.GetInstance<Provider.JobStateRepository>()
                            .Update(new Provider.Tables.JobState()
                            {
                                JobId = jobId,
                                RunState = (int) Provider.DirectiveType.Stop,
                                UpdateTime = DateTime.Now
                            });
                        Common.Logging.LogManager.GetLogger(this.GetType()).Error(ex.Message, ex);
                    }

            }
            catch (Exception ex)
            {
                Common.Logging.LogManager.GetLogger(this.GetType()).Error(ex.Message, ex);
                //调用的时候失败,写日志,这里错误,属于系统级错误,严重错误
            }
        }
    }

结果如下:

 

 

 

  JobRuntimeInfo

  JobRuntimeInfo

 

  

  

美高梅开户网址 4

public class JobRuntimeInfo
    {
        public AppDomain AppDomain;
        public BaseJob Job { get; set; }

        public JobInfo JobModel { get; set; }
    }
public class JobRuntimeInfo
    {
        public AppDomain AppDomain;
        public BaseJob Job { get; set; }

        public JobInfo JobModel { get; set; }
    }

image.png

  JobInfo

  JobInfo

public class JobInfo
    {
        public long JobId { get; set; }
        public string JobName { get; set; }public string TaskCron { get; set; }
        public string Namespace { get; set; }
        public string MainDllName { get; set; }
        public string Remark { get; set; }
        public string ZipFileName { get; set; }

        public string Version { get; set; }

        public DateTime? CreateTime { get; set; }
    }
public class JobInfo
    {
        public long JobId { get; set; }
        public string JobName { get; set; }public string TaskCron { get; set; }
        public string Namespace { get; set; }
        public string MainDllName { get; set; }
        public string Remark { get; set; }
        public string ZipFileName { get; set; }

        public string Version { get; set; }

        public DateTime? CreateTime { get; set; }
    }

 

 

   接下来大家来教学那么些作业是什么样履行的

   接下来大家来讲课那么些作业是如何举行的

  壹.由此3个上传页面把作业类库打包为zip或许rar上传播服务器,并填写Job运转的相干消息,添加到数据Curry

  1.通过3个上传页面把作业类库打包为zip也许rar上传播服务器,并填写Job运维的相干音讯,添加到数据Curry

  2.上传实现未来发表一条广播信息给全体的作业调度框架

  贰.上传完毕之后发布一条广播音讯给持有的功课调度框架

  叁.学业调度框架接收到广播新闻,从数据库获取JobInfo,自动依据上传的时候填写的音讯(见上面包车型客车JobInfo类的性质),自动解压,装载到AppDomain里

  三.学业调度框架接收到广播音信,从数据库获取JobInfo,自动依照上传的时候填写的音讯(见下边包车型客车JobInfo类的特性),自动解压,装载到AppDomain里

public class AppDomainLoader
    {
        /// <summary>
        /// 加载应用程序,获取相应实例
        /// </summary>
        /// <param name="dllPath"></param>
        /// <param name="classPath"></param>
        /// <param name="appDomain"></param>
        /// <returns></returns>
        public static BaseJob Load(string dllPath, string classPath, out AppDomain appDomain) where T : class
        {
            AppDomainSetup setup = new AppDomainSetup();
            if (System.IO.File.Exists($"{dllPath}.config"))
                setup.ConfigurationFile = $"{dllPath}.config";
            setup.ShadowCopyFiles = "true";
            setup.ApplicationBase = System.IO.Path.GetDirectoryName(dllPath);
            appDomain = AppDomain.CreateDomain(System.IO.Path.GetFileName(dllPath), null, setup);
            AppDomain.MonitoringIsEnabled = true;
            BaseJob obj = (BaseJob) appDomain.CreateInstanceFromAndUnwrap(dllPath, classPath);
            return obj;
        }

        /// <summary>
        /// 卸载应用程序
        /// </summary>
        /// <param name="appDomain"></param>
        public static void UnLoad(AppDomain appDomain)
        {
            AppDomain.Unload(appDomain);
            appDomain = null;
        }
    }
public class AppDomainLoader
    {
        /// <summary>
        /// 加载应用程序,获取相应实例
        /// </summary>
        /// <param name="dllPath"></param>
        /// <param name="classPath"></param>
        /// <param name="appDomain"></param>
        /// <returns></returns>
        public static BaseJob Load(string dllPath, string classPath, out AppDomain appDomain) where T : class
        {
            AppDomainSetup setup = new AppDomainSetup();
            if (System.IO.File.Exists($"{dllPath}.config"))
                setup.ConfigurationFile = $"{dllPath}.config";
            setup.ShadowCopyFiles = "true";
            setup.ApplicationBase = System.IO.Path.GetDirectoryName(dllPath);
            appDomain = AppDomain.CreateDomain(System.IO.Path.GetFileName(dllPath), null, setup);
            AppDomain.MonitoringIsEnabled = true;
            BaseJob obj = (BaseJob) appDomain.CreateInstanceFromAndUnwrap(dllPath, classPath);
            return obj;
        }

        /// <summary>
        /// 卸载应用程序
        /// </summary>
        /// <param name="appDomain"></param>
        public static void UnLoad(AppDomain appDomain)
        {
            AppDomain.Unload(appDomain);
            appDomain = null;
        }
    }

 

 

  四.因为作业都卫冕了BaseJob类,所以AppDomain里的入口程序正是JobInfo.Namespace,反射实例化之后强制转换为BaseJob,然后创立二个JobRuntime对象,添加到JobPoolManager里,JobPoolManager里维护有着的正在周转的Job

  4.因为作业都连续了BaseJob类,所以AppDomain里的进口程序正是JobInfo.Namespace,反射实例化之后强制转换为BaseJob,然后成立二个JobRuntime对象,添加到JobPoolManager里,JobPoolManager里维护有着的正在周转的Job

  5.依据JobInfo.TaskCron(时间表达式)创设Trigger,创造1个JobImplement,并在Context里加三个JobId,保险在JobImplement的Run运转的时候能够从JobPoolManager里获取到Job的宗旨新闻,以及BaseJob的事例,并调用JobRuntime=>BaseJob=>Run()方法来运转实际上的课业

  5.依据JobInfo.TaskCron(时间表达式)创建Trigger,创造二个JobImplement,并在Context里加三个JobId,保险在JobImplement的Run运转的时候能够从JobPoolManager里获取到Job的中坚信息,以及BaseJob的例子,并调用JobRuntime=>BaseJob=>Run()方法来运维实际上的作业

  

  

 public class JobPoolManager:IDisposable
    {
        private static ConcurrentDictionary<long, JobRuntimeInfo> JobRuntimePool =
            new ConcurrentDictionary<long, JobRuntimeInfo>();

        private static IScheduler _scheduler;
        private static JobPoolManager _jobPollManager;

        private JobPoolManager(){}

        static JobPoolManager()
        {
            _jobPollManager = new JobPoolManager();
            _scheduler = StdSchedulerFactory.GetDefaultScheduler();
            _scheduler.Start();
        }

        public static JobPoolManager Instance
        {
            get { return _jobPollManager; }

        }


        static object _lock=new object();
        public bool Add(long jobId, JobRuntimeInfo jobRuntimeInfo)
        {
            lock (_lock)
            {
                if (!JobRuntimePool.ContainsKey(jobId))
                {
                    if (JobRuntimePool.TryAdd(jobId, jobRuntimeInfo))
                    {
                        IDictionary<string, object> data = new Dictionary<string, object>()
                        {
                            ["JobId"]=jobId
                        };
                        IJobDetail jobDetail = JobBuilder.Create<JobImplement>()
                            .WithIdentity(jobRuntimeInfo.JobModel.JobName, jobRuntimeInfo.JobModel.Group)
                            .SetJobData(new JobDataMap(data))
                            .Build();
                        var tiggerBuilder = TriggerBuilder.Create()
                            .WithIdentity(jobRuntimeInfo.JobModel.JobName, jobRuntimeInfo.JobModel.Group);
                        if (string.IsNullOrWhiteSpace(jobRuntimeInfo.JobModel.TaskCron))
                        {
                            tiggerBuilder = tiggerBuilder.WithSimpleSchedule((simple) =>
                            {
                                simple.WithInterval(TimeSpan.FromSeconds(1));
                            });
                        }
                        else
                        {
                            tiggerBuilder = tiggerBuilder
                                .StartNow()
                                .WithCronSchedule(jobRuntimeInfo.JobModel.TaskCron);
                        }
                        var trigger = tiggerBuilder.Build();
                        _scheduler.ScheduleJob(jobDetail, trigger);
                        return true;
                    }
                }
                return false;
            }
        }

        public JobRuntimeInfo Get(long jobId)
        {
            if (!JobRuntimePool.ContainsKey(jobId))
            {
                return null;
            }
            lock (_lock)
            {
                if (JobRuntimePool.ContainsKey(jobId))
                {
                    JobRuntimeInfo jobRuntimeInfo = null;
                    JobRuntimePool.TryGetValue(jobId, out jobRuntimeInfo);
                    return jobRuntimeInfo;
                }
                return null;
            }
        }

        public bool Remove(long jobId)
        {
            lock (_lock)
            {
                if (JobRuntimePool.ContainsKey(jobId))
                {
                    JobRuntimeInfo jobRuntimeInfo = null;
                    JobRuntimePool.TryGetValue(jobId, out jobRuntimeInfo);
                    if (jobRuntimeInfo != null)
                    {
                        var tiggerKey = new TriggerKey(jobRuntimeInfo.JobModel.JobName,
                            jobRuntimeInfo.JobModel.Group);
                        _scheduler.PauseTrigger(tiggerKey);

                        _scheduler.UnscheduleJob(tiggerKey);

                        _scheduler.DeleteJob(new JobKey(jobRuntimeInfo.JobModel.JobName,
                            jobRuntimeInfo.JobModel.Group));

                        JobRuntimePool.TryRemove(jobId, out jobRuntimeInfo);

                        return true;
                    }
                }
                return false;
            }
        }

        public virtual void Dispose()
        {
            if (_scheduler != null && !_scheduler.IsShutdown)
            {
                foreach (var jobId in JobRuntimePool.Keys)
                {
                    var jobState = ConnectionFactory.GetInstance<Job.Provider.JobStateRepository>().Get(jobId);
                    if (jobState != null)
                    {
                        jobState.RunState = (int) DirectiveType.Stop;
                        jobState.UpdateTime = DateTime.Now;
                        ConnectionFactory.GetInstance<Job.Provider.JobStateRepository>().Update(jobState);
                    }
                }
                _scheduler.Shutdown();
            }
        }
    }
 public class JobPoolManager:IDisposable
    {
        private static ConcurrentDictionary<long, JobRuntimeInfo> JobRuntimePool =
            new ConcurrentDictionary<long, JobRuntimeInfo>();

        private static IScheduler _scheduler;
        private static JobPoolManager _jobPollManager;

        private JobPoolManager(){}

        static JobPoolManager()
        {
            _jobPollManager = new JobPoolManager();
            _scheduler = StdSchedulerFactory.GetDefaultScheduler();
            _scheduler.Start();
        }

        public static JobPoolManager Instance
        {
            get { return _jobPollManager; }

        }


        static object _lock=new object();
        public bool Add(long jobId, JobRuntimeInfo jobRuntimeInfo)
        {
            lock (_lock)
            {
                if (!JobRuntimePool.ContainsKey(jobId))
                {
                    if (JobRuntimePool.TryAdd(jobId, jobRuntimeInfo))
                    {
                        IDictionary<string, object> data = new Dictionary<string, object>()
                        {
                            ["JobId"]=jobId
                        };
                        IJobDetail jobDetail = JobBuilder.Create<JobImplement>()
                            .WithIdentity(jobRuntimeInfo.JobModel.JobName, jobRuntimeInfo.JobModel.Group)
                            .SetJobData(new JobDataMap(data))
                            .Build();
                        var tiggerBuilder = TriggerBuilder.Create()
                            .WithIdentity(jobRuntimeInfo.JobModel.JobName, jobRuntimeInfo.JobModel.Group);
                        if (string.IsNullOrWhiteSpace(jobRuntimeInfo.JobModel.TaskCron))
                        {
                            tiggerBuilder = tiggerBuilder.WithSimpleSchedule((simple) =>
                            {
                                simple.WithInterval(TimeSpan.FromSeconds(1));
                            });
                        }
                        else
                        {
                            tiggerBuilder = tiggerBuilder
                                .StartNow()
                                .WithCronSchedule(jobRuntimeInfo.JobModel.TaskCron);
                        }
                        var trigger = tiggerBuilder.Build();
                        _scheduler.ScheduleJob(jobDetail, trigger);
                        return true;
                    }
                }
                return false;
            }
        }

        public JobRuntimeInfo Get(long jobId)
        {
            if (!JobRuntimePool.ContainsKey(jobId))
            {
                return null;
            }
            lock (_lock)
            {
                if (JobRuntimePool.ContainsKey(jobId))
                {
                    JobRuntimeInfo jobRuntimeInfo = null;
                    JobRuntimePool.TryGetValue(jobId, out jobRuntimeInfo);
                    return jobRuntimeInfo;
                }
                return null;
            }
        }

        public bool Remove(long jobId)
        {
            lock (_lock)
            {
                if (JobRuntimePool.ContainsKey(jobId))
                {
                    JobRuntimeInfo jobRuntimeInfo = null;
                    JobRuntimePool.TryGetValue(jobId, out jobRuntimeInfo);
                    if (jobRuntimeInfo != null)
                    {
                        var tiggerKey = new TriggerKey(jobRuntimeInfo.JobModel.JobName,
                            jobRuntimeInfo.JobModel.Group);
                        _scheduler.PauseTrigger(tiggerKey);

                        _scheduler.UnscheduleJob(tiggerKey);

                        _scheduler.DeleteJob(new JobKey(jobRuntimeInfo.JobModel.JobName,
                            jobRuntimeInfo.JobModel.Group));

                        JobRuntimePool.TryRemove(jobId, out jobRuntimeInfo);

                        return true;
                    }
                }
                return false;
            }
        }

        public virtual void Dispose()
        {
            if (_scheduler != null && !_scheduler.IsShutdown)
            {
                foreach (var jobId in JobRuntimePool.Keys)
                {
                    var jobState = ConnectionFactory.GetInstance<Job.Provider.JobStateRepository>().Get(jobId);
                    if (jobState != null)
                    {
                        jobState.RunState = (int) DirectiveType.Stop;
                        jobState.UpdateTime = DateTime.Now;
                        ConnectionFactory.GetInstance<Job.Provider.JobStateRepository>().Update(jobState);
                    }
                }
                _scheduler.Shutdown();
            }
        }
    }

 

 

  

  

  然后大家除了做了2个web版的上传界面之外,还足以做有所的job列表,用来做Start|Stop|Restart等,思路正是揭橥一条广播给持有的作业调度框架,作业调度框架依照广播新闻来开始展览学业的装载,运行,停止,卸载等操作。

  然后大家除了做了三个web版的上传界面之外,还足以做有所的job列表,用来做Start|Stop|Restart等,思路就是发表一条广播给拥有的课业调度框架,作业调度框架依据广播新闻来开始展览学业的装载,运营,为止,卸载等操作。

  至此,三个主干的动态作业调度框架就截至了。

  至此,三个基本的动态作业调度框架就终止了。

 

 

 

 

 

 

  

  

 

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图