@Scheduled注解=====》spring框架提供的
class类放在SpringBoot的启动类下的所在的包或者启动类所在的包的子包下
/**
* 定时任务扫描组件
*/
@Configuration
@EnableScheduling
public class BookingPhoneStatusJob {
public static final Logger logger = LoggerFactory.getLogger(BookingPhoneStatusJob.class);
@Autowired
private BookingCardService bookingCardService;
/**
* 每1天执行一次号码池状态任务
*/
@Scheduled(fixedRate = 24 * 60 * 60 * 1000L)
private void configureTasks() {
try {
bookingCardService.updateStatusByIdInTimer();
}catch (Exception e){
logger.error("每1天执行一次号码池状态任务出错",e);
}
}
}
@ElasticJobConfig注解,当当的定时任务,要引入相应的依赖【如果和guaua共同引入了,记得<exclusion>一下】
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-core</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-spring</artifactId>
<version>2.1.5</version>
</dependency>
/**
* 将已过期的业务设置为过期状态
* 每天晚上00:00:00开始更新状态
*/
@Slf4j
@ElasticJobConfig(cron = "0 0 0 * * ?")
public class NoticeStatusJob extends ElasticJobSupport {
@Autowired
private NoticeBaseInfoService noticeBaseInfoService;
@Override
public void execute(ShardingContext shardingContext) {
log.info("--- 开始更新配置的过期状态 ----");
try {
Boolean result = this.noticeBaseInfoService.updateNoticeStatusJob();
} catch (Exception exp) {
log.error("更新配置失败", exp);
}
log.info("--- 更新配置完成 --");
}