I would like to execute same job in different scheduled times.
Here is one sample which I wrote:
ISchedulerFactory schedFact = new StdSchedulerFactory();
var setoftrigs = new HashSet<ITrigger>();
for (int x=0; x<=2; x++)
{
setoftrigs.Add(TriggerBuilder.Create().WithIdentity("trigger" + x.ToString(), "group1").WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(21, 44+x)).Build());
}
// get a scheduler
IScheduler sched = schedFact.GetScheduler().GetAwaiter().GetResult();
sched.Start();
// create job
IJobDetail job = JobBuilder.Create<RunJob>()
.WithIdentity("job1", "group1")
.Build();
sched.ScheduleJob(job, setoftrigs, false);
The problem is that in certain time jobs is not executed.
But if I do on this way:
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(21, 49))
.Build();
sched.ScheduleJob(job, trigger);
...it works fine.
What I want to achieve is to have a list of triggers when job should be executed.
How to do that?
Related
I am currently working with Quartz Scheduler and I am trying to make a feature, which will be blocking an user if he is inactive for example for more than two days, but unfortunately I didn't work with Quartz Scheduler and I have no idea how to do that.
Well then, you will have a daily cron or an hourly cron (depending on the sensitivity) that checks that value and update the user.
public class CheckForInactiveUsers: IJob
{
public async Task Execute(IJobExecutionContext context)
{
// here you do your business
}
}
IJobDetail job = JobBuilder.Create<CheckForInactiveUsers>()
.WithIdentity("checkForInactiveUsers", "group1")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInHours(1)
.RepeatForever())
.Build();
await scheduler.ScheduleJob(job, trigger);
This question already has an answer here:
Multiple triggers of same Job Quartz.NET
(1 answer)
Closed 6 years ago.
i know how to fire my one routine every day at specific time of day. here is the code.
IScheduler sched = null;
//construct a scheduler factory
ISchedulerFactory schedFact = new StdSchedulerFactory();
//get a scheduler
sched = schedFact.GetScheduler();
sched.Start();
IJobDetail job = JobBuilder.Create<frmMain>()
.WithIdentity("Job", "group")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithDailyTimeIntervalSchedule
(s =>
s.WithIntervalInHours(24)
.OnEveryDay()
.StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(19, 07))
)
.Build();
sched.ScheduleJob(job, trigger);
suppose now i am in scenario that i need to trigger many routine at different time of the day once.
say routine1 should fire at 08:00, routine2 should fire at 15:00 and routine2 should fire at 18:00
now give me suggestion how could i fire different routine at different time of the day. thanks
Like stuartd stated, you need multiple triggers for your job(routine2). I would also suggest to use CronTrigger instead of SimpleTrigger. You can easily create a CronTrigger with:
var trigger1 = TriggerBuilder.Create()
.WithDescription(name)
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(15, 0))
.Build();
var trigger2 = TriggerBuilder.Create()
.WithDescription(name)
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(18, 0))
.Build();
And then just schedule your job with the 2 triggers:
sched.ScheduleJob(job, trigger1);
sched.ScheduleJob(job, trigger2);
I have the current code for my Quartz scheduler:
var scheduler = StdSchedulerFactory.GetDefaultScheduler();
// Job1
var Job1 = JobBuilder.Create<Test1>().WithIdentity("job1", "group1").Build();
// Job2
var Job2 = JobBuilder.Create<Test2>().WithIdentity("job2", "group2").Build();
// Triggers
ITrigger trigger1 = TriggerBuilder.Create().WithIdentity("trigger1", "group1").StartNow().Build()
ITrigger trigger2 = TriggerBuilder.Create().WithIdentity("trigger2", "group2").StartNow().WithSimpleSchedule(x => x.WithIntervalInSeconds(1).WithRepeatCount(4)).Build();
// JobKeys
JobKey jobKey1 = new JobKey("Job1", "group1");
JobKey jobKey2 = new JobKey("Job2", "group2");
// Chain jobs
JobChainingJobListener chain = new JobChainingJobListener("testChain");
chain.AddJobChainLink(jobKey1, jobKey2);
scheduler.ScheduleJob(Job1, trigger1);
scheduler.AddJob(Job2, true);
// Global listener here. I am not sure what I have is correct.
scheduler.ListenerManager.AddJobListener(chain, GroupMatcher<JobKey>.AnyGroup());`
scheduler.Start();
(For clarification, the jobs do nothing more than print to console at the moment.)
From the Quartz website, I found that this will add a JobListener that is interested in all jobs: scheduler.ListenerManager.AddJobListener(chain, GroupMatcher<JobKey>.AnyGroup()); I'm not sure that this is equivalent to a global listener.
I also found that some code where people have done scheduler.addGlobalJobListener(chain); in Java. Is there an equivalent method in c#?
My code compiles and seems to run without errors, but Job2 does not trigger. Job1 prints properly to console.
The issue here is that you have misspelled the key the second time ("Job1" vs "job1") which causes there to be no known link to fire. Here's updated code sample with redundancies removed.
var scheduler = StdSchedulerFactory.GetDefaultScheduler();
JobKey jobKey1 = new JobKey("job1", "group1");
JobKey jobKey2 = new JobKey("job2", "group2");
var job1 = JobBuilder.Create<Test1>().WithIdentity(jobKey1).Build();
var job2 = JobBuilder.Create<Test2>().WithIdentity(jobKey2).StoreDurably(true).Build();
ITrigger trigger1 = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartNow()
.Build();
JobChainingJobListener chain = new JobChainingJobListener("testChain");
chain.AddJobChainLink(jobKey1, jobKey2);
scheduler.ListenerManager.AddJobListener(chain, GroupMatcher<JobKey>.AnyGroup());
scheduler.ScheduleJob(job1, trigger1);
scheduler.AddJob(job2, true);
scheduler.Start();
The scheduler.addGlobalJobListener is old API and longer part of 2.x series. You should use the ListenerManager like you have done.
I'm trying to schedule some jobs using Quartz.NET but I cannot get it working. I tried the following code but nothing happened when the specified time was met.
public void Test()
{
ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
IScheduler scheduler = schedulerFactory.GetScheduler();
IJobDetail jobDetail = JobBuilder.Create<SatellitePaymentGenerationJob>()
.WithIdentity("TestJob")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.ForJob(jobDetail)
.WithCronSchedule("0 26 18 * * ?")
.WithIdentity("TestTrigger")
.StartNow()
.Build();
scheduler.ScheduleJob(jobDetail, trigger);
scheduler.Start();
}
UPDATE:
I also tried the following just to make sure it's not the Cron expression that is causing the problem. Did not work for me either...
IJobDetail jobDetail = JobBuilder.Create<SatellitePaymentGenerationJob>()
.WithIdentity("TestJob", "TestGroup")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.ForJob(jobDetail)
.WithSimpleSchedule(x=> x.RepeatForever().WithIntervalInSeconds(10).WithMisfireHandlingInstructionFireNow())
.StartAt(new DateTimeOffset(DateTime.UtcNow.AddSeconds(10)))
.WithIdentity("TestTrigger", "TestGroup")
.Build();
scheduler.ScheduleJob(jobDetail, trigger);
scheduler.Start();
Console.WriteLine(DateTime.UtcNow.ToLongTimeString());
Console.WriteLine(trigger.GetNextFireTimeUtc());
Note that trigger.GetNextFireTimeUtc() returns a valid time value, but the job never gets triggered.
Where did I go wrong?
Everything is ok with your sample code, maybe the Execute method from SatellitePaymentGenerationJob implementation is wrong, or the job is executed but not in the expected time. In the current shape it will be fired at 18:26 every day, is that what you want?
Compare with my code (working):
class Program
{
static void Main(string[] args)
{
Test();
}
public static void Test()
{
ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
IScheduler scheduler = schedulerFactory.GetScheduler();
IJobDetail jobDetail = JobBuilder.Create<SatellitePaymentGenerationJob>()
.WithIdentity("TestJob")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.ForJob(jobDetail)
.WithCronSchedule("0 45 20 * * ?")
.WithIdentity("TestTrigger")
.StartNow()
.Build();
scheduler.ScheduleJob(jobDetail, trigger);
scheduler.Start();
}
}
internal class SatellitePaymentGenerationJob : IJob
{
public void Execute(IJobExecutionContext context)
{
Console.WriteLine("test");
}
}
I am using Quartz to schedule job in my c# .net application. I am storing all data in database. My code is :
ISchedulerFactory schedFact = new StdSchedulerFactory(properties);
_scheduler = schedFact.GetScheduler();
_scheduler.Start();
job = JobBuilder.Create<JobTask>()
.WithIdentity("job1", "group1")
.Build();
trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.WithSchedule(
CronScheduleBuilder.CronSchedule("0 0/5 * * * ?"))
.Build();
_scheduler.ScheduleJob(job, trigger);
Now I would like to give user function so user can disable(unschedule) job. I have look in quartz tutorial but I can't find the way to do it in c#.
You can call following method in IScheduler
//
// Summary:
// Remove the indicated Quartz.Trigger from the scheduler.
bool UnscheduleJob(string triggerName, string groupName);
Your Job class can implement IInterruptableJob interface and implement
public void Interrupt()
{
JobKey jobKey = new JobKey(JobName, MyService.GroupName);
Scheduler.Interrupt(jobKey);
}