quartz.net 任务调度分布式,动态添加(1)

try
{
//1.首先创建一个作业调度池
var properties = new NameValueCollection();

properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
properties["quartz.threadPool.threadCount"] = "10";
properties["quartz.threadPool.threadPriority"] = "Normal";

//存储类型
properties["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz";

//驱动类型
properties["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz"; //数据源名称
properties["quartz.jobStore.dataSource"] = "myDS";

//连接字符串
properties["quartz.dataSource.myDS.connectionString"] = @"server=.;Initial Catalog=quartz;Integrated Security=True";
//sqlserver版本
properties["quartz.dataSource.myDS.provider"] = "SqlServer-20";

//是否集群
properties["quartz.jobStore.clustered"] = "true";
properties["quartz.scheduler.instanceId"] = "AUTO";

var factory = new StdSchedulerFactory(properties);

IScheduler scheduler = factory.GetScheduler();

scheduler.Start();
ISchedulerFactory schedFact = new StdSchedulerFactory(properties);
IScheduler sched = schedFact.GetScheduler();
var jobKey = JobKey.Create("myjob1", "group");

if (sched.CheckExists(jobKey))
{
Console.WriteLine("当前job已经存在,无需调度:{0}", jobKey.ToString());
}
else
{
IJobDetail job = JobBuilder.Create<HelloJob>()
.WithDescription("使用quartz进行持久化存储")
.StoreDurably()
.RequestRecovery()
.WithIdentity(jobKey)
.UsingJobData("count", 1)
.Build();

var trigger = TriggerBuilder.Create()
.WithIdentity("myjob1", "group")
.WithCronSchedule("/10 * * * * ?")
.Build();
//ITrigger trigger = TriggerBuilder.Create().WithSimpleSchedule(x => x.WithIntervalInSeconds(2).RepeatForever()).Build();

sched.ScheduleJob(job, trigger);

Console.WriteLine("调度进行中!!!");
}
}
catch (SchedulerException se)
{
Console.WriteLine(se);
}

posted @ 2019-04-19 16:59  S_Wilson  阅读(1629)  评论(1编辑  收藏  举报