diff --git a/bom/application/pom.xml b/bom/application/pom.xml index 2136dbb13a775b..aa101d2dbea816 100644 --- a/bom/application/pom.xml +++ b/bom/application/pom.xml @@ -1372,7 +1372,7 @@ io.quarkus - quarkus-smallrye-context-propagation-deployment-spi + quarkus-smallrye-context-propagation-spi ${project.version} diff --git a/extensions/arc/deployment/pom.xml b/extensions/arc/deployment/pom.xml index 5eb18e81e26a50..4ac95cec06da47 100644 --- a/extensions/arc/deployment/pom.xml +++ b/extensions/arc/deployment/pom.xml @@ -17,6 +17,10 @@ io.quarkus quarkus-core-deployment + + io.quarkus + quarkus-smallrye-context-propagation-spi + io.quarkus quarkus-vertx-http-dev-console-spi @@ -47,6 +51,11 @@ 1.0.2.Final test + + io.quarkus + quarkus-smallrye-context-propagation-deployment + test + diff --git a/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/ArcConfig.java b/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/ArcConfig.java index 35159793b75f7b..2eeaa0e2041d25 100644 --- a/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/ArcConfig.java +++ b/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/ArcConfig.java @@ -203,6 +203,12 @@ public class ArcConfig { @ConfigItem public Optional> ignoredSplitPackages; + /** + * Context propagation configuration. + */ + @ConfigItem + public ArcContextPropagationConfig contextPropagation; + public final boolean isRemoveUnusedBeansFieldValid() { return ALLOWED_REMOVE_UNUSED_BEANS_VALUES.contains(removeUnusedBeans.toLowerCase()); } diff --git a/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/ArcContextPropagationConfig.java b/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/ArcContextPropagationConfig.java new file mode 100644 index 00000000000000..dee455eefed429 --- /dev/null +++ b/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/ArcContextPropagationConfig.java @@ -0,0 +1,16 @@ +package io.quarkus.arc.deployment; + +import io.quarkus.runtime.annotations.ConfigGroup; +import io.quarkus.runtime.annotations.ConfigItem; + +@ConfigGroup +public class ArcContextPropagationConfig { + + /** + * If set to true and SmallRye Context Propagation extension is present then enable the context propagation for CDI + * contexts. + */ + @ConfigItem(defaultValue = "true") + public boolean enabled; + +} diff --git a/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/ArcProcessor.java b/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/ArcProcessor.java index f07140fff2de6a..abd6e9b6590862 100644 --- a/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/ArcProcessor.java +++ b/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/ArcProcessor.java @@ -81,6 +81,7 @@ import io.quarkus.arc.runtime.BeanContainer; import io.quarkus.arc.runtime.LaunchModeProducer; import io.quarkus.arc.runtime.LoggerProducer; +import io.quarkus.arc.runtime.context.ArcContextProvider; import io.quarkus.arc.runtime.test.PreloadedTestApplicationClassPredicate; import io.quarkus.bootstrap.BootstrapDebug; import io.quarkus.deployment.Capabilities; @@ -114,6 +115,7 @@ import io.quarkus.runtime.annotations.QuarkusMain; import io.quarkus.runtime.test.TestApplicationClassPredicate; import io.quarkus.runtime.util.HashUtil; +import io.quarkus.smallrye.context.deployment.spi.ThreadContextProviderBuildItem; /** * This class contains build steps that trigger various phases of the bean processing. @@ -813,6 +815,13 @@ void validateAsyncObserverExceptionHandlers(ValidationPhaseBuildItem validationP } } + @BuildStep + void registerContextPropagation(ArcConfig config, BuildProducer threadContextProvider) { + if (config.contextPropagation.enabled) { + threadContextProvider.produce(new ThreadContextProviderBuildItem(ArcContextProvider.class)); + } + } + private void registerListInjectionPointsBeans(BeanRegistrationPhaseBuildItem beanRegistrationPhase, List injectionPoints, BuildProducer reflectiveMethods, BuildProducer reflectiveFields, diff --git a/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/cp/ContextProviderDisabledTest.java b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/cp/ContextProviderDisabledTest.java new file mode 100644 index 00000000000000..9de7e983247f87 --- /dev/null +++ b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/cp/ContextProviderDisabledTest.java @@ -0,0 +1,74 @@ +package io.quarkus.arc.test.cp; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.fail; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import javax.annotation.PostConstruct; +import javax.enterprise.context.ContextNotActiveException; +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; + +import org.eclipse.microprofile.context.ManagedExecutor; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.arc.Arc; +import io.quarkus.arc.ManagedContext; +import io.quarkus.test.QuarkusUnitTest; + +public class ContextProviderDisabledTest { + + @RegisterExtension + static final QuarkusUnitTest config = new QuarkusUnitTest().overrideConfigKey("quarkus.arc.context-propagation.enabled", + "false"); + + @Inject + ManagedExecutor all; + + @Inject + MyRequestBean bean; + + @Test + public void testPropagationDisabled() throws InterruptedException, ExecutionException, TimeoutException { + ManagedContext requestContext = Arc.container().requestContext(); + + requestContext.activate(); + assertEquals("FOO", bean.getId()); + try { + assertEquals("OK", + all.completedFuture("OK").thenApplyAsync(text -> { + assertFalse(requestContext.isActive()); + try { + bean.getId(); + fail(); + } catch (ContextNotActiveException expected) { + } + return text; + }).toCompletableFuture().get(5, TimeUnit.SECONDS)); + } finally { + requestContext.terminate(); + } + } + + @RequestScoped + public static class MyRequestBean { + + String id; + + @PostConstruct + void init() { + id = "FOO"; + } + + public String getId() { + return id; + } + + } + +} diff --git a/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/cp/ContextProviderEnabledTest.java b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/cp/ContextProviderEnabledTest.java new file mode 100644 index 00000000000000..896330771e576d --- /dev/null +++ b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/cp/ContextProviderEnabledTest.java @@ -0,0 +1,69 @@ +package io.quarkus.arc.test.cp; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import javax.annotation.PostConstruct; +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; + +import org.eclipse.microprofile.context.ManagedExecutor; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.arc.Arc; +import io.quarkus.arc.ManagedContext; +import io.quarkus.test.QuarkusUnitTest; + +public class ContextProviderEnabledTest { + + @RegisterExtension + static final QuarkusUnitTest config = new QuarkusUnitTest().overrideConfigKey("quarkus.arc.context-propagation.enabled", + "true"); + + @Inject + ManagedExecutor all; + + @Inject + MyRequestBean bean; + + @Test + public void testPropagationDisabled() throws InterruptedException, ExecutionException, TimeoutException { + ManagedContext requestContext = Arc.container().requestContext(); + + requestContext.activate(); + assertEquals("FOO", bean.getId()); + try { + assertEquals("OK", + all.completedFuture("OK").thenApplyAsync(text -> { + assertTrue(requestContext.isActive()); + assertEquals("FOO", bean.getId()); + return text; + }).toCompletableFuture().get(5, TimeUnit.SECONDS)); + ; + } finally { + requestContext.terminate(); + } + } + + @RequestScoped + public static class MyRequestBean { + + String id; + + @PostConstruct + void init() { + id = "FOO"; + } + + public String getId() { + return id; + } + + } + +} diff --git a/extensions/arc/runtime/src/main/resources/META-INF/services/org.eclipse.microprofile.context.spi.ThreadContextProvider b/extensions/arc/runtime/src/main/resources/META-INF/services/org.eclipse.microprofile.context.spi.ThreadContextProvider deleted file mode 100644 index 45ab8e34c62507..00000000000000 --- a/extensions/arc/runtime/src/main/resources/META-INF/services/org.eclipse.microprofile.context.spi.ThreadContextProvider +++ /dev/null @@ -1 +0,0 @@ -io.quarkus.arc.runtime.context.ArcContextProvider \ No newline at end of file diff --git a/extensions/smallrye-context-propagation/deployment/pom.xml b/extensions/smallrye-context-propagation/deployment/pom.xml index 35d93b31deeb51..9b9c5e71d32c06 100644 --- a/extensions/smallrye-context-propagation/deployment/pom.xml +++ b/extensions/smallrye-context-propagation/deployment/pom.xml @@ -22,7 +22,7 @@ io.quarkus - quarkus-smallrye-context-propagation-deployment-spi + quarkus-smallrye-context-propagation-spi io.quarkus diff --git a/extensions/smallrye-context-propagation/spi/pom.xml b/extensions/smallrye-context-propagation/spi/pom.xml index 9fd7fe09c096dd..3f2672224e5f19 100644 --- a/extensions/smallrye-context-propagation/spi/pom.xml +++ b/extensions/smallrye-context-propagation/spi/pom.xml @@ -10,8 +10,8 @@ 4.0.0 - quarkus-smallrye-context-propagation-deployment-spi - Quarkus - SmallRye Context Propagation - Deployment - SPI + quarkus-smallrye-context-propagation-spi + Quarkus - SmallRye Context Propagation - SPI