SpringBoot定时任务

        定时任务是企业开发中最常见的功能之一,比如定时统计订单数、数据库备份、定时发送短信和邮件,简单的定时任务都可以通过Spring中的 @Scheduled注解来实现。复杂的定时任务可以通过集成Quartz实现。

 开启定时任务:

package com.song.songvue;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling 
public class SongVueApplication {
	public static void main(String[] args) {
		SpringApplication.run(SongVueApplication.class, args);
	}

}
package com.song.songvue.config.task;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MySchedule {

    /**
     * fixedDelay = 1000 每一秒执行一次
     */
    @Scheduled(fixedDelay = 1000)
    public void fixedDelay() {
        System.out.println("fixedDelay: " + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * fixedRate表示当前任务开始执行两秒后,开启另一个定时任务
     */
    /*@Scheduled(fixedRate = 2000)
    public void fixedRate() {
        System.out.println("fixedRate: " + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
    }*/

    /**
     * initialDelay: 表示首次执行的延迟时间
     */
    @Scheduled(initialDelay = 1000, fixedRate = 2000)
    public void initialDelay() {
        System.out.println("initialDelay: " + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
    }

    @Scheduled(cron = "0 * * * * ?")
    public void cron() {
        System.out.println("cron, 每分钟执行一次: " + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 每天早上五点
     */
    @Scheduled(cron = "0 0 5 * * ?")
    public void task() {
        System.out.println("cron, 每天早上五点执行一次: " + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
    }

}

SpringBoot整合Quartz :

pom.xml添加以下内容:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
package com.song.songvue.config.task;

import org.quartz.CronTrigger;
import org.quartz.JobDataMap;
import org.quartz.SimpleTrigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.*;

@Configuration
public class QuartzConfig {

    /**
     * 任务详情
     */
    @Bean
    MethodInvokingJobDetailFactoryBean jobDetail1() {
        MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean();
        bean.setTargetBeanName("myJob");
        bean.setTargetMethod("sayHello");
        return bean;
    }

    /**
     * 任务详情
     */
    @Bean
    JobDetailFactoryBean jobDetail2() {
        JobDetailFactoryBean bean = new JobDetailFactoryBean();
        bean.setJobClass(MySecondJob.class);
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("name", "song");
        bean.setJobDataMap(jobDataMap);
        bean.setDurability(true);
        return bean;
    }

    /**
     * 定义触发器,只需要指定Job的实例名和调用的方法即可, 注册这种方法无法在创建JobDetail时传递参数
     */
    @Bean
    SimpleTriggerFactoryBean simpleTrigger() {
        // 简单触发器
        SimpleTriggerFactoryBean bean = new SimpleTriggerFactoryBean();
        bean.setJobDetail(jobDetail1().getObject());
        bean.setRepeatCount(3);
        // 任务延迟启动时间
        bean.setStartDelay(1000);
        // 任务时间间隔
        bean.setRepeatInterval(2000);
        return bean;
    }

    /**
     * CronTrigger 触发
     */
    @Bean
    CronTriggerFactoryBean cronTrigger() {
        CronTriggerFactoryBean bean = new CronTriggerFactoryBean();
        bean.setJobDetail(jobDetail2().getObject());
        bean.setCronExpression("* * * * * ?");
        return bean;
    }

    /**
     * Trigger有两种实现方式, SimpleTrigger 和 CronTrigger
     */
    @Bean
    SchedulerFactoryBean schedulerFactory() {
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        SimpleTrigger simpleTrigger = simpleTrigger().getObject();
        CronTrigger cronTrigger = cronTrigger().getObject();
        bean.setTriggers(simpleTrigger,cronTrigger);
        return bean;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值