• springboot 优雅的启动类


    优雅的sprongboot启动类

    package fama.cost.server;
    
    import fama.cost.common.IStoppable;
    import fama.cost.common.thread.Stopper;
    import fama.cost.dao.utils.BeanContext;
    import fama.cost.server.bean.SpringApplicationContext;
    import fama.cost.service.quartz.QuartzExecutors;
    import fama.cost.server.runner.MasterSchedulerService;
    import fama.cost.server.zk.ZKClient;
    import org.quartz.SchedulerException;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.WebApplicationType;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.context.annotation.ComponentScan;
    
    import javax.annotation.PostConstruct;
    import java.lang.management.ManagementFactory;
    import java.lang.management.RuntimeMXBean;
    
    @ComponentScan(value = {"fama.cost"})
    public class FamaServerApplication implements IStoppable {
    
        private static final Logger logger = LoggerFactory.getLogger(FamaServerApplication.class);
    
        @Autowired
        private MasterSchedulerService schedulerService;
    
        @Autowired
        private SpringApplicationContext springApplicationContext;
    
        @Autowired
        private BeanContext beanContext;
    
        @Autowired
        private ZKClient zkClient;
    
        public static void main(String[] args) {
            Thread.currentThread().setName("Schedule-Server");
            new SpringApplicationBuilder(FamaServerApplication.class).web(WebApplicationType.NONE).run(args);
            int currentProcessPid = getProcessID();
            logger.info("Current Process Pid : {}",currentProcessPid);
        }
    
        @PostConstruct
        public void run() {
            this.zkClient.start(this);
            this.schedulerService.start();
            try {
                logger.info("start Quartz server...");
                QuartzExecutors.getInstance().start();
            } catch (Exception e) {
                try {
                    QuartzExecutors.getInstance().shutdown();
                } catch (SchedulerException e1) {
                    logger.error("QuartzExecutors shutdown failed : " + e1.getMessage(), e1);
                }
                logger.error("start Quartz failed", e);
            }
    
    
            Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
                @Override
                public void run() {
                    if (Stopper.isRunning()) {
                        close("shutdownHook");
                    }
                }
            }));
        }
    
        public void close(String cause) {
    
            try {
                //execute only once
                if (Stopper.isStopped()) {
                    return;
                }
    
                logger.info("master server is stopping ..., cause : {}", cause);
    
                // set stop signal is true
                Stopper.stop();
    
                try {
                    //thread sleep 3 seconds for thread quietly stop
                    Thread.sleep(3000L);
                } catch (Exception e) {
                    logger.warn("thread sleep exception ", e);
                }
                //close
                this.schedulerService.close();
                this.zkClient.close();
                //close quartz
                try {
                    QuartzExecutors.getInstance().shutdown();
                    logger.info("Quartz service stopped");
                } catch (Exception e) {
                    logger.warn("Quartz service stopped exception:{}", e.getMessage());
                }
            } catch (Exception e) {
                logger.error("master server stop exception ", e);
            } finally {
    //            System.exit(-1);
            }
        }
    
        @Override
        public void stop(String cause) {
            close(cause);
        }
    
        public static final int getProcessID() {
            try {
                RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
                return Integer.valueOf(runtimeMXBean.getName().split("@")[0]).intValue();
            }catch (Exception ex){
                logger.error(ex.getMessage(),ex);
                return 0;
            }
        }
    }
    
    
  • 相关阅读:
    17 正在表达式
    16 css实现模糊背景
    15 VScode 使用相关
    14 CSS题目附答案
    13 form表单
    12 postgresql数据库备份和恢复
    11 vs2015 连接oracle 11g 数据库及相关问题
    10 windows server 2012R2 发布MVC框架网站注意事项
    9 ArcGIS Server 性能优化
    Project Euler P4 Largest palindrome product 题解
  • 原文地址:https://www.cnblogs.com/PythonOrg/p/14596634.html
Copyright © 2020-2023  润新知