SpringBoot integrated Quartz timing task
1, create project
2. Create task class
package com.quartzjob.job.sysJob;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 具体任务
*/
public class HelloJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
String dateString = simpleDateFormat.format(date);
System.out.println(dateString);
}
}
3, configure trigger and task scheduler
package com.quartzjob.job.config;
import com.quartzjob.job.sysJob.HelloJob;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;
/**
* 配置触发器和任务调度器
*/
@Configuration
public class QuartzConfig {
/**
* 创建job对象
* @return
*/
@Bean
public JobDetailFactoryBean jobDetailFactoryBean(){
JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
//具体任务类
factoryBean.setJobClass(HelloJob.class);
return factoryBean;
}
/**
* 创建tigger对象
* @param jobDetailFactoryBean
* @return
*/
@Bean
public SimpleTriggerFactoryBean simpleTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
//关联jobDetailFactoryBean
factoryBean.setJobDetail(jobDetailFactoryBean.getObject());
//执行周期
factoryBean.setRepeatInterval(2000);
//重复次数
factoryBean.setRepeatCount(5);
return factoryBean;
}
/**
* 创建Scheduler
* @param simpleTriggerFactoryBean
* @return
*/
@Bean
public SchedulerFactoryBean schedulerFactoryBean(SimpleTriggerFactoryBean simpleTriggerFactoryBean){
SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
//关联tigger
factoryBean.setTriggers(simpleTriggerFactoryBean.getObject());
return factoryBean;
}
}
4. Turn on the timed task
package com.quartzjob.job;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class JobApplication {
public static void main(String[] args) {
SpringApplication.run(JobApplication.class, args);
}
}
div>
Read More:
- Springboot integrates quartz timed task trigger_ State error resolution
- Could not start Quartz Scheduler after delay; nested exception is org.quartz.SchedulerException:
- Using global timing task cronutil in hutool tool class
- Several implementation methods of Python timing task
- Spring integrates JUnit configuration
- Gradle tasks disappeared after upgrading Android stuido 4.2?
- Viewing crontab log of timed tasks in Linux
- Quick solution org.gradle.api . tasks.TaskExecutionException : Execution failed for task ‘: app:compileJava ‘
- Quarz timing task does not execute and does not report error
- Mybatis integrates Oracle query and reports an error in the datetime type field
- Spring boot integrates es cluster error report collection
- Spring boot integrates Mongo to solve some common connection and permission problems. Docker compose installs Mongo
- An error is reported when springboot starts: error creating bean with name ‘XXXX’
- Realization of springboot authorization verification technology based on JWT
- Springboot integration redis reports non null key required (solved)
- Springboot integrated with mybatis
- Springboot + mybatis plus transaction management
- The springboot integration mybatis reported an error. The parameter cannot be found
- Exception handling of httpmessage notwritableexception in springboot