威尼斯人线上娱乐

NET创设友好的动态作业调度器,net的回顾包装

13 4月 , 2019  

NET创设友好的动态作业调度器,net的回顾包装。分享二个以前封装的Quartz.net类。

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

   

   

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

威尼斯人线上娱乐 1

 

 

威尼斯人线上娱乐 2

image.png

  在平凡的费用中,运营定时职责基本三月经是很普遍的要求了,能够经过windows服务+timer组件来促成,也足以动用第2方框架来集成,Quartz.NET正是一款从JAVA的Quartz移植过来的三个不易的学业调度组件,然而当我们把作业都写好,并配备到位的时候,管理变为了很麻烦的工作,由此作者依据Quartz.NET,又简便做了1晃封装,来达成作业动态管理。

  在平时的成本中,运行定时职分基本相月经是很常见的供给了,能够透过windows服务+timer组件来完毕,也得以采用第贰方框架来集成,Quartz.NET正是一款从JAVA的Quartz移植过来的1个没有错的课业调度组件,可是当大家把作业都写好,并计划到位的时候,管理改为了很艰苦的业务,由此我依照Quartz.NET,又不难做了1晃包裹,来贯彻作业动态管理。

image.png

添加Quartz.net的引用。
大家新建三个JobBase.cs文件,里面有1个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方法。

}
</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类,类内只有3个简约的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正是每三个作业,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。
新建三个窗体项目,并丰裕对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>
结果如下:

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

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

在AddJob时,将Job传入JobData中。

威尼斯人线上娱乐 3

  之后大家兑现的每三个作业都以一而再自那一个类,完毕Run()方法即可(每个作业都当做3个单独的类库,引用那么些只有二个类的类库)

  之后大家达成的每1个学业都是继承自那几个类,实现Run()方法即可(每种作业都看成二个单身的类库,引用那几个只有三个类的类库)

上边大家进行调用Test。
新建贰个窗体项目,并加上对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; }
    }

 

 

   接下来我们来讲学那个作业是哪些执行的

   接下来我们来教学这么些作业是怎么履行的

  壹.通过二个上传页面把作业类库打包为zip恐怕rar上盛传服务器,并填写Job运营的相关新闻,添加到数据Curry

  一.经过一个上传页面把作业类库打包为zip可能rar上盛传服务器,并填写Job运维的相关音讯,添加到数据Curry

  贰.上传完结之后公布一条广播新闻给拥有的学业调度框架

  二.上传达成以往发布一条广播音信给全数的作业调度框架

  三.作业调度框架接收到广播音信,从数据库获取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,然后创制1个JobRuntime对象,添加到JobPoolManager里,JobPoolManager里维护有着的正在运维的Job

  四.因为作业都持续了BaseJob类,所以AppDomain里的进口程序就是JobInfo.Namespace,反射实例化之后强制转换为BaseJob,然后创设贰个JobRuntime对象,添加到JobPoolManager里,JobPoolManager里维护有着的正在运营的Job

  5.依照JobInfo.TaskCron(时间表明式)创制Trigger,创造一个JobImplement,并在Context里加一个JobId,有限支撑在JobImplement的Run运转的时候能够从JobPoolManager里获取到Job的主干音讯,以及BaseJob的事例,并调用JobRuntime=>BaseJob=>Run()方法来运转实际上的功课

  伍.依据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();
            }
        }
    }

 

 

  

  

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

  然后我们除了做了一个web版的上传界面之外,还是能够做有所的job列表,用来做Start|Stop|Restart等,思路正是发布一条广播给拥有的作业调度框架,作业调度框架依据广播音信来展开作业的装载,运营,甘休,卸载等操作。

  至此,2个核心的动态作业调度框架就结束了。

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

 

 

 

 

 

 

  

  

 

 


相关文章

发表评论

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

网站地图xml地图