Run a task only once. The use of quartz

Is there a way that I could only do the job once with Quartz (java). I understand that it makes no sense to use Quartz in this case, but the fact is that I have several tasks, and they run several times, so I use Quartz.

Is it possible.

+4
source share
5 answers

You must use SimpleTrigger, which fires at a specific time and does not repeat. TriggerUtils has many convenient methods for creating these kinds of things.

+10
source

Yes it is possible!

JobKey jobKey = new JobKey("testJob"); JobDetail job = newJob(TestJob.class) .withIdentity(jobKey) .storeDurably() .build(); scheduler.addJob(job, true); scheduler.triggerJob(jobKey); //trigger a job inmediately 
+4
source

In quartz> 2.0, you can force the scheduler to cancel the schedule of any work after completion of work:

 @Override protected void execute(JobExecutionContext context) throws JobExecutionException { ... // process execution ... context.getScheduler().unscheduleJob(triggerKey); ... } 

where triggerKey is the job id to run only once. After this, the task will no longer be called.

+2
source

I'm not sure how similar Quartz is in Mono and Java, but it seems to work in .Net

 TriggerBuilder.Create () .StartNow () .Build (); 
+2
source

The following is an example of running the TestJob class using Quartz 2.x:

 public JobKey runJob(String jobName) { // if you don't call startAt() then the current time (immediately) is assumed. Trigger runOnceTrigger = TriggerBuilder.newTrigger().build(); JobKey jobKey = new JobKey(jobName); JobDetail job = JobBuilder.newJob(TestJob.class).withIdentity(jobKey).build(); scheduler.scheduleJob(job, runOnceTrigger); return jobKey; } 

see also Quartz Enterprise Task Scheduler TutorialsSimpleTriggers

+2
source

Source: https://habr.com/ru/post/1307972/


All Articles