springboot 定时任务

配置流程

1、在@SpringBootApplication注解的全局应用类上,添加@Eablescheduling

@SpringBootApplication
@EnableCaching
@MapperScan( "org.javaboy.vhr.mapper")
@EnableScheduling
public class VhrApplication {

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

}

在 application 启动类中使用 @EnableScheduling 注解开启定时任务,会自动扫描,相当于一个开关,把这个开关开完之后,那么只要在相应的任务类中做相应的任务,那么就会被 spring boot 容器扫描到,扫描到后,根据任务定义的时间会自动运行。

2、任务类MailSendTask

将任务类注册为组件,注解定时任务表达式@Scheduled(cron = “0/10 * * * * ?”)

@Component
public class MailSendTask {
    @Autowired
    MailSendLogService mailSendLogService;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    EmployeeService employeeService;
    @Scheduled(cron = "0/10 * * * * ?")
    public void mailResendTask() {
        List<MailSendLog> logs = mailSendLogService.getMailSendLogsByStatus();
        if (logs == null || logs.size() == 0) {
            return;
        }
        logs.forEach(mailSendLog->{
            if (mailSendLog.getCount() >= 3) {
                mailSendLogService.updateMailSendLogStatus(mailSendLog.getMsgId(), 2);//直接设置该条消息发送失败
            }else{
                mailSendLogService.updateCount(mailSendLog.getMsgId(), new Date());
                Employee emp = employeeService.getEmployeeById(mailSendLog.getEmpId());
                rabbitTemplate.convertAndSend(MailConstants.MAIL_EXCHANGE_NAME, MailConstants.MAIL_ROUTING_KEY_NAME, emp, new CorrelationData(mailSendLog.getMsgId()));
            }
        });
    }
}

3、定时配置两种方式

@Scheduled(cron = “0/10 * * * * ?”)
@Scheduled(fixedRate = 1000)

4、分布式部署需要考虑,定时时间都是参考的上一次执行的时间,而上一次不能保证在同一服务器上执行的,所以参考时间就需要以下方案来保证参考时间的有效性。

1 可以选择代码分离进行单台部署,

2 可以选择redis分布式锁,让一台服务拿到相应的key之后去执行

3 使用zookeeper分布式锁获取单个锁,*

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值