• 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;
            }
        }
    }
    
    
  • 相关阅读:
    Oracle学习笔记--Oracle启动过程归纳整理
    Oracle 11g rac开启归档
    Oracle 11g rac中关于crsctl stop cluster/crs/has的区别
    Linux环境下安装MySQL 5.7.28
    将root用户权限赋予普通用户
    用Navicat Premium 连接mysql数据库时报错 -- 1130 Host xxxx is not allowed to connect to this MySQL server
    PCoIP vs HDX (CITRIX ICA)远程访问协议对比
    systemctl
    组播查询命令
    默认路由ip default-network和ip route 0.0.0.0 0.0.0.0区别
  • 原文地址:https://www.cnblogs.com/PythonOrg/p/14596634.html
Copyright © 2020-2023  润新知