C# 在特定日期和时间仅解雇一次作业
我有工作的代码-只需将信息记录到数据库C# 在特定日期和时间仅解雇一次作业,c#,asp.net,quartz.net,C#,Asp.net,Quartz.net,我有工作的代码-只需将信息记录到数据库 public class Job : IJob { private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod( ).DeclaringType); #region IJob Members public void
public class Job : IJob
{
private static readonly log4net.ILog log =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod( ).DeclaringType);
#region IJob Members
public void Execute(IJobExecutionContext context)
{
// This job simply prints out its job name and the
// date and time that it is running
JobKey jobKey = context.JobDetail.Key;
log.InfoFormat("SimpleJob says: {0} executing at {1}",
jobKey, DateTime.Now.ToString("r"));
}
#endregion
}
我的单例调度器类
public class Scheduler
{
static Scheduler()
{
NameValueCollection properties = new
NameValueCollection();
properties["quartz.scheduler.instanceName"] = "myApp";
properties["quartz.scheduler.instanceId"] = "MyApp";
properties["quartz.threadPool.type"] =
"Quartz.Simpl.SimpleThreadPool, Quartz";
properties["quartz.threadPool.threadCount"] = "10";
properties["quartz.threadPool.threadPriority"] = "Normal";
properties["quartz.scheduler.instanceName"] =
"TestScheduler";
properties["quartz.scheduler.instanceId"] =
"instance_one";
properties["quartz.jobStore.type"] =
"Quartz.Impl.AdoJobStore.JobStoreTX, Quartz";
properties["quartz.jobStore.useProperties"] = "true";
properties["quartz.jobStore.dataSource"] = "default";
properties["quartz.jobStore.tablePrefix"] = "QRTZ_";
// if running MS SQL Server we need this
properties["quartz.jobStore.lockHandler.type"] =
"Quartz.Impl.AdoJobStore.UpdateLockRowSemaphore, Quartz";
properties["quartz.dataSource.default.connectionString"] =
"Server=localhost;Database=quartzr;Uid=user;Pwd=pass";
properties["quartz.dataSource.default.provider"] =
"SqlServer-20";
_schedulerFactory = new StdSchedulerFactory(properties);
_scheduler = _schedulerFactory.GetScheduler();
}
public static IScheduler GetScheduler()
{
return _scheduler;
}
private static readonly ISchedulerFactory _schedulerFactory;
private static readonly IScheduler _scheduler;
}
Global.asax启动计划程序
void Application_Start(object sender, EventArgs e)
{
Scheduler.GetScheduler().Start();
}
和添加作业的代码
DateTime SelectedDate = this.Calendar1.SelectedDate;
int hour = this.TimeSelector1.Hour;
int minute = this.TimeSelector1.Minute;
int second = this.TimeSelector1.Second;
// First we must get a reference to a scheduler
// jobs can be scheduled before sched.start() has been
called
// get a "nice round" time a few seconds in the
future...
DateTimeOffset startTime = DateBuilder.DateOf(hour,
minute, second, SelectedDate.Day, SelectedDate.Month,
SelectedDate.Year);
try
{
// job1 will only fire once at date/time "ts"
IJobDetail job = JobBuilder.Create<Job>()
.WithIdentity("job1", "group1")
.Build();
ISimpleTrigger trigger =
(ISimpleTrigger)TriggerBuilder.Create()
.WithIdentity("trigger1",
"group1")
.StartAt(startTime)
.Build();
// schedule it to run!
DateTimeOffset? ft =
Scheduler.GetScheduler().ScheduleJob(job, trigger);
log.Info(job.Key +
" will run at: " + ft);
this.Label1.Text = "Zdarzenie dodane";
}
catch (Exception ex)
{
log.Error(ex.Message, ex);
}
DateTime SelectedDate=this.Calendar1.SelectedDate;
int hour=this.timeselector 1.hour;
int minute=this.timeselector 1.minute;
int second=this.timeselector 1.second;
//首先,我们必须获得对调度程序的引用
//可以在sched.start()启动之前安排作业
打电话
//在几秒钟内获得一个“不错的回合”时间
将来
DateTimeOffset startTime=DateBuilder.DateOf(小时,
分钟,秒,选择日期。天,选择日期。月,
选定日期(年);
尝试
{
//作业1将仅在日期/时间“ts”触发一次
IJobDetail job=JobBuilder.Create()
.WithIdentity(“工作1”、“组1”)
.Build();
ISimpleTrigger触发器=
(ISimpleTrigger)TriggerBuilder.Create()
.WithIdentity(“触发器1”),
“第1组”)
.StartAt(startTime)
.Build();
//安排它运行!
DateTimeOffset?英尺=
Scheduler.GetScheduler().ScheduleJob(作业,触发器);
日志信息(job.Key+
“将在以下位置运行:”+英尺);
this.Label1.Text=“Zdarzenie dodane”;
}
捕获(例外情况除外)
{
日志错误(例如消息,例如);
}
问题是jobs被添加到db中,但它会立即启动,而不是立即启动
我指定的具体时间:/I使用最新的库Quartz.NET 2.0 beta 2
我的代码有什么问题吗?我希望使用此API,请帮助Quartz.Net以UTC格式传递日期和时间。尝试更改此行:
.StartAt(startTime)
到
或者,在传入startTime之前,请确保startTime为UTC格式 Quartz.Net希望您以UTC格式传递日期和时间。尝试更改此行:
.StartAt(startTime)
到
或者,在传入startTime之前,请确保startTime为UTC格式
StartAt
除了DateTimeOffest
,Quartz为此提供了
DateBuilder
helper类
var minutesToFireDouble = (futureUTCtime - DateTime.UtcNow).TotalMinutes;
var minutesToFire = (int)Math.Floor(minutesToFireDouble); // negative or zero value will set fire immediately
var timeToFire = DateBuilder.FutureDate(minutesToFire, IntervalUnit.Minute);
var trigger = TriggerBuilder.Create()
.WithIdentity("triggerC", "groupC")
.StartAt(timeToFire)
.WithSimpleSchedule(x => x.WithMisfireHandlingInstructionFireNow())
.Build();
StartAt
除了DateTimeOffest
,Quartz为此提供了
DateBuilder
helper类
var minutesToFireDouble = (futureUTCtime - DateTime.UtcNow).TotalMinutes;
var minutesToFire = (int)Math.Floor(minutesToFireDouble); // negative or zero value will set fire immediately
var timeToFire = DateBuilder.FutureDate(minutesToFire, IntervalUnit.Minute);
var trigger = TriggerBuilder.Create()
.WithIdentity("triggerC", "groupC")
.StartAt(timeToFire)
.WithSimpleSchedule(x => x.WithMisfireHandlingInstructionFireNow())
.Build();
如果您已经知道需要在什么UTC时间运行任务,可以执行以下操作: UTC目标开始时间:2017年9月1日中午12时 按如下方式设置您的石英“StartTime”:
var MyTaskAbcStartTime = "2017-09-01 12:00:00Z"
.StartAt(MyTaskAbcStartTime)
有关utc时间格式的参考microsoft文档,请访问:
希望这有助于为未来的Quartz用户节省一些时间。如果您已经知道需要在什么UTC时间运行任务,您可以执行以下操作: UTC目标开始时间:2017年9月1日中午12时 按如下方式设置您的石英“StartTime”:
var MyTaskAbcStartTime = "2017-09-01 12:00:00Z"
.StartAt(MyTaskAbcStartTime)
有关utc时间格式的参考microsoft文档,请访问:
希望这有助于为未来的Quartz用户节省一些时间。这如何处理WithDailyIntervalsSchedule传递的DailyIntervalsScheduleBuilder和StartingDailyAt?这如何处理WithDailyIntervalsSchedule传递的DailyIntervalsScheduleBuilder和StartingDailyAt?