Quartz调度客户端服务器
本文关键字:服务器 客户端 调度 Quartz | 更新日期: 2023-09-27 18:24:54
异常::"名为"RemoteServer"的计划程序已存在。"
我使用的是singleton模式。也尝试了不同的方法,但当我第一次调用它时,它执行得很好,我的工作执行得也很好。但当我安排第二份工作时,我陷入了这个问题。"名为"RemoteServer"的计划程序已存在。"客户代码::
public void SchedulenewAlert()
{
try
{
NameValueCollection properties = new NameValueCollection();
properties["quartz.scheduler.instanceName"] = "RemoteClient";
properties["quartz.scheduler.instanceId"] = "AUTO";
// set thread pool info
properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool,Quartz";
properties["quartz.threadPool.threadCount"] = "5";
properties["quartz.threadPool.threadPriority"] = "Normal";
// set remoting expoter
properties["quartz.scheduler.proxy"] = "true";
properties["quartz.scheduler.proxy.address"] = "tcp://10.0.0.46:555/QuartzScheduler";/QuartzScheduler";
// First we must get a reference to a scheduler
ISchedulerFactory sf = new StdSchedulerFactory(properties);
IScheduler sched = sf.GetScheduler();
String jobname = "remotelyAddedJob2" + DateTime.Now.Ticks.ToString();
IJobDetail job = JobBuilder.Create<MyTest>()
.WithIdentity(jobname, "default1")
.Build();
JobDataMap map = job.JobDataMap;
map.Put("msg", "Your remotely added job has executed!");
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("remotelyAddedTrigger", "default")
.ForJob(job.Key)
.WithCronSchedule("/5 * * ? * *")
.Build();
var TestTrigger = TriggerBuilder.Create().ForJob(job).WithIdentity("remotelyAddedTrigger2", "default1")
.StartNow().Build();
DateTimeOffset pullReportsToQueuejobCalendar = DateBuilder.DateOf(9, 30, 17, 1, 6, 2005);
string trgName = "pullReportsFromQueuejobTrigger" + DateTime.Now.Ticks.ToString();
var pullReportsToQueuejobTrigger = new CalendarIntervalTriggerImpl
{
StartTimeUtc = pullReportsToQueuejobCalendar,
Name = trgName,
RepeatIntervalUnit = IntervalUnit.Second,
RepeatInterval = 40000 // every --- seconds
};
/// while (true)
// {
// schedule the job
sched.ScheduleJob(job, pullReportsToQueuejobTrigger);
}
catch (Exception ex)
{
string lines = "==================Exception Client==========================='n" + DateTime.Now.TimeOfDay.ToString() + "'n====================END========================='n";
System.IO.StreamWriter file = new System.IO.StreamWriter("c:''ExceptionClient.txt", true);
file.WriteLine(lines);
file.Close();
}
///}
}
============================服务器作业代码。。。。
public virtual void Execute(IJobExecutionContext context)
{
// This job simply prints out its job name and the
// date and time that it is running
try
{
string lines = "==================StartClient==========================='n"
+ DateTime.Now.TimeOfDay.ToString() + "'n====================END========================='n";
System.IO.StreamWriter file = new System.IO.StreamWriter("d:''testClient.txt", true);
file.WriteLine(lines);
file.Close();
}
catch(Exception ex)
{
string lines = "==================Exception at Client Calling==========================='n"
+ DateTime.Now.TimeOfDay.ToString() + "'n====================END========================='n";
System.IO.StreamWriter file = new System.IO.StreamWriter("d:''testExceptionClient.txt", true);
file.WriteLine(lines);
file.WriteLine("EXception........... <<<< Message "+ex.Message);
file.Close();
}
}
这对我来说很有用:
IScheduler sched = sf.GetScheduler("scheduler name");
if (sched == null)
{
sched = sf.GetScheduler();
}
您混淆了处理事情的逻辑
这里有一个管理Quartz 的好方法
public class SchedularHandler
{
private static IScheduler _scheduler;
public IScheduler Scheduler
{
get
{
if (_scheduler == null)
_scheduler = GetScheduler();
return _scheduler;
}
}
public SchedularHandler()
{
if (_scheduler == null)
_scheduler = GetScheduler();
if (!_scheduler.IsStarted)
_scheduler.Start();
}
private NameValueCollection GetProperties()
{
NameValueCollection properties = new NameValueCollection();
properties["quartz.scheduler.instanceName"] = "RemoteClient";
properties["quartz.scheduler.instanceId"] = "AUTO";
// set thread pool info
properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool,Quartz";
properties["quartz.threadPool.threadCount"] = "5";
properties["quartz.threadPool.threadPriority"] = "Normal";
// set remoting expoter
properties["quartz.scheduler.proxy"] = "true";
properties["quartz.scheduler.proxy.address"] = "tcp://10.0.0.46:555/QuartzScheduler";
// First we must get a reference to a scheduler
return properties;
}
private IScheduler GetScheduler()
{
var properties = GetProperties();
var sf = new StdSchedulerFactory(properties);
return sf.GetScheduler();
}
public DateTimeOffset ScheduleJob(IJobDetail jobDetail, ITrigger trigger)
{
return _scheduler.ScheduleJob(jobDetail, trigger);
}
}
///////////////////////////////////////////////////////
您的ScheduleAndAlert()方法将类似于
public void SchedulenewAlert()
{
try
{
SchedularHanlder schedHandler = new SchedularHanlder()
String jobname = "remotelyAddedJob2" + DateTime.Now.Ticks.ToString();
IJobDetail job = JobBuilder.Create<MyTest>()
.WithIdentity(jobname, "default1")
.Build();
JobDataMap map = job.JobDataMap;
map.Put("msg", "Your remotely added job has executed!");
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("remotelyAddedTrigger", "default")
.ForJob(job.Key)
.WithCronSchedule("/5 * * ? * *")
.Build();
var TestTrigger = TriggerBuilder.Create().ForJob(job).WithIdentity("remotelyAddedTrigger2", "default1")
.StartNow().Build();
DateTimeOffset pullReportsToQueuejobCalendar = DateBuilder.DateOf(9, 30, 17, 1, 6, 2005);
string trgName = "pullReportsFromQueuejobTrigger" + DateTime.Now.Ticks.ToString();
var pullReportsToQueuejobTrigger = new CalendarIntervalTriggerImpl
{
StartTimeUtc = pullReportsToQueuejobCalendar,
Name = trgName,
RepeatIntervalUnit = IntervalUnit.Second,
RepeatInterval = 40000 // every --- seconds
};
/// while (true)
// {
// schedule the job
schedHandler.ScheduleJob(job, pullReportsToQueuejobTrigger);
}
catch (Exception ex)
{
string lines = "==================Exception Client==========================='n" + DateTime.Now.TimeOfDay.ToString() + "'n====================END========================='n";
System.IO.StreamWriter file = new System.IO.StreamWriter("c:''ExceptionClient.txt", true);
file.WriteLine(lines);
file.Close();
}
///}
}