How to prevent Spring app context shutdown until shutdown hook is fired

前端 未结 2 1537
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-22 12:45

I have a spring-boot application.

I have implemented SmartLifecycle interface in my bean which starts async snmp server in it\'s start method and stops it i

2条回答
  •  执笔经年
    2021-01-22 13:31

        SpringApplication app = new SpringApplication(Main.class);
        app.setRegisterShutdownHook(false);
        ConfigurableApplicationContext applicationContext= app.run();
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            @Override
            public void run() {
                //do your things
                applicationContext.close();
            }
        }));
    

提交回复
热议问题