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
84
85
86
87
88
package com.dy.pipIrrStatistics.statistics;
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: 2024/7/22 14:39
 * @Description
 * 监听器,实现功能:在系统启动时初始化,向数据库中插入数据
 * 本监听器不能采用ServletContextListener方式,因为Servlet上下文Context创建后
 * Spring容器并没有创建完,而本类中用了Spring容器中的Bean,即*Dao 。
 * 所以采用了Spring事件监听器来实现
 */
@Slf4j
@Component
public class StatisticsListener extends OrgListenerSupport  implements ApplicationListener<ApplicationReadyEvent> {
 
    protected static final String orgKey = "tag" ;
    private static final String JobName = "statisticsJob" ;
    private static final String JobGroupName = "statisticsGroup" ;
    private static final Integer ThreadPoolMaxCount = 1 ;//线程池线程最大个数
    private static final Integer ThreadPoolPriority = 5 ;//线程优先级
 
    /**
     * 统计开始小时,必须0点或之后,见StatisticsJob中统计,日统计是统计昨天的
     */
    @Value("${auto-statistics.startHour: 0}")
    protected Integer startHour;//统计开始小时
 
    @Value("${auto-statistics.startMinute: 5}")
    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);
            for(Org.OrgVo vo : orgList){
                HashMap<String , Object> jobDataMap    = new HashMap<String , Object>() ;
                jobDataMap.put(orgKey, vo.tag) ;
                SchedulerTaskSupport.addDailyJob(JobName + vo.tag, JobGroupName, StatisticsJob.class, jobDataMap, startHour, startMinute ) ;
                //SchedulerTaskSupport.addSecondlyJob(JobName + vo.tag, JobGroupName, TestJob.class, jobDataMap, 10, 10000, 0 ) ;
            }
        }
    }
}