标签:set 表达式 prope release 服务 on() tomcat 测试 boot
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kuaishou.rao</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
</dependencies>
public abstract class AbstractJob extends QuartzJobBean {
public abstract String getCron();
public abstract String getName();
public abstract Class<? extends Job> getJobClass();
public String getGroup() {
return "TEST_GROUP";
}
public abstract Trigger getTrigger() throws ParseException;
public Trigger getDefaultTrigger() throws ParseException {
CronTriggerFactoryBean triggerFactoryBean = new CronTriggerFactoryBean();
//cron表达式
triggerFactoryBean.setCronExpression(getCron());
triggerFactoryBean.setName(getName());
triggerFactoryBean.setGroup(getGroup());
triggerFactoryBean.setJobDetail(getJobDetail());
//如果错过了这个执行周期,所有的misFire都不管,执行下一个周期的任务。
triggerFactoryBean.setMisfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING);
//在上述所有的bean都设置成功之后再执行下面的操作
triggerFactoryBean.afterPropertiesSet();
return triggerFactoryBean.getObject();
}
public abstract JobDetail getJobDetail();
public JobDetail getDefaultJobDetail() {
JobDetailFactoryBean detailFactoryBean = new JobDetailFactoryBean();
detailFactoryBean.setJobClass(getJobClass());
detailFactoryBean.setName(getName());
detailFactoryBean.setGroup(getGroup());
//之前的没有执行的任务将在服务重启后补执行
detailFactoryBean.setRequestsRecovery(true);
//把执行结果持久化到数据库
detailFactoryBean.setDurability(true);
detailFactoryBean.afterPropertiesSet();
return detailFactoryBean.getObject();
}
}
@Configuration
public class TestJob extends AbstractJob{
@Override
public String getCron() {
return "1-59 * * * * ? *";
}
@Override
public String getName() {
return "TestJob";
}
@Override
public Class<? extends Job> getJobClass() {
return TestJob.class;
}
@Override
@Bean("TestJobTrigger")
public Trigger getTrigger() throws ParseException {
return getDefaultTrigger();
}
@Override
@Bean("TestJobDetail")
public JobDetail getJobDetail() {
return getDefaultJobDetail();
}
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("hello world");
}
}
2020-06-05 11:21:24.672 INFO 1464 --- [ main] org.quartz.impl.StdSchedulerFactory : Quartz scheduler ‘quartzScheduler‘ initialized from an externally provided properties instance.
2020-06-05 11:21:24.672 INFO 1464 --- [ main] org.quartz.impl.StdSchedulerFactory : Quartz scheduler version: 2.3.2
2020-06-05 11:21:24.672 INFO 1464 --- [ main] org.quartz.core.QuartzScheduler : JobFactory set to: org.springframework.scheduling.quartz.SpringBeanJobFactory@5a5c128
2020-06-05 11:21:24.696 INFO 1464 --- [ main] o.s.s.quartz.SchedulerFactoryBean : Starting Quartz Scheduler now
2020-06-05 11:21:24.696 INFO 1464 --- [ main] org.quartz.core.QuartzScheduler : Scheduler quartzScheduler_$_NON_CLUSTERED started.
hello world
2020-06-05 11:21:24.732 INFO 1464 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ‘‘
2020-06-05 11:21:24.735 INFO 1464 --- [ main] com.kuaishou.rao.demo.DemoApplication : Started DemoApplication in 1.414 seconds (JVM running for 2.062)
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
标签:set 表达式 prope release 服务 on() tomcat 测试 boot
原文地址:https://www.cnblogs.com/rao11/p/13048906.html