Java Quartz 2.2.1 - Schedule - quartz-scheduler

Hi the following code triggers every 60 seconds.
How can I change it so that it will trigger once a day at 4am forever.
I am using Quartz 2.2.1 using Tomcat 7.0.53
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("TestTrigger", "group1")
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(interval_seconds).repeatForever())
.build();
I looked at the documentation exmaples but keep getting errors.

You could use startAt(Date triggerStartTime) method with your desired time (4am), and then repeat it forever every 24 hours
Trigger trigger = newTrigger()
.withSchedule(simpleSchedule()
.withIntervalInHours(24)
.repeatForever())
.startAt(new SimpleDateFormat("dd/MM/yyyy hh:mmaaa").parse("24/01/2015 04:00AM"))
.build();

Use a Cron-based Trigger:
Trigger trigger = TriggerBuilder.newTrigger()
.withSchedule(cronSchedule("0 0 4am * * ?"))
.build();

if you want an easier way to create the date then use DateBuilder to create a date with the parameters you want. dateOf( or todayAt) should work.
Something like:
.startAt(DateBuilder.dateOf(4, 0, 0))

Related

Flutter Local Notifications show notification every 72 hours

I am trying to send out notifications every 72 hours. I am using the flutter_local_notifications package. I know I can periodically show notifications but as far as I can see it is limited to these options:
/// The available intervals for periodically showing notifications.
enum RepeatInterval {
/// An interval for every minute.
everyMinute,
/// Hourly interval.
hourly,
/// Daily interval.
daily,
/// Weekly interval.
weekly
}
Is there any way to achieve the 72h interval? I couldn't find anything on this. Let me know if you need any more info! Any help is appreciated!
you can try this :-
fltrNotification = new FlutterLocalNotificationsPlugin();
var scheduledTime = DateTime.now().add(Duration(hour : 72));
fltrNotification.schedule(1, "Times Uppp", task,
scheduledTime, generalNotificationDetails);
always this approach works
make your own copy of package
modify it 😊
1.make your own copy
you can easily copy the package file to your project . and use it like this (flutter doc)
dependencies:
plugin1:
path: ../plugin1/
if you prefer you can fork project and use it like below
dependencies:
plugin1:
git:
url: git://github.com/flutter/plugin1.git
2.modify it 🛠
for your question you can change the value of Daily interval to (3 * Daily interval)
I found this part of code (android - ios)
I would use the flutter cron package if I were you: cron package on pub.dev
It allows you schedule a cron job which is simply a task that runs every x seconds or days, months...
For your example:
fltrNotification = new FlutterLocalNotificationsPlugin();
final cron = Cron();
// Schedule a task that will run every 3 days
cron.schedule(Schedule.parse('0 0 */3 * *'), () async {
// Schedule a notification right now
fltrNotification.schedule(1, "Times Uppp", task,
DateTime.now(), generalNotificationDetails);
print('every three days');
});
If you want to change the frequency, cron is very flexible and you can do pretty much any frequency, the cron syntax is pretty straightforward and their are some websites online that allow you to simply generate it.
There are, of course, several ways to use cron to do what you want. You could schedule a notification for the next 72 hours every 72 hours, refreshing every 24 hours, whatever seems better to you.
(I used part of Piyush Kumar's answer for this example by the way, and updated it to use cron)

JAVA Quartz - Skip job if previous is still running AND wait for the next schedule time

I have a Java solution that uses Quartz 2.2.3, and what I have is:
My job class is annotated #DisallowConcurrentExecution to avoid concurrence, so the same job just can run once per time (OK)
It is a CRON and runs every 1 hour (OK)
The time is 1pm and the the job starts run (OK)
Now the time is 2pm and the previous job didn't finish yet (OK)
As the class is annotated the next job will not start (IT IS GREAT - OK)
Now the time is 2h:15min and the first job just finished (OK)
Now the issue, as the second job didn't start at 2pm BUT now the first just finished, the second one will start.
This is my problem, I don't want to make the second job wait if the previous didn't finish, I want to skip the second one and when the time turns 3pm the job can run. Reading javadoc I added the withMisfireHandlingInstructionDoNothing() but it didn't work.
I think I'm doing something wrong or missing something.
My code:
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
JobDetail job = JobBuilder.newJob(TestCronService.class).withIdentity("testA","testB").build();
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity("testA","testB")
.withSchedule(CronScheduleBuilder.cronSchedule("0 0 * * * ?")
.withMisfireHandlingInstructionDoNothing())
.build();
scheduler.scheduleJob(job, trigger);
scheduler.start();
Change the function :
withMisfireHandlingInstructionDoNothing()
with
withMisfireHandlingInstructionNextWithRemainingCount()

quartz trigger builder startnow not firing the trigger during the start

i am tring to use quartz builder for creating a cron trigger and trying to give the startnow instruction. but the trigger is not starting instead it is starting only after completing the given time interval. can someone help me to start the trigger during the starting the server.i am using plain quartz and no springs.
Trigger trigger = newTrigger()
.withIdentity(SchedulerConstants.TRIGGER_CLARITY,SchedulerConstants.QI_GROUP)
.withSchedule(cronSchedule("0 0/60 * * * ?").withMisfireHandlingInstructionDoNothing())
.startNow()
.build();
There wont be any effect of calling startNow() on a CronTrigger as it triggers the job based on cron expression supplied unlike time based SimpleTrigger.
Your cron expression tells Quartz to run every 60 mins starting 0th minute of ever hour.
Unless you start the scheduler at exactly 0th min, you willnot see startNow effect.
Hope this is clear to you.
Refer Quartz CronTrigger tutorials/documentation for more details.
You can add a second trigger to your job with StartNow. I think this would work for you assuming you had a job class called SomeJob.
var schedulerFactory = new StdSchedulerFactory();
var scheduler = schedulerFactory.GetScheduler();
scheduler.Start();
IJobDetail job = JobBuilder.Create<SomeJob>()
.WithIdentity("job1", SchedulerConstants.QI_GROUP)
.Build();
Trigger trigger = newTrigger()
.withIdentity(SchedulerConstants.TRIGGER_CLARITY,SchedulerConstants.QI_GROUP)
.withSchedule(cronSchedule("0 0/60 * * * ?").withMisfireHandlingInstructionDoNothing())
.build();
scheduler.ScheduleJob(job, trigger);
IJobDetail job2 = JobBuilder.Create<SomeJob>()
.WithIdentity("job2", SchedulerConstants.QI_GROUP)
.Build();
Trigger trigger2 = newTrigger()
.withIdentity("trigger2",SchedulerConstants.QI_GROUP)
.StartNow()
.build();
scheduler.ScheduleJob(job2, trigger2);
.startNow() will let the trigger startup, but the trigger may or may not fire at this time - depending upon the schedule configured for the Trigger.
you can trigger the task when application starts
_scheduler.TriggerJob(new JobKey("A/BTestConfigsDaily")).Wait();

Help and advice needed working with Quartz.NET NthIncludedDayTrigger

I've started using Quartz.NET recently, and so far, it's been really
helpful. Now, I'm trying to use it to create a job that runs once a
month using a NthIncludedDayTrigger (I want to use the
NthIncludedDayTrigger as eventually I will be specifying a calendar to
exclude weekends/holidays).
To familiarise myself with the code, I've
set up a simple console application to create an NthIncludedDayTrigger
where the first fire time will be 15 seconds from now:
static void Main(string[] args)
{
IScheduler scheduler = StdSchedulerFactory.DefaultScheduler;
scheduler.Start();
var jobDetail = new JobDetail("Job name", "Group name", typeof(SomeIJobImplementation));
var trigger = new NthIncludedDayTrigger();
trigger.Name = "Trigger name";
trigger.MisfireInstruction = MisfireInstruction.NthIncludedDayTrigger.DoNothing;
trigger.IntervalType = NthIncludedDayTrigger.IntervalTypeMonthly;
//I'm using the following while experimenting with the code (AddHour(1) to account for BST):
trigger.FireAtTime = DateTime.UtcNow.AddHours(1).AddSeconds(15).ToString("HH:mm:ss");
//I'm using the following while experimenting with the code:
trigger.N = DateTime.Today.Day;
Console.WriteLine("Started, press any key to stop ...");
Console.ReadKey();
scheduler.Shutdown(false);
}
...
public class SomeIJobImplementation : IJob
{
public void Execute(JobExecutionContext context)
{
Logger.Write(String.Format(
"Job executed called at {0}",
DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss")), null, 1,
TraceEventType.Information);
}
}
Running this results in the job being executed multiple times
(approximately once per second) for one minute. I'm using an ADO.NET
job store and can see in my database that QRTZ_TRIGGERS.NEXT_FIRE_TIME
is set to the last executed time, i.e. doesn't seem to be scheduled to
run again.
I expected the above code to run the job once (after about 15
seconds), then schedule the job to run again in one months time.
Perphaps the issue is just with the way I'm using Quartz.NET whilst
I've been experimenting or, maybe, my expectations are wrong? Either
way, I would be most grateful for any help/suggestions to explain the
behaviour I've observed, and what I need to change to get the
behaviour I want.
I must be late but I was trying to implement the same solution and ended up here.
I reckon you should star the scheduler after you've defined jobs and triggers.

Quartz.Net - delay a simple trigger to start

I have a few jobs setup in Quartz to run at set intervals. The problem is though that when the service starts it tries to start all the jobs at once... is there a way to add a delay to each job using the .xml config?
Here are 2 job trigger examples:
<simple>
<name>ProductSaleInTrigger</name>
<group>Jobs</group>
<description>Triggers the ProductSaleIn job</description>
<misfire-instruction>SmartPolicy</misfire-instruction>
<volatile>false</volatile>
<job-name>ProductSaleIn</job-name>
<job-group>Jobs</job-group>
<repeat-count>RepeatIndefinitely</repeat-count>
<repeat-interval>86400000</repeat-interval>
</simple>
<simple>
<name>CustomersOutTrigger</name>
<group>Jobs</group>
<description>Triggers the CustomersOut job</description>
<misfire-instruction>SmartPolicy</misfire-instruction>
<volatile>false</volatile>
<job-name>CustomersOut</job-name>
<job-group>Jobs</job-group>
<repeat-count>RepeatIndefinitely</repeat-count>
<repeat-interval>43200000</repeat-interval>
</simple>
As you see there are 2 triggers, the first repeats every day, the next repeats twice a day.
My issue is that I want either the first or second job to start a few minutes after the other... (because they are both in the end, accessing the same API and I don't want to overload the request)
Is there a repeat-delay or priority property? I can't find any documentation saying so..
I know you are doing this via XML but in code you can set the StartTimeUtc to delay say 30 seconds like this...
trigger.StartTimeUtc = DateTime.UtcNow.AddSeconds(30);
This isn't exactly a perfect answer for your XML file - but via code you can use the StartAt extension method when building your trigger.
/* calculate the next time you want your job to run - in this case top of the next hour */
var hourFromNow = DateTime.UtcNow.AddHours(1);
var topOfNextHour = new DateTime(hourFromNow.Year, hourFromNow.Month, hourFromNow.Day, hourFromNow.Hour, 0, 0);
/* build your trigger and call 'StartAt' */
TriggerBuilder.Create().WithIdentity("Delayed Job").WithSimpleSchedule(x => x.WithIntervalInSeconds(60).RepeatForever()).StartAt(new DateTimeOffset(topOfNextHour))
You've probably already seen this by now, but it's possible to chain jobs, though it's not supported out of the box.
http://quartznet.sourceforge.net/faq.html#howtochainjobs