定时任务 - 构建定时任务task
生活随笔
收集整理的這篇文章主要介紹了
定时任务 - 构建定时任务task
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
@Component
public class OrderJob {@Autowiredprivate OrderService orderService;/*** 使用定時(shí)任務(wù)關(guān)閉超期未支付訂單,會(huì)存在的弊端:* 1. 會(huì)有時(shí)間差,程序不嚴(yán)謹(jǐn)* 10:39下單,11:00檢查不足1小時(shí),12:00檢查,超過1小時(shí)多余39分鐘* 2. 不支持集群* 單機(jī)沒毛病,使用集群后,就會(huì)有多個(gè)定時(shí)任務(wù)* 解決方案:只使用一臺(tái)計(jì)算機(jī)節(jié)點(diǎn),單獨(dú)用來運(yùn)行所有的定時(shí)任務(wù)* 3. 會(huì)對(duì)數(shù)據(jù)庫(kù)全表搜索,及其影響數(shù)據(jù)庫(kù)性能:select * from order where orderStatus = 10;* 定時(shí)任務(wù),僅僅只適用于小型輕量級(jí)項(xiàng)目,傳統(tǒng)項(xiàng)目** 后續(xù)課程會(huì)涉及到消息隊(duì)列:MQ-> RabbitMQ, RocketMQ, Kafka, ZeroMQ...* 延時(shí)任務(wù)(隊(duì)列)* 10:12分下單的,未付款(10)狀態(tài),11:12分檢查,如果當(dāng)前狀態(tài)還是10,則直接關(guān)閉訂單即可*/// @Scheduled(cron = "0/3 * * * * ?")
// @Scheduled(cron = "0 0 0/1 * * ?")public void autoCloseOrder() {orderService.closeOrder();System.out.println("執(zhí)行定時(shí)任務(wù),當(dāng)前時(shí)間為:"+ DateUtil.getCurrentDateString(DateUtil.DATETIME_PATTERN));}}
/*** A cron-like expression, extending the usual UN*X definition to include triggers* on the second as well as minute, hour, day of month, month and day of week.* <p>E.g. {@code "0 * * * * MON-FRI"} means once per minute on weekdays* (at the top of the minute - the 0th second).* <p>The special value {@link #CRON_DISABLED "-"} indicates a disabled cron trigger,* primarily meant for externally specified values resolved by a ${...} placeholder.* @return an expression that can be parsed to a cron schedule* @see org.springframework.scheduling.support.CronSequenceGenerator*/
String cron() default "";
@EnableScheduling // 開啟定時(shí)任務(wù)
?
總結(jié)
以上是生活随笔為你收集整理的定时任务 - 构建定时任务task的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 支付宝支付 - 异步通知与同步通知
- 下一篇: 定时任务 - 定时关闭超期未支付订单