Fire Job only one time

1,212 views
Skip to first unread message

netmajor

unread,
Mar 7, 2012, 4:17:00 PM3/7/12
to Quartz.NET
I have that code for Job - just log info to 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)
{
// 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
}



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 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 start scheduler

void Application_Start(object sender, EventArgs e)
{
Scheduler.GetScheduler().Start();
}


And code to add jobs

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);
}


Problem is that jobs was add to db, but it fire immediately, not at
specific time by me :/ I use latest library version 2 beta 2
Do I something wrong in code? I begginer with this API, please help



Robert Engberg

unread,
Mar 8, 2012, 11:28:13 AM3/8/12
to Quartz.NET
Hi Netmajor,

To fire at a specific time, you need to make sure your TriggerBuilder
is aware of a Schedule by feeding it a ScheduleBuilder, like so:

TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartAt(startTime)
.WithSchedule(SimpleScheduleBuilder.Create())
.Build();

You can do much more with a ScheduleBuilder of course, but it seems
that Quartz wants to have a schedule of some sort in order to know
that it should do anything but fire immediately, as you've noticed.

Hope that helps,
Rob

Zhiyong Hong

unread,
Mar 8, 2012, 12:02:23 PM3/8/12
to quar...@googlegroups.com
Also, the scheduler need to RepeatForever or Have a repeat count. E.g, this trigger will keep firing every minute.

 TriggerBuilder.Create()
                  .WithIdentity("trigger1", "group1")
                  .StartAt(startTime)
                  .WithSchedule(SimpleScheduleBuilder.Create().WithIntervalInMinutes(1).RepeatForever())
                  .Build(); 

--
You received this message because you are subscribed to the Google Groups "Quartz.NET" group.
To post to this group, send email to quar...@googlegroups.com.
To unsubscribe from this group, send email to quartznet+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/quartznet?hl=en.


netmajor

unread,
Mar 9, 2012, 2:54:35 AM3/9/12
to Quartz.NET
This tips resolve my problem, also I must use UTC time specific, cause
I live in country with +1 GMT ;)
Reply all
Reply to author
Forward
0 new messages