• 【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();
              
            }
  • 相关阅读:
    201771010125王瑜《面向对象程序设计(Java)》第十周学习总结
    201771010125王瑜《面向对象程序设计(Java)》第九周学习总结
    201771010125王瑜《面向对象程序设计(Java)》第八周学习总结
    7 Python外壳:代码结构
    6 python容器
    元组
    列表
    4 python基本元素之变量
    3 关于操作系统基本了解
    1 python是什么?
  • 原文地址:https://www.cnblogs.com/viewcozy/p/4610536.html
Copyright © 2020-2023  润新知