You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by "rangtao (GitHub)" <gi...@apache.org> on 2020/02/25 01:47:17 UTC

[GitHub] [dubbo] rangtao opened issue #5784: 优雅停机在Spring容器下通过ApplicationListener注册事件无法实现事件执行顺序(不支持应用自定义事件)

你好:
    我们在使用Dubbo框架进行优雅停机时,需要自定义一部分停机逻辑,一般情况此部分逻辑需放到Dubbo框架停机前,我们在实际使用过程中遇到如下问题。
方案1:应用自定义java停机hook,由于java多个hook之间无序执行,无法保证在dubbo框架停机前执行我们的逻辑;
方案2:因为dubbo在spring容器中优雅停机是注册了spring的事件(ApplicationListener),我们也通过注册spring的事件执行应用测停机逻辑。现在问题是多个ApplicationListener是无序的(本质是ApplicationListener的order是默认最大)。

我们建议dubbo框架注册Spring事件监听使用可排序的SmartApplicationListener,并初始化order,例如20?

* Dubbo version: 2.5.x-2.7.x
---------源程序-------------
/**
 * SpringExtensionFactory
 */
    private static class ShutdownHookListener implements ApplicationListener {
        @Override
        public void onApplicationEvent(ApplicationEvent event) {
            if (event instanceof ContextClosedEvent) {
                DubboShutdownHook shutdownHook = DubboShutdownHook.getDubboShutdownHook();
                shutdownHook.doDestroy();
            }
        }
    }
---------建议修改为如下-------------
    private static class ShutdownHookListener implements SmartApplicationListener {
        @Override
        public void onApplicationEvent(ApplicationEvent event) {
            if (event instanceof ContextClosedEvent) {
                DubboShutdownHook shutdownHook = DubboShutdownHook.getDubboShutdownHook();
                shutdownHook.doDestroy();
            }
        }
	@Override
	public int getOrder() {
		// TODO Auto-generated method stub
		return 20;
	}

	@Override
	public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
		// TODO Auto-generated method stub
		return eventType == ContextClosedEvent.class;
	}

	@Override
	public boolean supportsSourceType(Class<?> arg0) {
		// TODO Auto-generated method stub
		return true;
	}

    }

[ Full content available at: https://github.com/apache/dubbo/issues/5784 ]
This message was relayed via gitbox.apache.org for notifications@dubbo.apache.org