From e79ef08c3632d0b757d546740ba8ab635bfb7d5e Mon Sep 17 00:00:00 2001 From: Tomas Langer Date: Wed, 28 Sep 2022 21:36:11 +0200 Subject: [PATCH] Fix identification of parallel startup of CDI (#4964) * Added a custom InitializationFailed CDI event to notify extensions that build time initialization has failed. * Fix order of imports. * Refactor identification of parallel CDI startup. --- .../cdi/HelidonContainerInitializer.java | 5 ++++- .../server/ServerCdiExtension.java | 20 ++++++++----------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/microprofile/cdi/src/main/java/io/helidon/microprofile/cdi/HelidonContainerInitializer.java b/microprofile/cdi/src/main/java/io/helidon/microprofile/cdi/HelidonContainerInitializer.java index a17a3c2cbab..adc44a2918e 100644 --- a/microprofile/cdi/src/main/java/io/helidon/microprofile/cdi/HelidonContainerInitializer.java +++ b/microprofile/cdi/src/main/java/io/helidon/microprofile/cdi/HelidonContainerInitializer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, 2020 Oracle and/or its affiliates. + * Copyright (c) 2019, 2022 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -147,6 +147,9 @@ public SeContainerInitializer setClassLoader(ClassLoader classLoader) { @Override public SeContainer initialize() { + if (HelidonContainerImpl.isRuntime()) { + throw new IllegalStateException("Helidon CDI is already started, cannot create two instances in the same JVM"); + } container.initInContext(); return container.start(); } diff --git a/microprofile/server/src/main/java/io/helidon/microprofile/server/ServerCdiExtension.java b/microprofile/server/src/main/java/io/helidon/microprofile/server/ServerCdiExtension.java index 5094963ae57..47107d6dc51 100644 --- a/microprofile/server/src/main/java/io/helidon/microprofile/server/ServerCdiExtension.java +++ b/microprofile/server/src/main/java/io/helidon/microprofile/server/ServerCdiExtension.java @@ -43,7 +43,6 @@ import io.helidon.common.http.Http; import io.helidon.config.Config; import io.helidon.config.mp.Prioritized; -import io.helidon.microprofile.cdi.BuildTimeStart; import io.helidon.microprofile.cdi.RuntimeStart; import io.helidon.reactive.webserver.KeyPerformanceIndicatorSupport; import io.helidon.reactive.webserver.Routing; @@ -111,15 +110,6 @@ public class ServerCdiExtension implements Extension { private final Set routingsWithKPIMetrics = new HashSet<>(); - private void buildTime(@Observes @BuildTimeStart Object event) { - // update the status of server, as we may have been started without a builder being used - // such as when cdi.Main or SeContainerInitializer are used - if (!IN_PROGRESS_OR_RUNNING.compareAndSet(false, true)) { - throw new IllegalStateException("There is another builder in progress, or another Server running. " - + "You cannot run more than one in parallel"); - } - } - private void prepareRuntime(@Observes @RuntimeStart Config config) { serverBuilder.config(config.get("server")); this.config = config; @@ -169,6 +159,12 @@ private void registerKpiMetricsDeferrableRequestContextSetterHandler(JaxRsCdiExt private void startServer(@Observes @Priority(PLATFORM_AFTER + 100) @Initialized(ApplicationScoped.class) Object event, BeanManager beanManager) { + // update the status of server, as we may have been started without a builder being used + // such as when cdi.Main or SeContainerInitializer are used + if (!IN_PROGRESS_OR_RUNNING.compareAndSet(false, true)) { + throw new IllegalStateException("There is another builder in progress, or another Server running. " + + "You cannot run more than one in parallel"); + } // make sure all configuration is in place if (null == jaxRsExecutorService) { @@ -325,7 +321,7 @@ private void registerClasspathStaticContent(Config config) { private void stopServer(@Observes @Priority(PLATFORM_BEFORE) @BeforeDestroyed(ApplicationScoped.class) Object event) { try { if (started) { - doStop(event); + doStop(); } } finally { // as there only can be a single CDI in a single JVM, once this CDI is shutting down, we @@ -334,7 +330,7 @@ private void stopServer(@Observes @Priority(PLATFORM_BEFORE) @BeforeDestroyed(Ap } } - private void doStop(Object event) { + private void doStop() { if (null == webserver || !started) { // nothing to do return;