zoukankan      html  css  js  c++  java
  • Spring-IOC 在非 web 环境下优雅关闭容器

    当我们设计一个程序时,依赖了Spring容器,然而并不需要spring的web环境时(Spring web环境已经提供了优雅关闭),即程序启动只需要启动Spring ApplicationContext即可,那我们如何去进行优雅关闭呢?

    设计一个代理程序,仅需要Spring容器管理部分bean并启动即可。该工程最终打成一个可执行Jar包,并构建成docker镜像后进行启动

    public class Main {
        public static void main(String[] args) throws InterruptedException {
            ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath*:services.xml");
            applicationContext.start();
        }
    }
    

    有一个bean开启线程执行业务任务

    @Component
    public class HelloLifeCycle implements Lifecycle {
        private volatile boolean running = false;
        private volatile boolean businessRunning = true;
    
    
        private ExecutorService executors = Executors.newFixedThreadPool(1);
    
        public HelloLifeCycle() {
            executors.execute(() -> {
                while (businessRunning) {
                   //启动后,做业务需要做的事情
                }
            });
        }
    
    
        public void start() {
            logger.info("lifycycle start");
            running = true;
    
        }
    
        public void stop() {
            businessRunning=false;
            logger.info("lifycycle stop ,and stop the execute");
            executors.shutdown();
            try {
                executors.awaitTermination(1, TimeUnit.HOURS);
            } catch (InterruptedException e) {
            }
            running = false;
        }
    
        public boolean isRunning() {
            return running;
        }
    }
    

    该业务类实现了Spring的LifeCycle钩子,Spring在调用其context的start()和stop()方法时会回调业务类实现的start和stop方法

    优雅关闭

    如果就像上面的Main启动类,我们直接kill这个进程时,Spring是不会优雅关闭的,从而不会调用stop方法

    public class Main {
        public static void main(String[] args) throws InterruptedException {
            ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath*:services.xml");
            applicationContext.registerShutdownHook();
            applicationContext.start();
        }
    }
    

    当注册了JVM钩子后,即可以实现优雅关闭

  • 相关阅读:
    MyISAM表锁的解决方案
    RSA数字证书管理
    Self Host WebApi服务传输层SSL加密(服务器端+客户端调用)
    WebApi服务Uri加密及验证的两种方式
    利用MVC的自定义过滤器FilterAttribute、IActionFilter、IExceptionFilter实现异常处理等功能
    html页面中meta的作用
    [转]REST简介
    [转]webApi 参数传递总结
    REST服务中的异常处理
    REST服务返回自定义的HttpResponseMessage
  • 原文地址:https://www.cnblogs.com/leihuazhe/p/9623069.html
Copyright © 2011-2022 走看看