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 68fbdc771df..23f4cfd0652 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 @@ -45,7 +45,6 @@ import io.helidon.common.context.Contexts; import io.helidon.common.http.Http; import io.helidon.config.Config; -import io.helidon.microprofile.cdi.BuildTimeStart; import io.helidon.microprofile.cdi.RuntimeStart; import io.helidon.webserver.KeyPerformanceIndicatorSupport; import io.helidon.webserver.Routing; @@ -113,15 +112,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; @@ -171,6 +161,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) { @@ -335,7 +331,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 @@ -344,7 +340,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;