Açıklaması şöyle
Job — An interface to be implemented by components that we wish to have executed. It has a single method called execute() on which we need to provide the details to be performed by the Job
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
Job Arayüzü
Örnek
Şöyle yaparız
public class FileDeletionJob implements Job { @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { String regex = jobExecutionContext.getJobDetail() .getJobDataMap() .getString("regex"); File folder = new File(jobExecutionContext.getJobDetail() .getJobDataMap() .getString("path")); File[] files = folder.listFiles(); Function<File, Boolean> regexMatcher = (file) -> Pattern.compile(regex) .matcher(file.getName()).find(); System.out.println("#####Deleting Files#####"); Arrays.stream(files).filter(regexMatcher::apply) .peek(System.out::println) .forEach(File::delete); System.out.println("#####Done#####"); } }
2. Spring QuartzJobBean Sınıfı
Açıklaması şöyle
Spring Boot provides a wrapper around Quartz Scheduler’s Job interface called QuartzJobBean. This allows you to create Quartz Jobs as Spring beans where you can autowire other beans.
Örnek
Şöyle yaparız
import org.quartz.JobDataMap;import org.quartz.JobExecutionContext;import org.springframework.scheduling.quartz.QuartzJobBean;@Componentpublic class EmailJob extends QuartzJobBean {@Overrideprotected void executeInternal(JobExecutionContext jobExecutionContext)
throws JobExecutionException {logger.info("Executing Job with key {}", jobExecutionContext.getJobDetail().getKey());JobDataMap jobDataMap = jobExecutionContext.getMergedJobDataMap();...}}
Hiç yorum yok:
Yorum Gönder