How does DeltaSpike #Schedule wait finish before a new execution? - quartz-scheduler

I am using this lib DeltaSpike Schedule to create cron tasks.
My task runs every minute, but I do not know how set Schedule to wait complete the old task.
#Scheduled(cronExpression = "0 * * * * ?")
public class WaiterRideTask implements Job {
}
If a task delays more than 1 minute, then other task is created yet.

Thanks Dar Whi. This solved my problem: #DisallowConcurrentExecution

Related

VSCode terminate task by name

I created a VSCode extension and run successfully a task by its name, like:
vscode.commands.executeCommand("workbench.action.tasks.runTask", "task_name_here");
now, when I try to terminate that task by name, the UI keeps asking me to select the task manually:
vscode.commands.executeCommand("workbench.action.tasks.terminate", "task_name_here");
Is there a way to acomplish this?
As of VSCode 1.24, there is an API that lets you do this. You can register event listeners for things like tasks being started / ending, and those events all contain a TaskExecution instance. On top of that, there is a read-only list that contains all current executions. An execution is defined like this:
/**
* An object representing an executed Task. It can be used
* to terminate a task.
*
* This interface is not intended to be implemented.
*/
export interface TaskExecution {
/**
* The task that got started.
*/
task: Task;
/**
* Terminates the task execution.
*/
terminate(): void;
}

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 Scheduler Job Auto Termination

How do I create a Quartz Scheduler job that terminates automatically after given amount of time (if running job takes too much time)?
A Quartz scheduler has no built-in functionality to interrupt a job by itself after a given amount of time.
If you dont want to interrupt Jobs (see the interface InterruptableJob) manually (for example with rmi), you could easily establish such a automatically termination.
Either:
When starting a scheduler, fork a deamon-thread that runs periodically and checks whether some of the currently running jobs must be interrupted. For Example you could use a JobDataMap to store the maximum execution time on a per job instance basis.
Each Job could control its maximum execution time in a similar way.
To stop a job from the inside of the job itself the easiest way is to throw an exception after a specific amount of time. For example:
public class MyJob : IJob
{
Timer _t;
public MyJob()
{
TimeSpan maxRunningTime = TimeSpan.FromMinutes(1);
_t = new Timer(delegate { throw new JobExecutionException("took to long"); }, null, (int) maxRunningTime.TotalMilliseconds,
-1);
}
public void Execute(IJobExecutionContext context)
{
// do your word
// destroy T before leaving
_t = null;
}
}
Hope it helps :)

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

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