2017年3月21日 星期二

Quartz (Spring boot)

Quartz (Spring boot)


1.新增專案





2.加入相依Jar檔案

Gradle
//scheduling
compile 'org.springframework:spring-context-support:4.3.6.RELEASE'
// http://ift.tt/2oigF6L
compile group: 'org.quartz-scheduler', name: 'quartz', version: '2.2.3'
// http://ift.tt/2nCzttS
compile group: 'org.springframework', name: 'spring-tx', version: '4.3.6.RELEASE'



3.新增JOB和JOB DETAIL

JOB即為時間到時要執行之動作,以下藉由兩個JOB驗證排班情況
各別為每秒執行,以及每5秒執行。

org.iwlp.job.SecondlyJob
package org.iwlp.job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;
@Component
public class SecondlyJob extends QuartzJobBean{
    private static final Logger log = LoggerFactory.getLogger(SecondlyJob.class);
    /* (non-Javadoc)
     * @see org.springframework.scheduling.quartz.QuartzJobBean#executeInternal(org.quartz.JobExecutionContext)
     */
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        // TODO Auto-generated method stub
        log.debug("1sec RUN");
    }
}


org.iwlp.job.detail.SecondlyJobDetail
package org.iwlp.job.detail;
import org.iwlp.job.SecondlyJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.stereotype.Component;
@Component
public class SecondlyJobDetail extends JobDetailFactoryBean{
    private static final Logger log = LoggerFactory.getLogger(SecondlyJobDetail.class);
    @Override
    public void afterPropertiesSet() {
        setJobClass(SecondlyJob.class);
        setDurability(true);
        setName("secondJobClass");
        super.afterPropertiesSet();
    }
}





org.iwlp.job.FiveSecondJob
package org.iwlp.job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;
@Component
public class FiveSecondJob extends QuartzJobBean{
    private static final Logger log = LoggerFactory.getLogger(FiveSecondJob.class);
    /* (non-Javadoc)
     * @see org.springframework.scheduling.quartz.QuartzJobBean#executeInternal(org.quartz.JobExecutionContext)
     */
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        // TODO Auto-generated method stub
        log.debug("5sec RUN");
    }
}


org.iwlp.job.detail.FiveSecondJobDetail
package org.iwlp.job.detail;
import org.iwlp.job.FiveSecondJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.stereotype.Component;
@Component
public class FiveSecondJobDetail extends JobDetailFactoryBean{
    private static final Logger log = LoggerFactory.getLogger(FiveSecondJobDetail.class);
    @Override
    public void afterPropertiesSet() {
        setJobClass(FiveSecondJob.class);
        setDurability(true);
        setName("fiveJobClass");
        super.afterPropertiesSet();
    }
}




4.TIGGER
Tigger是設定Job何時啟動,以下以Crontab (setCronExpressio) 格式做示範案例
也可用線上工具產生Crontab語法,CronMark:http://ift.tt/19UHFqs

Crontab說明: second(0-59) minute(0-59) hour(0-23) day-of-month(1-31) month(1-12) day-of-week(1~7 或SUN~SAT) year(2009,2012,2013)
更完整說明請參閱:http://ift.tt/2nCPWyw

org.iwlp.job.tigger.SecondlyTigger
package org.iwlp.job.tigger;
import java.text.ParseException;
import org.iwlp.job.detail.SecondlyJobDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.stereotype.Component;

@Component
public class SecondlyTigger extends CronTriggerFactoryBean{
    @Autowired
    private SecondlyJobDetail secondlyJobDetail;
   
    @Override
    public void afterPropertiesSet() throws ParseException  {
        setCronExpression("0/1 * * * * ?");
        setJobDetail(secondlyJobDetail.getObject());
        super.afterPropertiesSet();
    }
}


org.iwlp.job.tigger.FiveSecondTigger
package org.iwlp.job.tigger;
import java.text.ParseException;
import org.iwlp.job.detail.FiveSecondJobDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.stereotype.Component;
@Component
public class FiveSecondTigger extends CronTriggerFactoryBean{
    @Autowired
    private FiveSecondJobDetail fiveSecondJobDetail;
   
    @Override
    public void afterPropertiesSet() throws ParseException  {
        setCronExpression("0/5 * * * * ?");
        setJobDetail(fiveSecondJobDetail.getObject());
        super.afterPropertiesSet();
    }
}



5.SCHEDULER FACTORY
透過Scheduler Factory把上述之Job, tigger加入系統中

org.iwlp.config.ActionSchedulerFactoryBean
package org.iwlp.config;
import java.util.ArrayList;
import java.util.List;
import org.iwlp.job.detail.FiveSecondJobDetail;
import org.iwlp.job.detail.SecondlyJobDetail;
import org.iwlp.job.tigger.FiveSecondTigger;
import org.iwlp.job.tigger.SecondlyTigger;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;
@Component
public class ActionSchedulerFactoryBean extends SchedulerFactoryBean{
    @Autowired
    private SecondlyJobDetail secondlyJobDetail;
    @Autowired
    private SecondlyTigger secondlyTigger;
   
    @Autowired
    private FiveSecondJobDetail secondJobDetail;
    @Autowired
    private FiveSecondTigger secondTigger;
   
    @Override
    public void afterPropertiesSet() throws Exception {
        List<JobDetail> jobDetails = new ArrayList<JobDetail>();
        jobDetails.add(secondlyJobDetail.getObject());
        jobDetails.add(secondJobDetail.getObject());
       
        List<Trigger> triggers = new ArrayList<Trigger>();
        triggers.add(secondlyTigger.getObject());
        triggers.add(secondTigger.getObject());
       
        JobDetail[] jobDetailArray = new JobDetail[jobDetails.size()];
        jobDetails.toArray(jobDetailArray);
       
        Trigger[] triggerArray = new Trigger[triggers.size()];
        triggers.toArray(triggerArray);
       
        setJobDetails(jobDetailArray);
        setTriggers(triggerArray);
       
        super.afterPropertiesSet();
    }
}


6.檢視結果

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.2.RELEASE)
10:07:14.039 [main] INFO  o.i.c.ActionSchedulerFactoryBean.startScheduler [647] - Starting Quartz Scheduler now
10:07:14.048 [actionSchedulerFactoryBean_Worker-1] DEBUG org.iwlp.job.SecondlyJob.executeInternal [35] - 1sec RUN
10:07:14.048 [actionSchedulerFactoryBean_Worker-2] DEBUG org.iwlp.job.SecondlyJob.executeInternal [35] - 1sec RUN
10:07:15.002 [actionSchedulerFactoryBean_Worker-3] DEBUG org.iwlp.job.FiveSecondJob.executeInternal [35] - 5sec RUN
10:07:15.003 [actionSchedulerFactoryBean_Worker-4] DEBUG org.iwlp.job.SecondlyJob.executeInternal [35] - 1sec RUN



7.下載專案
https://drive.google.com/open?id=0B1csp1uQbCtjb0ZmN2VndHZNSWc






Tags: Quartz, Spring Boot, IFTTT-SYNC
March 21, 2017 at 09:24AM
Open in Evernote

沒有留言:

張貼留言