feiyu02
2022-07-28 e844ef2fdab88508e7dff4bb9e7b1632fcce15b2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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)
    }
}