91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

dubbo中AwaitingNonWebApplicationListener的實例介紹

發布時間:2021-09-08 16:06:13 來源:億速云 閱讀:142 作者:chen 欄目:大數據

本篇內容主要講解“dubbo中AwaitingNonWebApplicationListener的實例介紹”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“dubbo中AwaitingNonWebApplicationListener的實例介紹”吧!

本文主要研究一下dubbo的AwaitingNonWebApplicationListener

AwaitingNonWebApplicationListener

public class AwaitingNonWebApplicationListener implements SmartApplicationListener {

    private static final String[] WEB_APPLICATION_CONTEXT_CLASSES = new String[]{
            "org.springframework.web.context.WebApplicationContext",
            "org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext"
    };

    private static final Logger logger = LoggerFactory.getLogger(AwaitingNonWebApplicationListener.class);

    private static final Class<? extends ApplicationEvent>[] SUPPORTED_APPLICATION_EVENTS =
            of(ApplicationReadyEvent.class, ContextClosedEvent.class);

    private static final AtomicBoolean awaited = new AtomicBoolean(false);

    private static final Lock lock = new ReentrantLock();

    private static final Condition condition = lock.newCondition();

    private static final ExecutorService executorService = newSingleThreadExecutor();

    private static <T> T[] of(T... values) {
        return values;
    }

    static AtomicBoolean getAwaited() {
        return awaited;
    }

    @Override
    public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
        return containsElement(SUPPORTED_APPLICATION_EVENTS, eventType);
    }

    @Override
    public boolean supportsSourceType(Class<?> sourceType) {
        return true;
    }

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationReadyEvent) {
            onApplicationReadyEvent((ApplicationReadyEvent) event);
        } else if (event instanceof ContextClosedEvent) {
            onContextClosedEvent((ContextClosedEvent) event);
        }
    }

    @Override
    public int getOrder() {
        return LOWEST_PRECEDENCE;
    }

    protected void onApplicationReadyEvent(ApplicationReadyEvent event) {

        final ConfigurableApplicationContext applicationContext = event.getApplicationContext();

        if (!isRootApplicationContext(applicationContext) || isWebApplication(applicationContext)) {
            return;
        }

        await();
    }

    private boolean isRootApplicationContext(ApplicationContext applicationContext) {
        return applicationContext.getParent() == null;
    }

    private boolean isWebApplication(ApplicationContext applicationContext) {
        boolean webApplication = false;
        for (String contextClass : WEB_APPLICATION_CONTEXT_CLASSES) {
            if (isAssignable(contextClass, applicationContext.getClass(), applicationContext.getClassLoader())) {
                webApplication = true;
                break;
            }
        }
        return webApplication;
    }

    private static boolean isAssignable(String target, Class<?> type, ClassLoader classLoader) {
        try {
            return ClassUtils.resolveClassName(target, classLoader).isAssignableFrom(type);
        } catch (Throwable ex) {
            return false;
        }
    }

    protected void onContextClosedEvent(ContextClosedEvent event) {
        release();
        shutdown();
    }

    protected void await() {

        // has been waited, return immediately
        if (awaited.get()) {
            return;
        }

        if (!executorService.isShutdown()) {
            executorService.execute(() -> executeMutually(() -> {
                while (!awaited.get()) {
                    if (logger.isInfoEnabled()) {
                        logger.info(" [Dubbo] Current Spring Boot Application is await...");
                    }
                    try {
                        condition.await();
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                }
            }));
        }
    }

    protected void release() {
        executeMutually(() -> {
            while (awaited.compareAndSet(false, true)) {
                if (logger.isInfoEnabled()) {
                    logger.info(" [Dubbo] Current Spring Boot Application is about to shutdown...");
                }
                condition.signalAll();
            }
        });
    }

    private void shutdown() {
        if (!executorService.isShutdown()) {
            // Shutdown executorService
            executorService.shutdown();
        }
    }

    private void executeMutually(Runnable runnable) {
        try {
            lock.lock();
            runnable.run();
        } finally {
            lock.unlock();
        }
    }
}
  • AwaitingNonWebApplicationListener實現了SmartApplicationListener接口,其supportsEventType支持ApplicationReadyEvent.class, ContextClosedEvent.class

  • onApplicationEvent判斷是ApplicationReadyEvent時執行onApplicationReadyEvent方法;判斷是ContextClosedEvent時,執行onContextClosedEvent方法

  • onApplicationReadyEvent對于rootApplicationContext或者是nonWebApplicationContext執行await方法,該方法會注冊一個異步線程去執行condition.await();onContextClosedEvent則執行release及shutdown方法,release方法會更新awaited為true,然后執行condition.signalAll(),shutdown方法則關閉executorService

AwaitingNonWebApplicationListenerTest

dubbo-spring-boot-project-2.7.3/dubbo-spring-boot-compatible/autoconfigure/src/test/java/org/apache/dubbo/spring/boot/context/event/AwaitingNonWebApplicationListenerTest.java

public class AwaitingNonWebApplicationListenerTest {

    @Test
    public void init() {
        AtomicBoolean awaited = AwaitingNonWebApplicationListener.getAwaited();
        awaited.set(false);

    }

    @Test
    public void testSingleContextNonWebApplication() {
        new SpringApplicationBuilder(Object.class)
                .web(false)
                .run().close();
        AtomicBoolean awaited = AwaitingNonWebApplicationListener.getAwaited();
        Assert.assertTrue(awaited.get());
    }

    @Test
    public void testMultipleContextNonWebApplication() {
        new SpringApplicationBuilder(Object.class)
                .parent(Object.class)
                .web(false)
                .run().close();
        AtomicBoolean awaited = AwaitingNonWebApplicationListener.getAwaited();
        Assert.assertTrue(awaited.get());
    }

}
  • AwaitingNonWebApplicationListenerTest驗證了SingleContextNonWebApplication及MultipleContextNonWebApplication

小結

  • AwaitingNonWebApplicationListener實現了SmartApplicationListener接口,其supportsEventType支持ApplicationReadyEvent.class, ContextClosedEvent.class

  • onApplicationEvent判斷是ApplicationReadyEvent時執行onApplicationReadyEvent方法;判斷是ContextClosedEvent時,執行onContextClosedEvent方法

  • onApplicationReadyEvent對于rootApplicationContext或者是nonWebApplicationContext執行await方法,該方法會注冊一個異步線程去執行condition.await();onContextClosedEvent則執行release及shutdown方法,release方法會更新awaited為true,然后執行condition.signalAll(),shutdown方法則關閉executorService

到此,相信大家對“dubbo中AwaitingNonWebApplicationListener的實例介紹”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

察隅县| 东港市| 临安市| 尼木县| 黑水县| 防城港市| 井陉县| 东乡族自治县| 孝义市| 洛川县| 泰顺县| 桂平市| 内丘县| 新野县| 洛扎县| 秀山| 津南区| 洛宁县| 贵定县| 托里县| 友谊县| 宿松县| 静安区| 青田县| 逊克县| 苏州市| 新绛县| 乐亭县| 洛浦县| 江门市| 沂源县| 正蓝旗| 正定县| 大新县| 项城市| 新巴尔虎左旗| 利津县| 黔东| 旺苍县| 双鸭山市| 德格县|