Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Fix identification of parallel startup of CDI #4993

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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