liurunyu
2024-07-22 bafb6d7ee1e434097d9412a2d63ab6960ca4062d
实现月统计Listener
1个文件已修改
1个文件已添加
75 ■■■■■ 已修改文件
pipIrr-platform/pipIrr-global/src/main/resources/application-global.yml 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-web/pipIrr-web-statistics/src/main/java/com/dy/pipIrrStatistics/listeners/StatisticsMonthListener.java 68 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-global/src/main/resources/application-global.yml
@@ -149,3 +149,10 @@
        - /sso/sso
        - /remote/comRes/receive
        - /wx/comRes/receive
#自动统计配置
auto-statistics:
    #月统计
    month:
        startDay: 1
        startHour: 0
        startMinute: 5
pipIrr-platform/pipIrr-web/pipIrr-web-statistics/src/main/java/com/dy/pipIrrStatistics/listeners/StatisticsMonthListener.java
New file
@@ -0,0 +1,68 @@
package com.dy.pipIrrStatistics.listeners;
/**
 * @Author: liurunyu
 * @Date: 2024/7/22 14:39
 * @Description
 */
import com.dy.common.util.ConfigXml;
import com.dy.common.webListener.ConfigListener;
import org.jdom2.Document;
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;
import java.net.URL;
/**
 * 监听器,实现功能:在系统启动时初始化,向数据库中插入数据
 * 本监听器不能采用ServletContextListener方式,因为Servlet上下文Context创建后
 * Spring容器并没有创建完,而本类中用了Spring容器中的Bean,即*Dao 。
 * 所以采用了Spring事件监听器来实现
 */
@Component
public class StatisticsMonthListener implements ApplicationListener<ApplicationReadyEvent> {
    @Value("${auto-statistics.month.startDay: 1}")
    private Integer startDay ;//月统计开始日期
    @Value("${auto-statistics.month.startHour: 0}")
    private Integer startHour ;//月统计开始小时
    @Value("${auto-statistics.month.startMinute: 5}")
    private 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 {
            this.start(event);
        }
    }
    /**
     * 实始化
     */
    private void start(ApplicationReadyEvent event){
        try {
            URL configFileURL = ConfigListener.class.getResource("/init-config.xml" );
            ConfigXml configXml = new ConfigXml() ;
            Document doc = configXml.createDom(configFileURL) ;
            this.doInit(configXml, doc);
        } catch (Exception e) {
            System.out.println("系统启动时,初始化配置出错 !");
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}