package cn.flightfeather.supervision.datafetch
|
|
import org.springframework.stereotype.Component
|
import java.util.*
|
import java.util.concurrent.*
|
|
/**
|
* 数据获取控制器
|
*/
|
@Component
|
class FetchController(
|
fetchNightConstruction: FetchNightConstruction
|
) {
|
|
companion object {
|
private const val FETCH_PERIOD_MIN = 30L
|
private const val MAINTAIN_PERIOD_MIN = 15L
|
}
|
|
private val fetchData = mutableListOf<FetchData>()
|
private var shecdule = Executors.newScheduledThreadPool(2)
|
private var fetchTime: Date = Date()
|
private var maintainTime: Date = Date()
|
|
init {
|
fetchData.add(fetchNightConstruction)
|
}
|
|
fun run() {
|
println("【飞羽监管】数据获取任务启动")
|
fetchTask()
|
maintainTask()
|
}
|
|
private fun fetchTask() {
|
shecdule.scheduleAtFixedRate({
|
fetchTime = Date()
|
if (Date().time - maintainTime.time > (MAINTAIN_PERIOD_MIN + 1) * 60 * 1000) {
|
maintainTask()
|
}
|
fetchData.forEach {
|
it.fetch()
|
}
|
}, 0, FETCH_PERIOD_MIN, TimeUnit.MINUTES)
|
}
|
|
private fun maintainTask() {
|
|
shecdule.scheduleAtFixedRate({
|
maintainTime = Date()
|
if (Date().time - fetchTime.time > (FETCH_PERIOD_MIN + 1) * 60 * 1000) {
|
fetchTask()
|
}
|
}, 0, MAINTAIN_PERIOD_MIN, TimeUnit.MINUTES)
|
}
|
}
|