新聞中心
- JDK定時(shí)器Timer使用及原理分析
- 小頂堆
- 時(shí)間輪算法
- Timer
- 定時(shí)任務(wù)線程池解析
- Schedule
- Leader-Follower模式
- 定時(shí)任務(wù)框架Quartz
- quartz demo
- JobDataMap
- job并發(fā)及持久化
- 觸發(fā)器
- 調(diào)度器
- 整合springboot

完全二叉樹:只有最后一層節(jié)點(diǎn)數(shù)不滿
堆:是一顆完全二叉樹,元素滿足每個(gè)節(jié)點(diǎn)的值都大于或者小于其父節(jié)點(diǎn)的值
數(shù)組存儲(chǔ)結(jié)構(gòu):
小頂堆:最上面的最小
更好的找到父節(jié)點(diǎn)是誰,除以2
插入元素、刪除元素
缺點(diǎn):插入和刪除要移動(dòng)比較全部的元素,需要耗費(fèi)大量性能浪費(fèi)
適合比較偏小量的
鏈表或者數(shù)組實(shí)現(xiàn)時(shí)間輪:遍歷數(shù)組,每個(gè)下標(biāo)放置一個(gè)鏈表,鏈表節(jié)點(diǎn)放置任務(wù),遍歷到了就取出執(zhí)行
round型時(shí)間輪:任務(wù)上記錄一個(gè)round,遍歷到了就將round減一,為0時(shí)取出執(zhí)行
缺點(diǎn):需要遍歷所有的任務(wù),效率較低
分層時(shí)間輪:使用多個(gè)不同時(shí)間維度的輪。天輪記錄幾點(diǎn)執(zhí)行。月輪記錄幾號(hào)執(zhí)行。月輪遍歷到了。將任務(wù)取出放到天輪里面,即可實(shí)現(xiàn)幾號(hào)幾點(diǎn)執(zhí)行。
Timer:任務(wù)啟動(dòng):new()、任務(wù)添加:.schedule()
小頂堆里面添加任務(wù)、從小頂堆里獲取最近的任務(wù)、獲取當(dāng)前時(shí)間和這個(gè)任務(wù)下一次要執(zhí)行的時(shí)間,若、要把任務(wù)刪除掉重新入隊(duì)、預(yù)設(shè)的時(shí)間,任務(wù)真正執(zhí)行時(shí)間取決于上一個(gè)任務(wù)執(zhí)行完的時(shí)間
scheduleAtFixRate():
TimerTask:寫業(yè)務(wù)邏輯、再啟動(dòng)線程池去執(zhí)行
單線程:任務(wù)阻塞、任務(wù)超時(shí)
ScheduledExecutorService scheduledThreadPool = Excutors.newScheduledThreadPool()
scheduleAtFixedRate
避免沒必要的喚醒和阻塞操作,更有效,更節(jié)省資源
定時(shí)任務(wù)框架Quartz quartz demoJob:業(yè)務(wù)邏輯
-JobBuilder、JobDataMap
execute()
JobDetail:Job的包裝
JobBuilder.newJob().withIdentity().build()
Trigger:觸發(fā)器
-TriggerBuilder 、JobDataMap、ScheduleBuilder simple、
.startNow()
.withSchedule()
Scheduler:按照觸發(fā)器定義的時(shí)間去執(zhí)行Job
-SchedulerFactory:StdSchedulerFactory properties配置
.shcheduleJob(jobDetail,triggerr)
存儲(chǔ)一些變量
job并發(fā)及持久化Scheduler每次執(zhí)行,都會(huì)根據(jù)JobDetail創(chuàng)建一個(gè)新的Job實(shí)例,這樣就可以規(guī)避并發(fā)訪問的問題
@DisallowConcurrentExecution:禁止并發(fā)地執(zhí)行同一個(gè)job定義的多個(gè)實(shí)例
前一個(gè)任務(wù)執(zhí)行完了,才會(huì)執(zhí)行下一個(gè)任務(wù)
@PersistJobDataAfterExecution: 將JobDataMap進(jìn)行持久化
觸發(fā)器優(yōu)先級(jí):同時(shí)觸發(fā)的比較優(yōu)先級(jí)
錯(cuò)過觸發(fā):到達(dá)觸發(fā)時(shí)間時(shí)沒有被執(zhí)行、可以配置延遲的閾值
simple:
cron:
JobStore
默認(rèn)存儲(chǔ)在內(nèi)存中
集群處理。每個(gè)節(jié)點(diǎn)執(zhí)行一些任務(wù)
節(jié)點(diǎn)間互相不通信,通過數(shù)據(jù)庫來
quartz.properties
QRTZ
配置文件、建表語句
QuartzJobBean
executeInternal(JobExecutionContext context)
SchedulerConfig
Scheduler按照配置文件創(chuàng)建調(diào)度器
@Bean
Scheduler scheduler()
@Bean
SchedulerFactoryBean
factory.setSchedulerName(“”)
factory.setDataSource()
factory.setApplicationContextSchedulerContextKey(“applicaiton”)
factory.setQuartzProperties(quartzProperties())
factory.setTaskExecutor(schedulerThreadPool())
factory.setStarter
@Bean
DataSource
@Bean
Executor schedulerThreadPool{
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor
executor.setCorePoolSize
executor.setMaxPoolSize
executor.setQueue
@Bean
Properties quartzProperties()
PropertiesFactoryBean propertiesFactoryBean
propertiesFactoryBean.setLocation(new ClassPathResource(“quartz.properties”))
propertiesFactoryBean.afterpeopertiesSet()
return propertiesFactoryBean.getObject
通過監(jiān)聽器啟動(dòng)調(diào)度
StarterApplocationListener implements ApplicationListener()
@Autowired
Scheduler
IOC容器啟動(dòng)起來以后啟動(dòng)調(diào)度
onApplicationEvent(ContextRefreshedEvent event){
TriggerKey triggerKey=TriggerKey.triggerKey()
scheduler.getTrigger(triggerKey);
if(trigger == null) {
trigger=TriggerBuilder.newTrigger()
.withIdentity(triggerKey)
.withSchedule(CronScheduleBuiler.cronSchedule(“0/10 * * * * ?”))
.build();
JobDetail jobDetail = JobBuilder.newJob(QuartzJob.class)
.withIdentity()
.build()
scheduler.scheduleJob(jobDetail,trigger);
sheduler.start();
}
@SpringBootApplication
數(shù)據(jù)庫里存儲(chǔ)了一些歷史數(shù)據(jù)
一個(gè)JobDetail在一個(gè)節(jié)點(diǎn)
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
新聞標(biāo)題:Java定時(shí)任務(wù)-創(chuàng)新互聯(lián)
標(biāo)題URL:http://www.dlmjj.cn/article/hodpp.html


咨詢
建站咨詢
