前面简单学习了通过XML配置或者内存指定的方式实现调度任务。但此用法实战用途较小,企业上多需要分布式集群的方式。quart团队也考虑到了这点,于是有了我们今天要学习的。基于数据库实现分布式。
1 NameValueCollection properties = new NameValueCollection(); 2 3 // 驱动类型,这里用的mysql,目前支持如下驱动: 4 //Quartz.Impl.AdoJobStore.FirebirdDelegate 5 //Quartz.Impl.AdoJobStore.MySQLDelegate 6 //Quartz.Impl.AdoJobStore.OracleDelegate 7 //Quartz.Impl.AdoJobStore.SQLiteDelegate 8 //Quartz.Impl.AdoJobStore.SqlServerDelegate 9 properties["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.MySQLDelegate, Quartz"; 10 11 // 数据源名称 12 properties["quartz.jobStore.dataSource"] = "myDS"; 13 14 // 数据库版本 15 /* 数据库版本 MySql.Data.dll版本,二者必须保持一致 16 * MySql-10 1.0.10.1 17 * MySql-109 1.0.9.0 18 * MySql-50 5.0.9.0 19 * MySql-51 5.1.6.0 20 * MySql-65 6.5.4.0 21 * MySql-695 6.9.5.0 22 * System.Data 23 * SqlServer-20 2.0.0.0 24 * SqlServerCe-351 3.5.1.0 25 * SqlServerCe-352 3.5.1.50 26 * SqlServerCe-400 4.0.0.0 27 * 其他还有OracleODP,Npgsql,SQLite,Firebird,OleDb 28 */ 29 properties["quartz.dataSource.myDS.provider"] = "MySql-65"; 30 31 // 连接字符串 32 properties["quartz.dataSource.myDS.connectionString"] = "server=172.17.43.120;database=JobScheduler;charset=utf8;uid=writeuser;pwd=111111"; 33 34 // 事物类型JobStoreTX自动管理 JobStoreCMT应用程序管理 35 properties["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz"; 36 37 // 表明前缀 38 properties["quartz.jobStore.tablePrefix"] = "QRTZ_"; 39 40 // Quartz Scheduler唯一实例ID,auto:自动生成 41 properties["quartz.scheduler.instanceId"] = "AUTO"; 42 43 // 集群 44 properties["quartz.jobStore.clustered"] = "true"; 45 46 ISchedulerFactory schedfDataBase = new StdSchedulerFactory(properties); 47 IScheduler sched = schedfDataBase.GetScheduler(); 48 49 // 添加任务和触发器 50 IJobDetail jobDetail = JobBuilder.Create<JobTest>().WithIdentity("jobtest1", "group1").Build(); 51 IJobDetail jobDetail2 = JobBuilder.Create<JobTest2>().WithIdentity("jobtest2", "group1").Build(); 52 53 ITrigger simpleTrigger = (ISimpleTrigger)TriggerBuilder.Create().WithIdentity("simpleTrigger1", "group1").WithSimpleSchedule(x => x.WithIntervalInSeconds(2).WithRepeatCount(5)).Build(); 54 55 ITrigger crontrigger = (ICronTrigger)TriggerBuilder.Create().WithIdentity("cronTrigger1", "group1").WithCronSchedule("0/5 * * * * ? ").Build(); 56 57 sched.ScheduleJob(jobDetail, crontrigger); 58 59 sched.ScheduleJob(jobDetail2, simpleTrigger); 60 61 // 开始调度 62 sched.Start();
时间: 2024-10-13 09:11:26