import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@EnableScheduling
public class SchedulerTask{
/*
*
* DESC : 编写定时任务,每5秒输出一次
*
*/
@Scheduled(cron = "*/5 * * * * ?")
private void scheduleTask(){
System.out.println("Schedule............");
}
}
如果想要在项目启动时就执行一次定时任务,可以实现InitializingBean接口
import org.springframework.beans.factory.InitializingBean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@EnableScheduling
public class SchedulerTask implements InitializingBean{
/*
*
* DESC : 编写定时任务,每5秒输出一次
*
*/
@Scheduled(cron = "*/5 * * * * ?")
private void scheduleTask(){
System.out.println("Schedule............");
}
@Override
public void afterPropertiesSet() throws Exception {
this.scheduleTask(); //调用定时任务,项目初始化就会执行一次
}
}
有关触发器时间表达式corn配置
每隔5秒执行一次任务: "*/5 * * * * ?"
每隔1分钟执行一次任务: "0 */1 * * * ?"
每天23点执行一次任务: "0 0 23 * * ?"
每天凌晨1点执行一次任务: "0 0 1 * * ?"
每月1号凌晨1点执行一次任务: "0 0 1 1 * ?"
每月1号凌晨2点执行一次任务: "0 0 2 1 * ? *"
每月最后一天23点执行一次任务: "0 0 23 L * ?"
每周星期天凌晨1点执行一次任务: "0 0 1 ? * L"
26分、29分、33分各执行一次任务: "0 26,29,33 * * * ?"
每天的0点、13点、18点、21点各执行一次任务: "0 0 0,13,18,21 * * ?"
周一到周五每天上午10:15执行一次任务: "0 15 10 ? * MON-FRI"
2020-2021年的每个月的最后一个星期五上午10:15执行一次任务: "0 15 10 ? 6L 2020-2021"
#在线配置
https://qqe2.com/cron
定时任务编写试验
@Slf4j
@Component
@RestController
@AllArgsConstructor
@ApiOperation(value = "临时额度到期逻辑处理")
public class ResidualMarginTask {
/**
* 基本信息
*/
@Autowired
private ChannelQuotaInfoService channelQuotaInfoService;
/**
* 每天上午10点、下午14点、下午16点执行
*/
@Scheduled(cron = "0 0 10,14,16 * * ?")
public void start() {
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
//获取当前日期时间
Long dayTime = calendar.getTime().getTime();
//Step1:查询额度信息表数据并更新数据库
List<ChannelQuotaInfo> list = channelQuotaInfoService.list(Wrappers.<ChannelQuotaInfo>query().lambda());
List<ChannelQuotaInfo> quotaInfoList = new ArrayList<>();
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
ChannelQuotaInfo info = list.get(i);
Long endTime = info.getValidityTermEnd().getTime();
//判断如果当前日期大于临时额度有效期:表示临时额度已过期,需要将临时额度清零,且剩余额度对应减少
if (dayTime >= endTime) {
//获取剩余额度
BigDecimal surplusQuota = info.getSurplusQuota() == null ? BigDecimal.ZERO : info.getSurplusQuota();
//获取临时额度
BigDecimal tempQuota = info.getTempQuota() == null ? BigDecimal.ZERO : info.getTempQuota();
//剩余额度=剩余额度-临时额度
info.setSurplusQuota(surplusQuota.subtract(tempQuota));
//临时额度清零
info.setTempQuota(BigDecimal.ZERO);
channelQuotaInfoService.updateById(info);
quotaInfoList.add(info);
}
}
//Step2: 调用渠道服务更新渠道子系统额度信息
channelQuotaInfoService.synchronousLimitToChannel(quotaInfoList);
}
}
}