liurunyu
5 天以前 e1f1023dee5d094fcb1e428f36cce09211c4542a
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.dy.pipIrrModel.timingTask;
 
import com.dy.common.schedulerTask.SchedulerTaskSupport;
import com.dy.pipIrrGlobal.util.Org;
import com.dy.pipIrrGlobal.util.OrgListenerSupport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.io.ResourceLoader;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
 
import java.util.HashMap;
import java.util.List;
 
/**
 * @Author: liurunyu
 * @Date: 2025/8/6 9:32
 * @Description
 * 作物蒸散自动计算
 */
@Slf4j
@Component
public class ModelListener extends OrgListenerSupport implements ApplicationListener<ApplicationReadyEvent> {
 
    protected static final String orgListKey = "tagList" ;
    private static final String JobName = "modelCalculateJob" ;
    private static final String JobGroupName = "modelCalculateGroup" ;
    private static final Integer ThreadPoolMaxCount = 1 ;//线程池线程最大个数
    private static final Integer ThreadPoolPriority = 5 ;//线程优先级
 
    /**
     * 计算开始小时,必须0点或之后,见autoCalculateJob中计算,所用数据是昨天的
     */
    @Value("${model-calculate.startHour: 0}")
    protected Integer startHour;//计算开始时刻--小时
 
    @Value("${model-calculate.startMinute: 10}")
    protected Integer startMinute;//计算开始时刻--分钟
 
    @Autowired
    protected ResourceLoader resourceLoader ;
 
    /**
     * SpringBoot容器已经准备好了,执行下面方法
     * @param event 事件
     */
    @Override
    public void onApplicationEvent(@NonNull ApplicationReadyEvent event) {
        try {
            //等1秒,等待com.alibaba.druid.pool.DruidDataSource初始化完成
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            try{
                super.init(resourceLoader);
                this.start(event);
            }catch (Exception e){
                log.error("注册模型计算定时任务出错", e);
            }
 
        }
    }
    /**
     * 初始化
     */
    private void start(ApplicationReadyEvent event) throws Exception{
        if(startHour < 0 || startHour > 5){
            startHour = 0 ;
        }
        List<Org.OrgVo> orgList = Org.OrgList ;
        if(orgList != null && orgList.size() >0){
            SchedulerTaskSupport.setThreadPoolPro(ThreadPoolMaxCount * orgList.size(), ThreadPoolPriority);
            HashMap<String , Object> jobDataMap    = new HashMap<String , Object>() ;
            jobDataMap.put(orgListKey,orgList) ;
            SchedulerTaskSupport.addDailyJob(JobName, JobGroupName, CalculateJob.class, jobDataMap, startHour, startMinute ) ;
 
        }
    }
}