Skip to content

Commit

Permalink
ArC - make it possible to disable context propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
mkouba committed Sep 5, 2022
1 parent a287432 commit bf375bf
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,7 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-context-propagation-deployment-spi</artifactId>
<artifactId>quarkus-smallrye-context-propagation-spi</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
Expand Down
9 changes: 9 additions & 0 deletions extensions/arc/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-core-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-context-propagation-spi</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-http-dev-console-spi</artifactId>
Expand Down Expand Up @@ -47,6 +51,11 @@
<version>1.0.2.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-context-propagation-deployment</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ public class ArcConfig {
@ConfigItem
public Optional<List<String>> ignoredSplitPackages;

/**
* Context propagation configuration.
*/
@ConfigItem
public ArcContextPropagationConfig contextPropagation;

public final boolean isRemoveUnusedBeansFieldValid() {
return ALLOWED_REMOVE_UNUSED_BEANS_VALUES.contains(removeUnusedBeans.toLowerCase());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -813,6 +815,13 @@ void validateAsyncObserverExceptionHandlers(ValidationPhaseBuildItem validationP
}
}

@BuildStep
void registerContextPropagation(ArcConfig config, BuildProducer<ThreadContextProviderBuildItem> threadContextProvider) {
if (config.contextPropagation.enabled) {
threadContextProvider.produce(new ThreadContextProviderBuildItem(ArcContextProvider.class));
}
}

private void registerListInjectionPointsBeans(BeanRegistrationPhaseBuildItem beanRegistrationPhase,
List<InjectionPointInfo> injectionPoints, BuildProducer<ReflectiveMethodBuildItem> reflectiveMethods,
BuildProducer<ReflectiveFieldBuildItem> reflectiveFields,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}

}

This file was deleted.

2 changes: 1 addition & 1 deletion extensions/smallrye-context-propagation/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-context-propagation-deployment-spi</artifactId>
<artifactId>quarkus-smallrye-context-propagation-spi</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
Expand Down
4 changes: 2 additions & 2 deletions extensions/smallrye-context-propagation/spi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-smallrye-context-propagation-deployment-spi</artifactId>
<name>Quarkus - SmallRye Context Propagation - Deployment - SPI</name>
<artifactId>quarkus-smallrye-context-propagation-spi</artifactId>
<name>Quarkus - SmallRye Context Propagation - SPI</name>

<dependencies>
<dependency>
Expand Down

0 comments on commit bf375bf

Please sign in to comment.