zoukankan      html  css  js  c++  java
  • 基于Quartz.Net 的软件热插拔

    需求 平台可以管理所有的定时作业 只要上传dll ,然后不用你管 。 每个dll 就是一个任务

    任务一

    namespace CXY_One
    {
        [PersistJobDataAfterExecution] 
        [DisallowConcurrentExecution]
        public class Task_One : AbstractIjob, IJob
        {
            public Task Execute(IJobExecutionContext context)
            {
    
                return Task.Run(()=> {
    
                    Console.WriteLine("*****美橙科技*1111111111111111111111**********");
                
                });
    
            }
    
            public override JobModel InitJobModel()
            {
                return new JobModel("deflaut", "deflautgroup", "deflaut", "3/5 * * * * ?", new Hashtable());
            }
        }
    }

    任务2

    namespace CXY_Two
    {
        [PersistJobDataAfterExecution] 
        [DisallowConcurrentExecution] 
        public class Task_Two : AbstractIjob, IJob
        {
            public Task Execute(IJobExecutionContext context)
            {
                var ht = context.JobDetail.JobDataMap.Get("data") as Hashtable;
    
            
    
                return Task.Run(() =>
                {
                    foreach (var item in ht)
                    {
                        Console.WriteLine($"item:{item.ToString()}2222222222222222222222222222");
                  
                    }
                 
                });
            }
    
            public override JobModel InitJobModel()
            {
                return new JobModel("abc", "deflautgroup2", "deflaut2", "3/10 * * * * ?", new Hashtable() { { "key1", "你好" }, { "key2", "你好帅" } });
            }
        }
    }
    
    
    
    核心代码
    namespace CNDNS.WorkServices
    {
        public class JobManage
        {
            public async Task RunAsync()
            {
                ///任务放到一个集合中
                List<AbstractIjob> ijobs = new List<AbstractIjob>();
                ///没有界面暂时写死路径
                var path = @"D:345";
                // 策略 读取cxy 的前缀的dll
             string[] fileInfos = Directory.GetFiles(path).Where(f => f.Contains("CXY_")).ToArray();
    //流方式 读取dell
                var assemblyLoadContext = new AssemblyLoadContext(Guid.NewGuid().ToString("N"), true);
    
                Dictionary<string, AbstractIjob> fileInfolist = new Dictionary<string, AbstractIjob>();
                foreach (var item in fileInfos)
                {
                    AbstractIjob abstractIjob;
    
                    using (var fs = new FileStream(item, FileMode.Open, FileAccess.Read))
                    {
    
                        var assembly = assemblyLoadContext.LoadFromStream(fs);
    
                        Type type = assembly.GetExportedTypes().Where(f => f.GetInterfaces().Contains(typeof(IJob))).FirstOrDefault();
                    //反射创建对象
                        abstractIjob = Activator.CreateInstance(type) as AbstractIjob;
                    }
                    FileInfo fileInfo = new FileInfo(item);
                    abstractIjob.FileInfoLastTime = fileInfo.LastWriteTime;
                    fileInfolist.Add(fileInfo.Name, abstractIjob);
                    if (!SchedulerCenter.dllexe.ContainsKey(fileInfo.Name))
                    {
    
                        SchedulerCenter.AddJob(abstractIjob);
                        SchedulerCenter.dllexe[fileInfo.Name] = abstractIjob;
                    }
                    if (SchedulerCenter.dllexe.ContainsKey(fileInfo.Name) && fileInfo.LastWriteTime > SchedulerCenter.dllexe[fileInfo.Name].FileInfoLastTime)
                    {
                        // 先删 
                        SchedulerCenter.RomverJob(abstractIjob);
                        SchedulerCenter.dllexe.Remove(fileInfo.Name);
    
                        // 再加
                        SchedulerCenter.AddJob(abstractIjob);
                        SchedulerCenter.dllexe[fileInfo.Name] = abstractIjob;
                    }
    
                }
                var dllkeys = SchedulerCenter.dllexe.Keys.ToArray();
             
                for (int i = dllkeys.Length - 1; i >= 0; i--)
                {
                 
                    if (!fileInfolist.Keys.Contains(dllkeys[i]))
                    {
                        SchedulerCenter.RomverJob(SchedulerCenter.dllexe[dllkeys[i]]);
                        SchedulerCenter.dllexe.Remove(dllkeys[i]);
                    }
                }
            }
    
    
    
    
        }
    }
    
    
    

    截图
     
  • 相关阅读:
    JavaScript基础学习篇
    js,html,css注释大集合
    JS中的专业术语
    BFC给我的帮助以及对hasLayout的认识
    框架
    PHP echo和print语句
    PHP变量
    PHP语法
    PHP入门
    SQLite学习网址
  • 原文地址:https://www.cnblogs.com/jasontarry/p/15514609.html
Copyright © 2011-2022 走看看