Giriş
Açıklaması şöyle. Yani Trigger'a bir isim ve grup veriliyor
Trigger — A component that determines the schedule upon which a given Job will be performed. Every Trigger is identified by the TriggerKey that has a name and group and the name must be unique within a group. Triggers can also send data to the JobTriggerBuilder: A builder pattern implementation to create a Trigger instance
Trigger Çeşitleri
Açıklaması şöyle.
There are 2 types of triggers,Simple Trigger — Used if you need to execute a job exactly once at a specific moment in time, or at a specific moment in time followed by repeats at a specific interval.Cron Trigger — For scheduling jobs having complex times like if you need to execute a job that recurs based on calendar-like notions
org.quartz.Trigger yaratmak için kendi kodumuzda şu yöntemlerden birisini kullanırız
1. Quartz projesine ait TriggerBuilder kullanılır.
2. Spring projesine ait SimpleTriggerFactoryBean kullanılır
3. Spring projesine ait CronTriggerFactoryBean kullanılır
TriggerBuilder
forJob metodu
Örnekorg.quartz.Trigger yaratmak için kendi kodumuzda Quartz projesine ait TriggerBuilder kullanılır. Şöyle yaparız
private 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();}
İşi belirtilen zamanda koşan basit bir SimpleTrigger nesnesi yaratır
Örnek
Şöyle yaparız
Trigger buildTrigger() { SimpleTriggerFactoryBean triggerFactory = new SimpleTriggerFactoryBean(); triggerFactory.setName("SimpleTriggerName"); triggerFactory.setGroup("Group"); triggerFactory.setStartTime(localTimeToDate(LocalTime.now().plusHours(2))); triggerFactory.setRepeatCount(0); triggerFactory.setRepeatInterval(0); triggerFactory.afterPropertiesSet(); return triggerFactory.getObject(); }
Şöyle yaparız
/*** Create simple trigger.** @param triggerName Trigger name.* @param startTime Trigger start time.* @param repeatTime Job repeat period mills* @param misFireInstruction Misfire instruction (what to do in case of misfire happens).* @return {@link SimpleTrigger}*/public SimpleTrigger createSimpleTrigger(String triggerName, Date startTime,
Long repeatTime, int misFireInstruction) {SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();factoryBean.setName(triggerName);factoryBean.setStartTime(startTime);factoryBean.setRepeatInterval(repeatTime);factoryBean.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);factoryBean.setMisfireInstruction(misFireInstruction);factoryBean.afterPropertiesSet();return factoryBean.getObject();}
Spring CronTriggerFactoryBean Sınıfı
Cron gibi çalışan daha karmaşık iş zamanlama yeteneği sağlar.
Örnek
Şöyle yaparız
Trigger buildCronTrigger() { CronTriggerFactoryBean crontriggerFactory = new CronTriggerFactoryBean(); crontriggerFactory.setName("CronName"); crontriggerFactory.setGroup("Group"); crontriggerFactory.setCronExpression("2 * * * * *"); crontriggerFactory.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW); try { crontriggerFactory.afterPropertiesSet(); } catch (ParseException e) { ... } return crontriggerFactory.getObject(); }
Örnek
Şöyle yaparız
import org.quartz.CronTrigger;import org.quartz.JobDataMap;import org.quartz.JobDetail;import org.quartz.SimpleTrigger;import org.springframework.context.ApplicationContext;import org.springframework.scheduling.quartz.CronTriggerFactoryBean;import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;/*** Create cron trigger.** @param triggerName Trigger name.* @param startTime Trigger start time.* @param cronExpression Cron expression.* @param misFireInstruction Misfire instruction (what to do in case of misfire happens).* @return {@link CronTrigger}*/public CronTrigger createCronTrigger(String triggerName, Date startTime,
String cronExpression, int misFireInstruction) {CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();factoryBean.setName(triggerName);factoryBean.setStartTime(startTime);factoryBean.setCronExpression(cronExpression);factoryBean.setMisfireInstruction(misFireInstruction);try {factoryBean.afterPropertiesSet();} catch (ParseException e) {log.error(e.getMessage(), e);}return factoryBean.getObject();}
Hiç yorum yok:
Yorum Gönder