Пример #1
0
 private IQuartzTask Add(QuartzModel quartzModel)
 {
     if (!this.QuartzSchedule.CheckExists(quartzModel))
     {
         return(this.QuartzSchedule.AddJob(quartzModel));
     }
     return(null);
 }
Пример #2
0
        public bool CheckExists(QuartzModel model)
        {
            IQuartzTask model1 = null;
            bool        bl     = this.Scheduler.CheckExists(model.JobDetail.Key);
            bool        b2     = this.QuartzTaskList.TryGetValue(model.JobName, out model1);

            return(bl && b2);
        }
Пример #3
0
        public IQuartzTask GetQuartTask(QuartzModel model)
        {
            //从列表里找到对应的
            QuartzTask quartzTask = null;

            if (this.CheckExists(model))
            {
                quartzTask = new QuartzTask(this, model);
                QuartzTaskList.TryAdd(model.JobName, quartzTask);
            }
            return(quartzTask);
        }
Пример #4
0
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IQuartzTask UpdateJob(QuartzModel oldmodel, QuartzModel newModel)
        {
            IQuartzTask quartzTask = null;

            if (this.CheckExists(oldmodel))
            {
                //需要加QuartzTaskList
                if (this.RemoveJob(oldmodel))
                {
                    quartzTask = AddJob(newModel);
                }
            }
            return(quartzTask);
        }
Пример #5
0
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IQuartzTask AddJob(QuartzModel model)
        {
            QuartzTask quartzTask = null;

            if (!this.CheckExists(model))
            {
                quartzTask = new QuartzTask(this, model);
                if (this.QuartzTaskList.TryAdd(model.JobName, quartzTask))
                {
                    this.Scheduler.ScheduleJob(model.JobDetail, model.Trigger);
                }
            }
            return(quartzTask);
        }
Пример #6
0
        /// <summary>
        /// 两个问题有QuartzModel 有了对象还要这个做什么?二考虑把 QuartzModelList 这个分出去
        /// </summary>
        public bool Update(string jobName)
        {
            if (this.QuartzSchedule == null || string.IsNullOrEmpty(jobName))
            {
                return(false);
            }

            string dllName = jobName + "QuartzTask.dll";
            string path    = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, jobName, dllName);

            byte[]   assemblyBuf = File.ReadAllBytes(path);
            Assembly assembly    = Assembly.Load(assemblyBuf);

            Type type = assembly.GetType(dllName + "." + jobName);

            if (type == null)
            {
                return(false);
            }
            object[] objs = type.GetCustomAttributes(typeof(ScanSignal), true);
            if (objs != null && objs.Length > 0)
            {
                JobBase     job1             = Activator.CreateInstance(type) as JobBase;
                QuartzModel model            = null;
                ScanSignal  customScanSignal = objs[0] as ScanSignal;
                if (customScanSignal != null)
                {
                    IJobDetail job = JobBuilder.Create(type)
                                     .WithIdentity(customScanSignal.JobName, QuartzGroupLevel.GetGroupLevelName(customScanSignal.GroupLevel))
                                     .UsingJobData(new JobDataMap(job1.map))
                                     .Build();
                    TriggerBuilder triggerBuilder = TriggerBuilder.Create()
                                                    .WithIdentity(customScanSignal.JobName + "trigger", QuartzGroupLevel.GetGroupLevelName(customScanSignal.GroupLevel))
                                                    .StartNow();
                    if (String.IsNullOrEmpty(customScanSignal.CronExpression))
                    {
                        triggerBuilder.WithSimpleSchedule(x => x.WithInterval(customScanSignal.TimeSpan).RepeatForever());
                    }
                    else
                    {
                        triggerBuilder.WithCronSchedule(customScanSignal.CronExpression);
                    }
                    ITrigger trigger = triggerBuilder.Build();
                    model = new QuartzModel(customScanSignal.JobName, job, trigger);
                    return(this.QuartzSchedule.UpdateJob(model, model) != null);
                }
            }
            return(false);
        }
Пример #7
0
        public bool RemoveJob(QuartzModel model)
        {
            IQuartzTask model1 = null;

            //if (this.CheckExists(model))
            //{
            //if (this.QuartzTaskList.TryRemove(model.JobName, out model1))
            //{

            //}
            if (this.CheckExists(model))
            {
                this.Scheduler.DeleteJob(model.JobDetail.Key);
            }
            return(this.QuartzTaskList.TryRemove(model.JobName, out model1));

            // }
        }
Пример #8
0
        /// <summary>
        /// 成功返回true ,false 失败有可能是已经存在或找不到jobname
        /// </summary>
        /// <param name="jobName"></param>
        /// <returns></returns>
        public IQuartzTask Add(string jobName)
        {
            if (this.QuartzSchedule == null || String.IsNullOrWhiteSpace(jobName))
            {
                return(null);
            }
            string dllName = jobName + "QuartzTask.dll";
            string path    = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, jobName, dllName);

            string plugName     = string.Concat(jobName, "Plug.txt");
            string plugNamepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, jobName, plugName);
            var    plugdic      = configurationReader.Read(plugNamepath);

            byte[]   assemblyBuf = File.ReadAllBytes(path);
            Assembly assembly    = Assembly.Load(assemblyBuf);

            Type type = assembly.GetType(Path.GetFileNameWithoutExtension(dllName) + "." + jobName);

            if (type.GetInterface("IQuartzService") != null)
            {
                QuartzModel           model         = null;
                AssemblyDynamicLoader dyNamicLoader = new AssemblyDynamicLoader(jobName);
                dyNamicLoader.LoadAssembly(path);

                TaskBase task = new TaskBase();
                IDictionary <string, object> dic = task.map;
                dic.Add("DyNamicLoader", dyNamicLoader);
                dic.Add("DyNamicMethod", Path.GetFileNameWithoutExtension(dllName) + "." + jobName);
                string timePression     = "";
                long   timelongPression = 1000;
                bool   bl = false;
                bl = plugdic.TryGetValue("CRONEXPRESSION", out timePression);
                if (!bl)
                {
                    if (plugdic.TryGetValue("TIMESPAN", out timePression))
                    {
                        if (!long.TryParse(timePression, out timelongPression))
                        {
                            timePression = "1000";
                        }
                    }
                }

                IJobDetail job = JobBuilder.Create(task.GetType())
                                 .WithIdentity(plugdic["JOBNAME"], plugdic["GROUPNAME"])
                                 .UsingJobData(new JobDataMap(dic))
                                 .Build();

                TriggerBuilder triggerBuilder = TriggerBuilder.Create()
                                                .WithIdentity(plugdic["JOBNAME"] + "TRIGGER", plugdic["GROUPNAME"])
                                                .StartNow();
                if (bl)
                {
                    triggerBuilder.WithCronSchedule(timePression);
                }
                else
                {
                    triggerBuilder.WithSimpleSchedule
                        (x => x.WithInterval(new TimeSpan(timelongPression))
                        .RepeatForever());
                }
                ITrigger trigger = triggerBuilder.Build();
                model = new QuartzModel(plugdic["JOBNAME"], job, trigger);
                if (!this.QuartzSchedule.CheckExists(model))
                {
                    this.QuartzSchedule.AddJob(model);
                }
            }
            return(null);
        }
Пример #9
0
        internal void ScanQuartzExterior()
        {
            string runroot = AppDomain.CurrentDomain.BaseDirectory;
            var    subDir  = Directory.GetDirectories(runroot);

            foreach (var dir in subDir)
            {
                var    dinfo        = new DirectoryInfo(dir);
                string dllName      = string.Concat(dinfo.Name, "QuartzTask.dll");
                string path         = Path.Combine(dir, dllName);
                string plugName     = string.Concat(dinfo.Name, "Plug.txt");
                string plugNamepath = Path.Combine(dir, plugName);
                if (!File.Exists(plugNamepath))
                {
                    continue;
                }
                var plugdic = configurationReader.Read(plugNamepath);
                try
                {
                    byte[]   assemblyBuf = File.ReadAllBytes(path);
                    Assembly assembly    = Assembly.Load(assemblyBuf);
                    Type[]   types       = assembly.GetTypes();
                    foreach (var item in types)
                    {
                        if (item.GetInterface("IQuartzService") != null)
                        {
                            QuartzModel           model         = null;
                            AssemblyDynamicLoader dyNamicLoader = new AssemblyDynamicLoader(dinfo.Name);
                            dyNamicLoader.LoadAssembly(path);

                            TaskBase task = new TaskBase();
                            IDictionary <string, object> dic = task.map;
                            dic.Add("DyNamicLoader", dyNamicLoader);
                            dic.Add("DyNamicMethod", Path.GetFileNameWithoutExtension(dllName) + "." + dinfo.Name);
                            string timePression     = "";
                            int    timelongPression = 1000;
                            bool   bl = false;
                            bl = plugdic.TryGetValue("CRONEXPRESSION", out timePression);
                            if (!bl)
                            {
                                if (plugdic.TryGetValue("TIMESPAN", out timePression))
                                {
                                    if (!int.TryParse(timePression, out timelongPression))
                                    {
                                        timePression = "1000";
                                    }
                                }
                            }

                            IJobDetail job = JobBuilder.Create(task.GetType())
                                             .WithIdentity(plugdic["JOBNAME"], plugdic["GROUPNAME"])
                                             .UsingJobData(new JobDataMap(dic))
                                             .Build();

                            TriggerBuilder triggerBuilder = TriggerBuilder.Create()
                                                            .WithIdentity(plugdic["JOBNAME"] + "TRIGGER", plugdic["GROUPNAME"])
                                                            .StartNow();
                            if (bl)
                            {
                                triggerBuilder.WithCronSchedule(timePression);
                            }
                            else
                            {
                                triggerBuilder.WithSimpleSchedule
                                    (x => x.WithInterval(new TimeSpan(0, 0, 0, 0, timelongPression))
                                    .RepeatForever());
                            }
                            ITrigger trigger = triggerBuilder.Build();
                            model = new QuartzModel(plugdic["JOBNAME"], job, trigger);
                            if (!this.QuartzSchedule.CheckExists(model))
                            {
                                this.QuartzSchedule.AddJob(model);
                            }
                        }
                    }
                }
                catch (Exception oe)
                {
                    Debug.WriteLine(oe.Message);
                }
            }
        }
Пример #10
0
        public IQuartzTask Add(PlugModel plug)
        {
            if (this.QuartzSchedule == null || plug == null)
            {
                return(null);
            }

            string runroot = AppDomain.CurrentDomain.BaseDirectory;
            string path    = Path.Combine(
                runroot,
                plug.DllRelativeDir,
                plug.DllIQuartzServiceImplementFile
                );

            if (!File.Exists(path))
            {
                throw new ArgumentException("找不到需要载入的目录或文件", path);
            }
            byte[]   assemblyBuf = File.ReadAllBytes(path);
            Assembly assembly    = null;

            if (assemblyBuf.Length > 0)
            {
                assembly = Assembly.Load(assemblyBuf);
            }
            else
            {
                return(null);
            }
            Type[] types = assembly.GetTypes();
            try
            {
                foreach (var type in types)
                {
                    if (type.GetInterface("IQuartzService") != null)
                    {
                        QuartzModel           model         = null;
                        AssemblyDynamicLoader dyNamicLoader = new AssemblyDynamicLoader(plug.DllRelativeDir);
                        dyNamicLoader.LoadAssembly(path);

                        TaskBase task = new TaskBase();
                        IDictionary <string, object> dic = task.map;
                        dic.Add("DyNamicLoader", dyNamicLoader);
                        dic.Add("DyNamicMethod", type.FullName);
                        IJobDetail job = JobBuilder.Create(task.GetType())
                                         .WithIdentity(plug.JobName, plug.GroupName)
                                         .UsingJobData(new JobDataMap(dic))
                                         .Build();

                        TriggerBuilder triggerBuilder = TriggerBuilder.Create()
                                                        .WithIdentity(plug.JobName + "Trigger", plug.GroupName)
                                                        .StartNow();

                        if (!string.IsNullOrEmpty(plug.CronexPression))
                        {
                            triggerBuilder.WithCronSchedule(plug.CronexPression);
                        }
                        else
                        {
                            triggerBuilder.WithSimpleSchedule
                                (x => x.WithInterval(new TimeSpan(0, 0, 0, 0, plug.TimeSpan))
                                .RepeatForever());
                        }

                        ITrigger trigger = triggerBuilder.Build();
                        model = new QuartzModel(plug.JobName, job, trigger);
                        return(Add(model));
                    }
                }
            }
            catch (Exception oe)
            {
                Debug.WriteLine(oe.Message);
            }
            return(null);
        }
Пример #11
0
 public QuartzTask(QuartzSchedule quartzSchedule, QuartzModel quartzModel)
 {
     this.QSchedule = quartzSchedule;
     this.QTModel   = quartzModel;
 }