zoukankan      html  css  js  c++  java
  • 【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();
              
            }
  • 相关阅读:
    揭开webRTC媒体服务器的神秘面纱——WebRTC媒体服务器&开源项目介绍
    打造一个上传图片到图床利器的插件(Mac版 开源)
    游戏编程十年总结(下)
    游戏编程十年总结(上)
    使用“Cocos引擎”创建的cpp工程如何在VS中调试Cocos2d-x源码
    手机网游实时同步方案
    Unity AssetBundle爬坑手记
    Unity3D新手引导开发手记
    敏捷开发随笔(一)高效软件开发之道
    U3D DrawCall优化手记
  • 原文地址:https://www.cnblogs.com/viewcozy/p/4610536.html
Copyright © 2011-2022 走看看