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

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

   

   

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

金沙注册送58 1

 

 

金沙注册送58 2

image.png

  在常常的费用中,运转定时任务基本晚春经是很广泛的必要了,能够通过windows服务+timer组件来贯彻,也得以动用第叁方框架来集成,Quartz.NET就是壹款从JAVA的Quartz移植过来的八个不利的功课调度组件,然而当大家把作业都写好,并计划到位的时候,管理变为了很劳苦的作业,因而小编依照Quartz.NET,又简便做了弹指间打包,来兑现作业动态管理。

  在平常的开发中,运维定时职责基本淑节经是很常见的要求了,能够通过windows服务+timer组件来完结,也足以应用第贰方框架来集成,Quartz.NET正是壹款从JAVA的Quartz移植过来的二个正确的学业调度组件,可是当大家把作业都写好,并布置形成的时候,管理变为了很麻烦的事务,因而笔者依据Quartz.NET,又简便做了1晃包裹,来贯彻作业动态管理。

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方法。

}
</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
NET塑造和谐的动态作业调度器,net的简要包装。{
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类,类内惟有2个不难易行的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正是每1个学业,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中。

      

      

上边我们新增三个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>

  

金沙注册送58 ,  

 

<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 ,里面唯有3个Run()方法

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

在AddJob时,将Job传入JobData中。

金沙注册送58 3

  之后我们兑现的每多少个功课都以三番五次自这些类,达成Run()方法即可(各类作业都看成2个独立的类库,引用那一个只有一个类的类库)

  之后大家完结的每一个学业都以继承自那么些类,实现Run()方法即可(各类作业都当做3个独自的类库,引用这一个只有多个类的类库)

下边我们开始展览调用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

 

  

  

金沙注册送58 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,然后创设二个JobRuntime对象,添加到JobPoolManager里,JobPoolManager里维护有着的正在运作的Job

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

  五.基于JobInfo.TaskCron(时间表达式)创立Trigger,成立1个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();
            }
        }
    }

 

 

  

  

  然后我们除了做了二个web版的上传界面之外,仍可以够做有所的job列表,用来做Start|Stop|Restart等,思路就是揭发一条广播给拥有的学业调度框架,作业调度框架根据广播信息来开始展览学业的装载,运营,结束,卸载等操作。

  然后大家除了做了贰个web版的上传界面之外,还能够做有所的job列表,用来做Start|Stop|Restart等,思路正是公布一条广播给拥有的学业调度框架,作业调度框架根据广播新闻来进行学业的装载,运行,停止,卸载等操作。

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

  至此,叁个骨干的动态作业调度框架就甘休了。

 

 

 

 

 

 

  

  

 

 

相关文章

网站地图xml地图