配置流程
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分布式锁获取单个锁,*