SpringBoot 使用定时任务动态执行任务

import com.patient.core.adapter.CorsFilter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling//开启定时任务注解
@SpringBootApplication
@MapperScan("com.patient.core.mapper")
public class PatientSystemApplication {

    public static void main(String[] args) {
        SpringApplication.run(PatientSystemApplication.class, args);
    }

    @Bean
    public FilterRegistrationBean setFilter() {
        FilterRegistrationBean filterBean = new FilterRegistrationBean();
        filterBean.setFilter(new CorsFilter());
        filterBean.setName("CorsFilter");
        filterBean.addUrlPatterns("/*");
        return filterBean;
    }
}

  

import com.patient.core.service.UserService;
import com.patient.core.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

import java.util.Date;


@Configuration
public class ReminderTask implements SchedulingConfigurer {
    private static Logger log = LoggerFactory.getLogger(ReminderTask.class);

    @Autowired
    private UserService userService;

  
//项目启动会自动执行该Controller 类,首先执行触发器的方法,查询要执行业务逻辑的时间(需要转换成Cron表达式),动态的执行业务逻辑,业务逻辑执行完毕会再次执行触发器 设定下次执行时间
@Override public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { scheduledTaskRegistrar.addTriggerTask(doTask(), getTrigger()); } // 业务执行方法 private Runnable doTask() { return new Runnable() { @Override public void run() { try { //业务逻辑 log.info("业务执行方法"); } catch (Exception e) { e.printStackTrace(); } } }; } //业务触发器 private Trigger getTrigger() { return new Trigger() { @Override public Date nextExecutionTime(TriggerContext triggerContext) { String dateStr = userService.getLastTime(); if ("".equals(dateStr) || dateStr == null) return null; String cron = DateUtils.getCron(DateUtils.strToDateLong(dateStr)); //定时任务触发,可修改定时任务的执行周期 CronTrigger trigger = new CronTrigger(cron); Date nextExecDate = trigger.nextExecutionTime(triggerContext); log.info("业务触发器:"+DateUtils.dateToStrLong(nextExecDate)); return nextExecDate; } }; }

  

转载于:https://www.cnblogs.com/EveningWind/p/10826649.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值