net的简易包装,NET构建和谐的动态作业调度器

By admin in 4858.com on 2019年3月29日

享受一个从前封装的Quartz.net类。

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

   

   

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

4858.com 1

 

 

4858.com 2

image.png

  在平凡的支付中,运营定时任务基本十一月经是很常见的须求了,能够经过windows服务+timer组件来完毕,也足以动用第3方框架来集成,Quartz.NET正是一款从JAVA的Quartz移植过来的2个没错的学业调度组件,但是当我们把作业都写好,并配备形成的时候,管理变为了很麻烦的作业,因而作者依照Quartz.NET,又简约做了一下包装,来兑现作业动态管理。

  在平时的开发中,运维定时义务基本辰月经是很常见的供给了,能够经过windows服务+timer组件来完成,也能够动用第1方框架来集成,Quartz.NET便是一款从JAVA的Quartz移植过来的三个正确的学业调度组件,不过当大家把作业都写好,并安顿形成的时候,管理变为了很麻烦的事务,因而作者依据Quartz.NET,又简便做了一下装进,来贯彻作业动态管理。

image.png

添加Quartz.net的引用。
我们新建一个JobBase.cs文件,里面有多个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();

}
}

其后新增2个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();
net的简易包装,NET构建和谐的动态作业调度器。}
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正是每三个功课,Trigger正是作业执行策略(多久执行二回等),Schedule则把Job和Tigger装载起来

     
Job正是每二个学业,Trigger就是作业执行策略(多长期执行2遍等),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中。

      

      

下边大家新增一个JobManager.cs类,类内唯有七个简单易行的AddJob方法

上面大家进行调用Test。
新建3个窗体项目,并丰裕对LY.QuartzClass项指标引用。
新增三个Job类,继承自JobBase
<pre>
4858.com ,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>
结果如下:

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

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

在AddJob时,将Job传入JobData中。

4858.com 3

  之后大家贯彻的每2个功课皆未来续自那一个类,完成Run()方法即可(每一个作业都看作3个单身的类库,引用那几个唯有多少个类的类库)

  之后我们兑现的每三个作业皆未来续自这些类,实现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.瑟维斯 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

 

  

  

4858.com 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; }
    }

 

 

   接下来我们来教学那么些作业是怎么着执行的

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

  1.通过三个上传页面把作业类库打包为zip或然rar上流传服务器,并填写Job运维的有关音讯,添加到数据Curry

  1.经过3个上传页面把作业类库打包为zip可能rar上传播服务器,并填写Job运维的有关信息,添加到数据库里

  2.上传实现以后发表一条广播音讯给持有的作业调度框架

  2.上传达成以后发表一条广播消息给持有的作业调度框架

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

  3.功课调度框架接收到广播新闻,从数据库获取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;
        }
    }

 

 

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

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

  5.依照JobInfo.TaskCron(时间表明式)创造Trigger,创制一个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();
            }
        }
    }

 

 

  

  

  然后大家除了做了三个web版的上传界面之外,还足以做有所的job列表,用来做Start|Stop|Restart等,思路正是公布一条广播给全体的作业调度框架,作业调度框架依照广播音信来开展学业的装载,运转,截至,卸载等操作。

  然后大家除了做了3个web版的上传界面之外,还足以做有所的job列表,用来做Start|Stop|Restart等,思路就是公布一条广播给持有的课业调度框架,作业调度框架依据广播信息来开始展览学业的装载,运行,结束,卸载等操作。

  至此,多个为主的动态作业调度框架就终止了。

  至此,3个主干的动态作业调度框架就结束了。

 

 

 

 

 

 

  

  

 

 

发表评论

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

网站地图xml地图
Copyright @ 2010-2019 美高梅手机版4858 版权所有