下面是关于“Java之SpringBoot定时任务案例讲解”的完整攻略。
简介
在实际的项目中,我们时常需要定时执行一些任务,比如数据统计、数据备份、消息通知等。SpringBoot提供了很好的定时任务支持,本文将着重介绍如何使用SpringBoot实现定时任务,并提供两个示例。
步骤
1.依赖添加
在pom.xml
文件中添加以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
</dependency>
2.任务实现
任务实现类需要继承QuartzJobBean
类,并实现其中的executeInternal
方法。下面是一个简单的示例:
@Component
public class SimpleJob extends QuartzJobBean{
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
System.out.println("Simple Job执行");
}
}
3.任务调度
在SpringBoot项目中,可以通过注解方式进行任务调度。下面是通过注解方式实现任务调度的示例:
@Configuration
public class QuartzConfig {
@Bean
public JobDetail simpleJobDetail() {
return JobBuilder.newJob(SimpleJob.class)
.withIdentity("simpleJob")
.storeDurably()
.build();
}
@Bean
public Trigger simpleJobTrigger() {
SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(5)
.repeatForever();
return TriggerBuilder.newTrigger()
.forJob(simpleJobDetail())
.withIdentity("simpleJobTrigger")
.withSchedule(scheduleBuilder)
.build();
}
}
上述代码中,通过simpleJobDetail()
方法定义了SimpleJob
实现类的细节信息,并返回JobDetail
类型的对象。通过simpleJobTrigger()
方法定义了任务触发器的执行策略和执行逻辑,并返回Trigger
类型的对象。
4.任务启动
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
Scheduler scheduler = (Scheduler) context.getBean("schedulerFactoryBean");
scheduler.start();
}
}
在上述的代码片段中,通过Application
类中的main
方法启动SpringBoot应用,并通过调用scheduler.start()
方法来启动任务调度器。
示例
接下来我们提供两个示例,帮助读者更好的理解上述步骤。
示例1
下面的示例将每隔5秒钟输出一条日志。
任务实现类:
@Component
public class SimpleJob extends QuartzJobBean{
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
System.out.println("Simple Job执行");
}
}
任务调度和任务启动类:
@Configuration
public class QuartzConfig {
@Bean
public JobDetail simpleJobDetail() {
return JobBuilder.newJob(SimpleJob.class)
.withIdentity("simpleJob")
.storeDurably()
.build();
}
@Bean
public Trigger simpleJobTrigger() {
SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(5)
.repeatForever();
return TriggerBuilder.newTrigger()
.forJob(simpleJobDetail())
.withIdentity("simpleJobTrigger")
.withSchedule(scheduleBuilder)
.build();
}
@Bean
public SchedulerFactoryBean getSchedulerFactoryBean() {
SchedulerFactoryBean scheduler = new SchedulerFactoryBean();
scheduler.setTriggers(simpleJobTrigger());
return scheduler;
}
}
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
Scheduler scheduler = (Scheduler) context.getBean("schedulerFactoryBean");
scheduler.start();
}
}
示例2
下面的示例将在每周一和每周五的凌晨3点执行一次。
任务实现类:
@Component
public class WeeklyJob extends QuartzJobBean{
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
System.out.println("Weekly Job执行");
}
}
任务调度和任务启动类:
@Configuration
public class QuartzConfig {
@Bean
public JobDetail weeklyJobDetail() {
return JobBuilder.newJob(WeeklyJob.class)
.withIdentity("weeklyJob")
.storeDurably()
.build();
}
@Bean
public Trigger weeklyJobTrigger() {
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 3 ? * MON,FRI");
return TriggerBuilder.newTrigger()
.forJob(weeklyJobDetail())
.withIdentity("weeklyJobTrigger")
.withSchedule(scheduleBuilder)
.build();
}
@Bean
public SchedulerFactoryBean getSchedulerFactoryBean() {
SchedulerFactoryBean scheduler = new SchedulerFactoryBean();
scheduler.setTriggers(weeklyJobTrigger());
return scheduler;
}
}
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
Scheduler scheduler = (Scheduler) context.getBean("schedulerFactoryBean");
scheduler.start();
}
}
总结
至此,我们就完成了SpringBoot定时任务的案例讲解。SpringBoot提供了很好的定时任务支持,可以使得项目实现更多的自动化操作,大大提高开发效率。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java之SpringBoot定时任务案例讲解 - Python技术站