Hi all: I had make some improvement base on current mechanism and PR is here : https://github.com/apache/incubator-servicecomb-java-chassis/pull/693 , I think it is enough. When user call system.exit(0),these three works will do: 1.Unregister microservice instance from Service Center: this work will do by process ContextClosedEvent, we need unregister immediately for stopping provide service any more. 2.Waiting for all invocations to finish: this work will do by ShutdownHandler, when all invocations finished or 'deadline time reached', cc and transport vertx threads will close. 3.All spring bean do close process we had registerShutdownHook for spring ApplicationContext and beans can define 'destroy-method' do cleaning. here is example: Spring mvc Hello Java Chassis Pojo Hello person ServiceComb/Java Chassis Jaxrs Hello person ServiceComb/Java Chassis Spring mvc Hello person ServiceComb/Java Chassis 2018-05-09 16:02:14,288 [WARN] handler chain is shutting down org.apache.servicecomb.core.handler.ShutdownHookHandler.run(ShutdownHookHandler.java:87) 2018-05-09 16:02:14,289 [INFO] Closing org.springframework.context.support.ClassPathXmlApplicationContext@7cf10a6f: startup date [Wed May 09 16:02:05 CST 2018]; root of context hierarchy org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:984) 2018-05-09 16:02:14,290 [WARN] cse is closing now... org.apache.servicecomb.core.CseApplicationListener.onApplicationEvent(CseApplicationListener.java:148) 2018-05-09 16:02:14,291 [INFO] service center task is shutdown. org.apache.servicecomb.serviceregistry.registry.RemoteServiceRegistry.onShutdown(RemoteServiceRegistry.java:72) 2018-05-09 16:02:14,295 [WARN] handler chain is shut down org.apache.servicecomb.core.handler.ShutdownHookHandler.run(ShutdownHookHandler.java:103) 2018-05-09 16:02:14,296 [INFO] Unregister microservice instance success. microserviceId=90b76fd551c511e8b51db4b676a39f40 instanceId=481f630d535f11e8bc19b4b676a39f40 org.apache.servicecomb.serviceregistry.registry.AbstractServiceRegistry.unregisterInstance(AbstractServiceRegistry.java:232) Process finished with exit code 0 Best Regards! YangYongZheng ------------------ ???????? ------------------ ??????: "wjm wjm"<zzz...@gmail.com>; ????????: 2018??5??9??(??????) ????3:06 ??????: "dev"<dev@servicecomb.apache.org>; ????: Re: [Discussion]What Java Chassis need to do for support Gracefully Shutdown
it's impossible we are a framework, we can handle shutdown work in one place but how can customer do in this places? shutdown process is ordered, if customer's shutdown work processed before us, then exists invocations maybe can not finished. 2018-05-09 14:40 GMT+08:00 Willem Jiang <willem.ji...@gmail.com>: > I think its better to have one place to handle the shutdown work. > > > Willem Jiang > > Blog: http://willemjiang.blogspot.com (English) > http://jnn.iteye.com (Chinese) > Twitter: willemjiang > Weibo: ????willem > > On Tue, May 8, 2018 at 4:36 PM, wjm wjm <zzz...@gmail.com> wrote: > > > eventBus maybe not a good idea > > because eventBus is not ordered, we can not control which one run first. > > > > 2018-05-08 16:33 GMT+08:00 Willem Jiang <willem.ji...@gmail.com>: > > > > > How about register a Vertx thread clean up task in the event bus? > > > > > > Then we just need to trigger the Event after close. > > > > > > And the user can do some extension it by adding event handler which > > listens > > > to the close event. > > > > > > > > > Willem Jiang > > > > > > Blog: http://willemjiang.blogspot.com (English) > > > http://jnn.iteye.com (Chinese) > > > Twitter: willemjiang > > > Weibo: ????willem > > > > > > On Tue, May 8, 2018 at 4:27 PM, ?????? <yangyong.zh...@qq.com> wrote: > > > > > > > Yes, mechanism based on spring had existed, and I had use these code > to > > > > make Gracefully Shutdown work : > > > > > > > > ... start up event code > > > > } else if (event instanceof ContextClosedEvent) { > > > > LOGGER.warn("cse is closing now..."); > > > > triggerEvent(EventType.BEFORE_CLOSE); > > > > > > > > //1.Unregister microservice instance from Service Center > > > > //it contains eventBus.post(new ShutdownEvent());I think we may > > > move > > > > out from it > > > > RegistryUtils.destroy(); > > > > > > > > //2.Stop all network transport threads > > > > for (String vertx : VertxUtils.getVertxMap(). > > keySet().toArray(new > > > > String[0])) { > > > > VertxUtils.getVertxMap().get(vertx).close(); > > > > VertxUtils.getVertxMap().remove(vertx); > > > > } > > > > > > > > //3.Close ApplicationContext > > > > if (applicationContext instanceof AbstractApplicationContext) { > > > > ((AbstractApplicationContext) applicationContext).close(); > > > > } > > > > > > > > //4.Raise an ShutdownEvent in EventBus in order to notify > > > > components do clean works > > > > //eventBus.post(new ShutdownEvent()); > > > > > > > > triggerEvent(EventType.AFTER_CLOSE); > > > > isInit = false; > > > > } > > > > > > > > > > > > And I think we can use EventBus listening ShutdownEvent rather than > > only > > > > use SPI, may even simpler ? > > > > > > > > Best Regards! > > > > YangYongZheng > > > > > > > > ------------------ ???????? ------------------ > > > > ??????: "wjm wjm"<zzz...@gmail.com>; > > > > ????????: 2018??5??8??(??????) ????3:50 > > > > ??????: "dev"<dev@servicecomb.apache.org>; > > > > > > > > ????: Re: [Discussion]What Java Chassis need to do for support > > Gracefully > > > > Shutdown > > > > > > > > > > > > > > > > we already have this mechanism, but listener depend on spring bean. > > > > so we can improve it: > > > > 1.allow listener registered by SPI > > > > 2.modules should register listener to do their clean up works > > > > 3.clean up maybe must ordered, so listener add getOrder method. > > > > > > > > 2018-05-08 14:56 GMT+08:00 Willem Jiang <willem.ji...@gmail.com>: > > > > > > > > > My suggestion is register a shutdown callback (to do 1,2,4) in the > > > > > ApplicationContext. > > > > > In this way we can do some clean up work with or without Spring. > > > > > > > > > > > > > > > Willem Jiang > > > > > > > > > > Blog: http://willemjiang.blogspot.com (English) > > > > > http://jnn.iteye.com (Chinese) > > > > > Twitter: willemjiang > > > > > Weibo: ????willem > > > > > > > > > > On Tue, May 8, 2018 at 2:53 PM, ?????? <yangyong.zh...@qq.com> wrote: > > > > > > > > > > > Hi All: > > > > > > In order to let Java Chassis support Gracefully Shutdown, I > > think > > > > need > > > > > > to do these works : > > > > > > 1.Unregister microservice instance from Service Center(clean > > > service > > > > > > registry). > > > > > > 2.Stop all network transport threads (vertx > > eventloop-threads)(to > > > > > > prevent blocking exit). > > > > > > 3.Close ClassPathXmlApplicationContext created in > > BeanUtils.init() > > > > and > > > > > > release all resources and locks. > > > > > > 4.Raise an ShutdownEvent in EventBus. > > > > > > > > > > > > Also if user directly call `System.exit(0)` , all these works > > will > > > > > done > > > > > > and jvm can terminate with `Process finished with exit code 0`. > > > > > > > > > > > > What's missing or any suggestions are welcome ! > > > > > > > > > > > > Best Regards! > > > > > > YangYongZheng > > > > > > > > > > > > > > >