java ---- SpringBoot 实现定时任务

SpringBoot 实现定时任务

前言

需求:开发微信小程序的订阅消息,实现定时发送订阅消息任务!

方法一: @EnableScheduling、@Scheduled 实现

1. 开启定时任务的注解 @EnableScheduling
package com.tyfo.app;

import com.tyfo.app.common.utils.ApplicationHolder;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

@EnableScheduling //开启定时任务注解
@MapperScan("com.tyfo.app.model.*.mapper")
@SpringBootApplication
@EnableAsync
@EnableCaching
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        ConfigurableApplicationContext application = SpringApplication.run(Application.class, args);
        ApplicationHolder.setApplication(application);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        //注入application对象实例
        application.application().addInitializers(ApplicationHolder::setApplication);
        return application.sources(Application.class);
    }

}
2. 编写定时任务 @Scheduled
package com.tyfo.app.model.data.service;

import com.tyfo.app.model.api.utils.GetSubscribeMessageTemplateUtil;
import com.tyfo.app.model.api.utils.SendSubscribeMessageUtil;
import com.tyfo.app.model.api.utils.TemplateUtil;
import com.tyfo.app.model.api.web.UserLoginController;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * 定时任务 定时发送订阅消息
 * 指定openID和TEMPLATE_ID进行定时发送消息
 * @author: 11480
 * @create: 2020/11/03 14:39
 **/
@Service
public class ScheduledTaskService {
  private static Logger log4j = LoggerFactory.getLogger(UserLoginController.class);
  public static final String TEMPLATE_ID = "xxxxxxxxxxxxxxxxxxxxx";
  private static final String openid = "xxxxxxxxxxxxxxxxxxxxxxxxx";

  @Scheduled(cron = "0 30 08 ? * *" ) //使用cron属性可按照指定时间执行,本例指的是每天08点30分执行;
  public void fixTimeExecution(){
    GetSubscribeMessageTemplateUtil getSubscribeMessageTemplateUtil = new GetSubscribeMessageTemplateUtil();
    SendSubscribeMessageUtil sendSubscribeMessageUtil = new SendSubscribeMessageUtil();
    try {
      //获取订阅消息
      TemplateUtil template = getSubscribeMessageTemplateUtil.getCorrespondenceTemplate(openid,TEMPLATE_ID);
      //发送订阅消息
      JSONObject jsonResult = sendSubscribeMessageUtil.sendSubscribeMessage(template);
    } catch (Exception e) {
      log4j.error("===============发送订阅信息异常", e);
    }
  }
}
3. @Scheduled 注解参数说明
字段 含义 实例
cron 定时执行的表达式 【0 30 08 ? * *】指每天08点30分执行
zone 执行时间的时区
fixedDelay/fixedDelayString 表示一个固定延迟时间执行,上个任务完成后,延迟多长时间执行 【fixedDelay=5000】5秒后执行
fixedRate/fixedRateString 表示一个固定频率执行,上个任务开始后,多长时间后开始执行 【fixedRate=5000】每隔5秒执行一次
initialDelay/initialDelayString 表示一个初始延迟时间,第一次被调用前延迟的时间 【initialDelay=5000】
4. CronTrigger配置完整格式为: [秒] [分] [小时] [日] [月] [周] [年]
说明 是否必填 允许填写的值 允许的通配符
0-59 , - * /
0-59 , - * /
小时 0-23 , - * /
1-31 , - * ? / L W
1-12或JAN-DEC , - * /
1-7或SUN-SAT , - * ? / L W
empty 或1970-2099 , - * /
5. 通配符说明
通配符 说明 实例
, 表示指定多个值。 在周字段上设置 “MON,WED,FRI” 表示周一,周三和周五触发。
- 表示区间。 在小时上设置 “10-12”,表示 10,11,12点都会触发。
* 表示所有值。 在分的字段上设置 “*”,表示每一分钟都会触发。
? 表示不指定值。使用的场景为不需要关心当前设置这个字段的值。 在每月的10号触发一个操作,但不关心是周几,所以需要周位置的那个字段设置为"?" 具体设置为 0 0 0 10 * ?
/ 用于递增触发。 在秒上面设置"5/15" 表示从5秒开始,每增15秒触发(5,20,35,50)。 在月字段上设置’1/3’所示每月1号开始,每隔三天触发一次。
L 表示最后的意思。 在日字段设置上,表示当月的最后一天(依据当前月份,如果是二月还会依据是否是润年[leap]), 在周字段上表示星期六,相当于"7"或"SAT"。如果在"L"前加上数字,则表示该数据的最后一个。例如在周字段上设置"6L"这样的格式,则表示“本月最后一个星期五"
W 表示离指定日期的最近那个工作日(周一至周五)。 在日字段上设置"15W",表示离每月15号最近的那个工作日触发。如果15号正好是周六,则找最近的周五(14号)触发, 如果15号是周未,则找最近的下周一(16号)触发.如果15号正好在工作日(周一至周五),则就在该天触发。如果指定格式为 “1W”,它则表示每月1号往后最近的工作日触发。如果1号正是周六,则将在3号下周一触发。(注,“W"前只能设置具体的数字,不允许区间”-")

'L’和 'W’可以组合使用。如果在日字段上设置"LW",则表示在本月的最后一个工作日触发;

周字段的设置,若使用英文字母是不区分大小写的,即MON 与mon相同;

猜你喜欢

转载自blog.csdn.net/m0_38082783/article/details/112762992