24 Ağustos 2021 Salı

SpringQuartz Kullanımı JobDetail Yaratma

JobBuilder Sınıfı
org.quartz.JobDetail  yaratmak için kendi kodumuzda Quartz projesine ait JobBuilder kullanılır.

newJob metodu
Burada Job sınıfının sınıf tipini vermek gerekir.
Job iki şekilde kodlanabilir
1. Quartz projesinin Job arayüzünden kalıtılır
2. Spring' ait QuartzJobBean sınıfından  kalıtılır

Örnek
Elimizde şöyle bir Job olsun
public class FileDeletionJob implements Job {
  ...
}
Şöyle yaparız. Burada JobBuilder ile bir JobDetail yaratılıyor. Daha sonra Trigger yaratılıyor. Scheduler.schedule() çağrısına JobDetail ve Trigger geçiliyor.
public static void configureJob(String path, String regex) throws SchedulerException {
  JobDetail job = JobBuilder.newJob(FileDeletionJob.class)
    .usingJobData("regex", regex)
    .usingJobData("path", path)
    .build();

  Trigger trigger = newTrigger().withSchedule(simpleSchedule()
                                .withIntervalInMinutes(1)
     // .withIntervalInHours(1)
     .repeatForever())
     .build();

  Scheduler scheduler = new StdSchedulerFactory().getScheduler();
  scheduler.start();
  scheduler.scheduleJob(job, trigger);
}
usingJobData metodu
Örnek
Şöyle yaparız
JobDetail buildJobDetail(ScheduleReportRequest scheduleReportRequest) {
  JobDataMap jobDataMap = new JobDataMap();

  jobDataMap.put("reportname", scheduleEmailRequest.getEmail());
  jobDataMap.put("type", scheduleEmailRequest.getSubject());
       

  return JobBuilder.newJob(ReportJob.class)
    .withIdentity(UUID.randomUUID().toString(), "report-jobs")
    .withDescription("Send Reports")
    .usingJobData(jobDataMap)
    .storeDurably()
    .build();
}

storeDurably metodu
Örnek
Şöyle yaparız
private JobDetail buildJobDetail(ScheduleEmailRequest scheduleEmailRequest) {
  JobDataMap jobDataMap = new JobDataMap();
  jobDataMap.put("email", scheduleEmailRequest.getEmail());
  jobDataMap.put("subject", scheduleEmailRequest.getSubject());
  jobDataMap.put("body", scheduleEmailRequest.getBody());
  return JobBuilder.newJob(EmailJob.class)
    .withIdentity(UUID.randomUUID().toString(), "email-jobs")
    .withDescription("Send Email Job")
    .usingJobData(jobDataMap)
    .storeDurably()
    .build();
}
Spring JobDetailFactoryBean 
org.quartz.JobDetail  yaratmak için kendi kodumuzda Spring projesine ait JobDetailFactoryBean kullanırız

Örnek
Şöyle yaparız
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.QuartzJobBean;

/**
 * Create Quartz Job.
 *
 * @param jobClass  Class whose executeInternal() method needs to be called.
 * @param isDurable Job needs to be persisted even after completion. if true, job will be
persisted, not otherwise.
 * @param context   Spring application context.
 * @param jobName   Job name.
 * @param jobGroup  Job group.
 * @return JobDetail object
*/
public JobDetail createJob(Class<? extends QuartzJobBean> jobClass, boolean isDurable,
                           ApplicationContext context, String jobName, String jobGroup) {
  JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
  factoryBean.setJobClass(jobClass);
  factoryBean.setDurability(isDurable);
  factoryBean.setApplicationContext(context);
  factoryBean.setName(jobName);
  factoryBean.setGroup(jobGroup);

  // set job data map
  JobDataMap jobDataMap = new JobDataMap();
  jobDataMap.put(jobName + jobGroup, jobClass.getName());
  factoryBean.setJobDataMap(jobDataMap);

  factoryBean.afterPropertiesSet();

  return factoryBean.getObject();
}

Hiç yorum yok:

Yorum Gönder