1 Mart 2021 Pazartesi

Quartz TriggerBuilder Sınıfı

Giriş
Şu satırı dahil ederiz.
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
Trigger yaratır. Trigger işin ne zaman koşacağını bilir. Açıklaması şöyle
Trigger contains scheduling condition(ex. number of iterations, start time) to execute the job, and the scheduler performs the job based on this information.

The Relationship Between Trigger and Job
Trigger (1) : Job(1)
One Trigger must be designated as one Job
Trigger (N) : Job(1)
One Job can be run multiple times at different times
Trigger (1) : Job(N) (X)
One Trigger cannot have more then One Job

withSchedule metodu
Örnek - single shot SimpleSchedule
Şöyle yaparız
Trigger buildJobTrigger(JobDetail jobDetail, ZonedDateTime startAt) {
  return TriggerBuilder.newTrigger().forJob(jobDetail)
    .withIdentity(jobDetail.getKey().getName(), "email-triggers")
    .withDescription("Send Email Trigger")
    .startAt(Date.from(startAt.toInstant()))
    .withSchedule(SimpleScheduleBuilder.simpleSchedule()
.withMisfireHandlingInstructionFireNow())
    .build();
}
Örnek - periodic SimpleSchedule
Şöyle yaparız.
Trigger trigger = TriggerBuilder
  .newTrigger()
  .withIdentity("myTriggerName", "myGroup")
  .withSchedule(
    SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1).repeatForever())  )
  .build();
Örnek - CronSchedule
Şöyle yaparız.
Trigger trigger = TriggerBuilder.newTrigger()
  .withIdentity("cronTrigger", "group1")
  .withSchedule(CronScheduleBuilder.cronSchedule("0/10 * * * * ?"))
  .build();
Örnek - CronSchedule
Şöyle yaparız
import org.quartz.CronScheduleBuilder;
import org.quartz.ScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;

String cronExpression = ...;

ScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
Trigger trigger = TriggerBuilder.newTrigger()
  .withIdentity("databasetrigger", "databasegroup")
  .startNow()
  .withSchedule(scheduleBuilder)
  .build();

Hiç yorum yok:

Yorum Gönder