package com.dy.pipIrrStatistics.statistics;
|
|
/**
|
* @Author: liurunyu
|
* @Date: 2024/7/22 14:39
|
* @Description
|
*/
|
|
import com.dy.common.schedulerTask.SchedulerTaskSupport;
|
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.Logger;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
import org.springframework.context.ApplicationListener;
|
import org.springframework.lang.NonNull;
|
import org.springframework.stereotype.Component;
|
|
/**
|
* 监听器,实现功能:在系统启动时初始化,向数据库中插入数据
|
* 本监听器不能采用ServletContextListener方式,因为Servlet上下文Context创建后
|
* Spring容器并没有创建完,而本类中用了Spring容器中的Bean,即*Dao 。
|
* 所以采用了Spring事件监听器来实现
|
*/
|
@Component
|
public class StatisticsListener implements ApplicationListener<ApplicationReadyEvent> {
|
|
private static Logger log = LogManager.getLogger(StatisticsListener.class.getName()) ;
|
|
private static final String JobName = "statisticsJob" ;
|
private static final String JobGroupName = "statisticsGroup" ;
|
private static final Integer ThreadPoolMaxCount = 1 ;//线程池线程最大个数
|
private static final Integer ThreadPoolPriority = 5 ;//线程优先级
|
|
@Value("${auto-statistics.startHour: 0}")
|
protected Integer startHour;//统计开始小时
|
|
@Value("${auto-statistics.startMinute: 5}")
|
protected Integer startMinute;//统计开始分钟
|
|
/**
|
* 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{
|
this.start(event);
|
}catch (Exception e){
|
log.error("注册统计定时任务出错", e);
|
}
|
|
}
|
}
|
/**
|
* 初始化
|
*/
|
private void start(ApplicationReadyEvent event) throws Exception{
|
SchedulerTaskSupport.setThreadPoolPro(ThreadPoolMaxCount, ThreadPoolPriority);
|
SchedulerTaskSupport.addDailyJob(JobName, JobGroupName, StatisticsJob.class, null, startHour, startMinute ) ;
|
}
|
}
|