Skip to content

Commit

Permalink
Fix identification of parallel startup of CDI (#4964)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
tomas-langer authored Sep 28, 2022
1 parent dcb464b commit e79ef08
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -111,15 +110,6 @@ public class ServerCdiExtension implements Extension {

private final Set<Routing.Builder> 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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit e79ef08

Please sign in to comment.