c# - Fire Job only one time at specific date and time -
i have code job - log info database
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) { // job prints out job name , // date , time running jobkey jobkey = context.jobdetail.key; log.infoformat("simplejob says: {0} executing @ {1}", jobkey, datetime.now.tostring("r")); } #endregion }
my singleton scheduler class
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 need 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 start scheduler
void application_start(object sender, eventargs e) { scheduler.getscheduler().start(); }
and code add jobs
datetime selecteddate = this.calendar1.selecteddate; int hour = this.timeselector1.hour; int minute = this.timeselector1.minute; int second = this.timeselector1.second; // first must reference scheduler // jobs can scheduled before sched.start() has been called // "nice round" time few seconds in future... datetimeoffset starttime = datebuilder.dateof(hour, minute, second, selecteddate.day, selecteddate.month, selecteddate.year); try { // job1 fire once @ date/time "ts" ijobdetail job = jobbuilder.create<job>() .withidentity("job1", "group1") .build(); isimpletrigger trigger = (isimpletrigger)triggerbuilder.create() .withidentity("trigger1", "group1") .startat(starttime) .build(); // schedule run! datetimeoffset? ft = scheduler.getscheduler().schedulejob(job, trigger); log.info(job.key + " run at: " + ft); this.label1.text = "zdarzenie dodane"; } catch (exception ex) { log.error(ex.message, ex); }
problem jobs add db, fire immediately, not @ specific time me :/ use latest library quartz.net 2.0 beta 2 wrong in code? begginer api, please
quartz.net expects pass in dates , times in utc. try changing line:
.startat(starttime)
to
.startat(starttime.touniversaltime())
or, make sure starttime in utc before passing in.
Comments
Post a Comment