springboot定时任务

今天用springboot写到一个需求:每周定时发送任务,但是要避开法定节假日。

  网上找了些博客看,主要参考了https://www.cnblogs.com/lic309/p/4089633.html,整理补充了一下,完成需求。

  (另,如果想要实时动态更新定时任务,可以参考http://blog.csdn.net/liuchuanhong1/article/details/60873295)

 

  为了避开节假日,我写触发器,试了下用quartz的Calendar和HolidayCalendar,代码略复杂。放弃。

import org.quartz.Calendar;
import org.quartz.impl.calendar.HolidayCalendar;

  后来就弄了个数据库,存放需避开的日期,维护的时候只需更改数据库里的日期即可。(这个按照实际需求来,反复查找数据库,会消耗资源)

  类如下。

import lombok.Data;
 
import javax.persistence.Entity;
import javax.persistence.Id;
 
/**
 * Created by 孙义朗 on 2017/11/9 0009.
 */
@Data
@Entity
public class Holiday {
    @Id
    private String holidayName;
    private String holidayDate;
}

定时任务可以用spring自带支持定时器的任务实现。简单注解即可。代码如下。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
@Configurable
@EnableScheduling
public class ScheduledTaskRemoveHoliday {
    @Autowired
    HolidayRepository holidayRepository;

    @Scheduled(cron = "0 15 10 15 * ?"/*每月15日上午10:15触发*/)
//    @Scheduled(fixedRate = 1000 * 3/*每隔3秒触发一次,供测试使用*/)
//    @Scheduled(cron ="0/3 * * * * ?"/*每隔3秒触发一次,同上,具体cronExpression表达式使用方法见文末*/)
    public void doTest(){
        String date=simpleDateFormat().format(new Date());
        List<Holiday> hList=holidayRepository.findAll();
        boolean flag=false;
        for(Holiday holiday:hList){
            if(date.equals(holiday.getHolidayDate())){
                flag=true;
            }
        }
        if(flag){
            System.out.println("今天服务器休息");
        }else {
            System.out.println ("hello my friend");
        }
    }

    private SimpleDateFormat simpleDateFormat(){
        return new SimpleDateFormat ("MM-dd");
    }

}

完成。服务启动时,定时任务会按照cronExpression表达式自动执行。确实很简单方便。

  唯一遗憾的避开日期的方式用到了数据库。因为考虑的是节假日期不固定,不好写算法。

 

  另一种方法,稍微复杂一点,我也试用了一下,可行的。

  需要用到quartz,首先添加maven依赖

<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>1.8.4</version>
</dependency>

自定义一个定时任务

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class ScheduledTask implements Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("hello");
    }
}

然后,新建触发器的抽象类,这里需要添加maven依赖

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context-support</artifactId>
   <version>3.2.17.RELEASE</version>
</dependency>

 

import org.quartz.Job;
import org.quartz.JobDetail;
import org.springframework.scheduling.quartz.CronTriggerBean;

import java.io.Serializable;

public abstract class BaseCronTrigger extends CronTriggerBean implements Serializable {

    private static final long serialVersionUID = 1L;

    public void init() {
        // 得到任务
        JobDetail jobdetail = new JobDetail(this.getClass().getSimpleName(), this.getMyTargetObject().getClass());
        this.setJobDetail(jobdetail);
        this.setJobName(jobdetail.getName());
        this.setName(this.getClass().getSimpleName());
        try {
            this.setCronExpression(this.getMyCronExpression());
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }

    }

    public abstract String getMyCronExpression();

    public abstract Job getMyTargetObject();

}

实现类,其init()方法,来为这个触发器绑定任务。

import org.quartz.Job;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

import java.io.Serializable;

@Component
public class InitializingCronTrigger extends BaseCronTrigger implements Serializable {

    private static final long serialVersionUID = 1L;

    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    public InitializingCronTrigger() {
        init();
    }

    @Override
    public String getMyCronExpression() {
        return "0/3 * * * * ?";
    }

    @Override
    public Job getMyTargetObject() {
        return new ScheduledTask();
    }

    public void parse() {
        try {
            schedulerFactoryBean.getObject().pauseAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

最后一步,配置

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

@Configuration
public class ScheduledConfiguration {
    @Bean
    public SchedulerFactoryBean schedulerFactory(CronTriggerBean[] cronTriggerBean) {
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        System.err.println(cronTriggerBean[0]);
        bean.setTriggers(cronTriggerBean);
        return bean;
    }
}

完成。

 

 

  介绍一个cronExpression表达式。这一部分是摘抄的:

      

字段 允许值 允许的特殊字符
  0-59   , - * /
  0-59   , - * /
小时   0-23   , - * /
日期   1-31   , - *   / L W C
月份   1-12 或者 JAN-DEC   , - * /
星期   1-7 或者 SUN-SAT   , - *   / L C #
年(可选)   留空, 1970-2099   , - * /

  
如上面的表达式所示: 

“*”字符被用来指定所有的值。如:”*“在分钟的字段域里表示“每分钟”。 

“-”字符被用来指定一个范围。如:“10-12”在小时域意味着“10点、11点、12点”。
 
“,”字符被用来指定另外的值。如:“MON,WED,FRI”在星期域里表示”星期一、星期三、星期五”. 

“?”字符只在日期域和星期域中使用。它被用来指定“非明确的值”。当你需要通过在这两个域中的一个来指定一些东西的时候,它是有用的。看下面的例子你就会明白。 


“L”字符指定在月或者星期中的某天(最后一天)。即“Last ”的缩写。但是在星期和月中“L”表示不同的意思,如:在月子段中“L”指月份的最后一天-1月31日,2月28日,如果在星期字段中则简单的表示为“7”或者“SAT”。如果在星期字段中在某个value值得后面,则表示“某月的最后一个星期value”,如“6L”表示某月的最后一个星期五。

“W”字符只能用在月份字段中,该字段指定了离指定日期最近的那个星期日。

“#”字符只能用在星期字段,该字段指定了第几个星期value在某月中


每一个元素都可以显式地规定一个值(如6),一个区间(如9-12),一个列表(如9,11,13)或一个通配符(如*)。“月份中的日期”和“星期中的日期”这两个元素是互斥的,因此应该通过设置一个问号(?)来表明你不想设置的那个字段。表7.1中显示了一些cron表达式的例子和它们的意义:

表达式

 意义
"0 0 12 * * ?"   每天中午12点触发
"0 15 10 ? * *"   每天上午10:15触发
"0 15 10 * * ?"   每天上午10:15触发
"0 15 10 * * ? *"   每天上午10:15触发
"0 15 10 * * ? 2005"   2005年的每天上午10:15触发
"0 * 14 * * ?"   在每天下午2点到下午2:59期间的每1分钟触发
"0 0/5 14 * * ?"   在每天下午2点到下午2:55期间的每5分钟触发
"0 0/5 14,18 * * ?"   在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
"0 0-5 14 * * ?"   在每天下午2点到下午2:05期间的每1分钟触发
"0 10,44 14 ? 3 WED"   每年三月的星期三的下午2:10和2:44触发
"0 15 10 ? * MON-FRI"   周一至周五的上午10:15触发
"0 15 10 15 * ?"   每月15日上午10:15触发
"0 15 10 L * ?"   每月最后一日的上午10:15触发
"0 15 10 ? * 6L"   每月的最后一个星期五上午10:15触发 
"0 15 10 ? * 6L 2002-2005"   2002年至2005年的每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6#3"   每月的第三个星期五上午10:15触发

 

附上自己的调用方法

package com.zooper.demo;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ScheduledTasks {
    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    /**
     * 每隔5秒执行一次
     */
    @Scheduled(fixedRate = 5000)
    public void reportCurrentTime() {
        log.info("The time is now {}", dateFormat.format(new Date()));
    }

    /**
     * 根据cron表达式格式触发定时任务
     *  cron表达式格式:
     *      1.Seconds Minutes Hours DayofMonth Month DayofWeek Year
     *      2.Seconds Minutes Hours DayofMonth Month DayofWeek 
     *  顺序:
     *      秒(0~59)
     *      分钟(0~59)
     *      小时(0~23)
     *      天(月)(0~31,但是你需要考虑你月的天数)
     *      月(0~11)
     *      天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
     *      年份(1970-2099)
     * 
     *  注:其中每个元素可以是一个值(如6),一个连续区间(9-12),一个间隔时间(8-18/4)(/表示每隔4小时),一个列表(1,3,5),通配符。
     *  由于"月份中的日期"和"星期中的日期"这两个元素互斥的,必须要对其中一个设置?.
     */
    @Scheduled(cron="5 * * * * ?")
    public void cronScheduled() {
        log.info("cron : The time is now {}", dateFormat.format(new Date()));
    }
}

启用定时任务

package com.zooper.demo;

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

@SpringBootApplication
@EnableScheduling
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class);
    }
}

 

转自: https://www.cnblogs.com/arrrrrya/p/7811090.html

posted @ 2018-04-11 18:45  UniqueColor  阅读(298)  评论(0编辑  收藏  举报