原文网址:Spring容器生命周期--SmartLifecycle的用法_IT利刃出鞘的博客-CSDN博客
说明
本文介绍Spring容器生命周期SmartLifeCycle的用法。
Bean的初始化方法和销毁方法是Bean生命周期级别的;而Lifecycle是容器生命周期级别的。如果我们想在容器本身的生命周期(比如容器启动完成后、停止之前)事件上做一些工作,可以使用LifeCycle接口。
SmartLifecycle是Lifecycle的一个子接口,比Lifecycle有更丰富的功能。其中有两点最重要的改进:
方法名 | 描述 |
isAutoStartup() | 一定要返回true。否则,容器启动时不会回调start()方法。 true:让Lifecycle类所在的上下文在调用`refresh`时,能够自己自动进行回调。 |
getPhase() | 控制多个SmartLifecycle的回调顺序的:越小:start()方法越靠前,stop()方法越靠后 |
isRunning() | 与Lifecycle接口中的功能一样,用来判你的断组件是否在运行。 |
start() | 如果类A没在运行(isRunning返回false),则走该方法;否则不走该方法。 当刷新容器(启动完成)时调用。跟Lifecycle接口一样。 |
stop(Runnable callback) | 当容器停止时,回调该方法。 当执行完你自定义的逻辑后,一定要调用下callback.run(); 这是为了,告诉容器你已停止自己的组件完成。 此方法是有超时时间的,默认为30s。可以通过以下方式设置超时时间 |
stop() | 需要自己调用。不会被Spring框架调用到 |
stop(Runnable callback)
在SmartLifecycle中定义的停止方法接收一个回调函数。任何实现在关闭进程完成之后都必须调用回调的run()方法。当需要时这可以进行异步关闭,因为LifecycleProcessor接口、DefaultLifecycleProcessor接口的默认实现会等待每个阶段的对象组直到达到超时值,然后调用回调函数。默认每个阶段的超时值为30秒。可以在上下文中通过定义名为”lifecycleProcessor”的bean来覆盖默认的生命周期处理器实例。如果你只想修改超时值,这样改即可:
- <bean id="lifecycleProcessor" class="org.springframework.context.support.DefaultLifecycleProcessor">
-
- <property name="timeoutPerShutdownPhase" value="10000"/>
- bean>
- package com.example.config;
-
- import org.springframework.context.SmartLifecycle;
- import org.springframework.stereotype.Component;
-
- @Component
- public class MySmartLifecycle implements SmartLifecycle {
- private volatile boolean running = false;
-
- /**
- * true:让Lifecycle类所在的上下文在调用`refresh`时,能够自己自动进行回调
- * false:表明组件打算通过显式的start()调用来启动,类似于普通的Lifecycle实现。
- */
- @Override
- public boolean isAutoStartup() {
- return true;
- }
-
- /**
- * 很多框架中,把真正逻辑写在stop()方法内。比如quartz和Redis的spring支持包
- */
- @Override
- public void stop(Runnable callback) {
- System.out.println("stop(callback)");
- stop();
- callback.run();
- }
-
- @Override
- public void start() {
- System.out.println("start()");
- running = true;
- }
-
- @Override
- public void stop() {
- System.out.println("stop()");
- running = false;
- }
-
- @Override
- public boolean isRunning() {
- System.out.println("isRunning()");
- return running;
- }
-
- /**
- * 阶段值。越小:start()方法越靠前,stop()方法越靠后
- */
- @Override
- public int getPhase() {
- System.out.println("getPhase()");
- return 0;
- }
- }
启动时:
- . ____ _ __ _ _
- /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
- ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
- \\/ ___)| |_)| | | | | || (_| | ) ) ) )
- ' |____| .__|_| |_|_| |_\__, | / / / /
- =========|_|==============|___/=/_/_/_/
- :: Spring Boot :: (v2.3.0.RELEASE)
-
- 2021-03-09 19:30:57.138 INFO 6600 --- [ main] com.example.DemoApplication : Starting DemoApplication on DESKTOP-QI6B9ME with PID 6600 (E:\work\Idea_proj\demo_JAVA\demo_SpringBoot\target\classes started by Liu in E:\work\Idea_proj\demo_JAVA\demo_SpringBoot)
- 2021-03-09 19:30:57.143 INFO 6600 --- [ main] com.example.DemoApplication : No active profile set, falling back to default profiles: default
- 2021-03-09 19:30:57.899 INFO 6600 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
- 2021-03-09 19:30:57.907 INFO 6600 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
- 2021-03-09 19:30:57.907 INFO 6600 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.35]
- 2021-03-09 19:30:57.986 INFO 6600 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
- 2021-03-09 19:30:57.986 INFO 6600 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 808 ms
- 2021-03-09 19:30:58.135 INFO 6600 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
- getPhase()
- isRunning()
- start()
- 2021-03-09 19:30:58.260 INFO 6600 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
- 2021-03-09 19:30:58.268 INFO 6600 --- [ main] com.example.DemoApplication : Started DemoApplication in 1.442 seconds (JVM running for 2.44)
关闭时:
- getPhase()
- isRunning()
- stop(callback)
- stop()