Skip to content

Commit

Permalink
Fix identification of parallel startup of CDI (#4964) (#4993)
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Langer <[email protected]>
  • Loading branch information
tomas-langer authored Sep 29, 2022
1 parent 45fe655 commit 27df335
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 @@ -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;
Expand Down Expand Up @@ -113,15 +112,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 @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit 27df335

Please sign in to comment.