• springboot启动后和停止前执行方法


    springboot启动后即执行的方法

    1)实现ApplicationRunner接口

    @Configuration
    public class ApplicationService implements ApplicationRunner {
        @Override
        public void run(ApplicationArguments args) throws Exception {
            iForwardQueuesService.create();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2)实现CommandLineRunner接口

    @Configuration
    public class ApplicationService implements CommandLineRunner {
        @Override
        public void run(String... args) throws Exception {
            log.info("执行平台登出");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    注意:如果ApplicationListener和CommandLineRunner同时存在,则ApplicationRunner接口先执行,CommandLineRunner后执行;
    也可以使用执行执行顺序

    @Configuration
    @Order(1)
    public class ApplicationService implements CommandLineRunner {
    }
    
    • 1
    • 2
    • 3
    • 4

    原理:
    SpringApplication 的run方法会执行afterRefresh方法。
    afterRefresh方法会执行callRunners方法。
    callRunners方法会调用所有实现ApplicationRunner和CommondLineRunner接口的方法。

    springboot停止前执行的方法

    1)实现DisposableBean接口并实现destroy方法
    springboot销毁时执行

    @Configuration
    public class ApplicationService implements DisposableBean,{
        @Override
        public void destroy() throws Exception {
            log.info("执行平台登出");
            platformService.PlatformLogout();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2)使用ShutdownHook关闭钩子
    JAVA虚拟机关闭钩子(Shutdown Hook)在下面场景下被调用:

    1. 程序正常退出;
    2. 使用System.exit();
    3. 终端使用Ctrl+C触发的中断;
      4)系统关闭;
      5)OutOfMemory宕机;
    4. 使用Kill pid命令干掉进程(注:在使用kill -9 pid时,是不会被调用的);
    @SpringBootApplication
    @ComponentScan(value = "com.xxxxxx")
    public class ForwardGbApplication {
        public static void main(String[] args) {
            ForwardGbApplication application=new ForwardGbApplication();
            Thread t = new Thread(new ShutdownHook(application), "ShutdownHook-Thread");
            Runtime.getRuntime().addShutdownHook(t);
            SpringApplication.run(ForwardGbApplication.class, args);
        }
        static class ShutdownHook implements Runnable{
            private ForwardGbApplication manager;
            public ShutdownHook(ForwardGbApplication serverManager){
                manager = serverManager;
            }
            @Override
            public void run() {
                try {
                    PlatformService platform = ApplicationContextHandle.getObject(PlatformService.class);
                    platform.PlatformLogout();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    RunTime.getRunTime().addShutdownHook的作用就是在JVM销毁前执行的一个线程.当然这个线程依然要自己写.

  • 相关阅读:
    冲击继电器JC-7/11/DC110V
    cache line提升程序性能
    HPC集群自动弹性扩缩的两种实现方式
    使用vue开发中一些图片保存的方法
    Windows下安装RabbitMQ的步骤
    【第三天】C++类和对象进阶指南:从堆区空间操作到友元的深度掌握
    转行UI设计必看的5个建议优漫动游
    1011 A + B 和 C【PAT (Basic Level) Practice (中文)】
    c语言的三种基本结构——初学者一定要了解哦
    前端Vue返回顶部和底部四个角[样式加方法](代源码和详图)
  • 原文地址:https://blog.csdn.net/u010833154/article/details/126591324