【quartz】 数据库方式管理任务

 public  static void Run(bool inClearJobs, bool inScheduleJobs)
        {
            var properties = new NameValueCollection();
            properties["quartz.scheduler.instanceName"] = "测试任务"; //调度标识名 集群中每一个实例都必须使用相同的名称 
            properties["quartz.scheduler.instanceId"] = "instance_one"; //ID设置为自动获取 每一个必须不同
            properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
            properties["quartz.threadPool.threadCount"] = "2";//线程数量
            properties["quartz.threadPool.threadPriority"] = "Normal";//线程优先级
            properties["quartz.jobStore.misfireThreshold"] = "60000"; //容许的最大作业延长时间
            properties["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz";//AdoJobStore
            properties["quartz.jobStore.useProperties"] = "false";//设置为TRUE不会出现序列化非字符串类到 BLOB 时产生的类版本问题
            properties["quartz.jobStore.dataSource"] = "default";//数据库别名 随便取
            properties["quartz.jobStore.tablePrefix"] = "QRTZ_";//指定所使用的数据库表前缀
            properties["quartz.jobStore.clustered"] = "true";//加入集群
            properties["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz";//数据库类型
            properties["quartz.dataSource.default.connectionString"] = @"Server=10.32.11.23,14339;database=YintaiService;uid=erpdev;pwd=Test_2jaJk)@aA2";//数据库配置连接串
            properties["quartz.dataSource.default.provider"] = "SqlServer-20";// framework2.0以上

         
            ISchedulerFactory sf = new StdSchedulerFactory(properties);
            IScheduler sched = sf.GetScheduler();

            if (inClearJobs)
            {       
                sched.Clear();//清除数据
            }

           //注入一个任务
           //jobdatamap可以持久化到数据库,但是在运行期间改变了jobdatamap中的某个属性时,这个新值不能持久化到数据库,也就等于是没法修改。
            if (inScheduleJobs)
            {
                string schedId = sched.SchedulerInstanceId;
                int count = 1;     
                IJobDetail job = JobBuilder.Create<HelloJob>()
                    .WithIdentity("job_" + count, schedId) // put triggers in group named after the cluster node instance just to distinguish (in logging) what was scheduled from where
                    .RequestRecovery() // ask scheduler to re-execute this job if it was in progress when the scheduler went down...
                    .Build();
                ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create()
                                                              .WithIdentity("triger_" + count, schedId)
                                                              .StartAt(DateBuilder.FutureDate(1, IntervalUnit.Second))
                                                              .WithSimpleSchedule(x => x.WithRepeatCount(2000).WithInterval(TimeSpan.FromSeconds(2)))
                                                              .Build(); 
                count++;
                sched.ScheduleJob(job, trigger);      
            }

    
            sched.Start();
          
        }
原文地址:https://www.cnblogs.com/viewcozy/p/4610536.html