-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27685 from mkouba/cp-thread-context-provider-buil…
…d-item SmallRye CP - introduce ThreadContextProviderBuildItem
- Loading branch information
Showing
13 changed files
with
246 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...s/arc/deployment/src/main/java/io/quarkus/arc/deployment/ArcContextPropagationConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
...in/resources/META-INF/services/org.eclipse.microprofile.context.spi.ThreadContextProvider
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...est/java/io/quarkus/smallrye/context/deployment/test/cdi/ContextProviderDisabledTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package io.quarkus.smallrye.context.deployment.test.cdi; | ||
|
||
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 -> { | ||
// Assertion error would result in an ExecutionException thrown from the CompletableFuture.get() | ||
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; | ||
} | ||
|
||
} | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
...test/java/io/quarkus/smallrye/context/deployment/test/cdi/ContextProviderEnabledTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package io.quarkus.smallrye.context.deployment.test.cdi; | ||
|
||
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 testPropagationEnabled() throws InterruptedException, ExecutionException, TimeoutException { | ||
ManagedContext requestContext = Arc.container().requestContext(); | ||
|
||
requestContext.activate(); | ||
assertEquals("FOO", bean.getId()); | ||
try { | ||
assertEquals("OK", | ||
all.completedFuture("OK").thenApplyAsync(text -> { | ||
// Assertion error would result in an ExecutionException thrown from the CompletableFuture.get() | ||
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 contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,5 +17,6 @@ | |
<modules> | ||
<module>deployment</module> | ||
<module>runtime</module> | ||
<module>spi</module> | ||
</modules> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>quarkus-smallrye-context-propagation-parent</artifactId> | ||
<groupId>io.quarkus</groupId> | ||
<version>999-SNAPSHOT</version> | ||
<relativePath>../</relativePath> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>quarkus-smallrye-context-propagation-spi</artifactId> | ||
<name>Quarkus - SmallRye Context Propagation - SPI</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.eclipse.microprofile.context-propagation</groupId> | ||
<artifactId>microprofile-context-propagation-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-core-deployment</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
22 changes: 22 additions & 0 deletions
22
.../main/java/io/quarkus/smallrye/context/deployment/spi/ThreadContextProviderBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.quarkus.smallrye.context.deployment.spi; | ||
|
||
import org.eclipse.microprofile.context.spi.ThreadContextProvider; | ||
|
||
import io.quarkus.builder.item.MultiBuildItem; | ||
|
||
/** | ||
* This build item can be used to register a {@link ThreadContextProvider}. | ||
*/ | ||
public final class ThreadContextProviderBuildItem extends MultiBuildItem { | ||
|
||
private final Class<? extends ThreadContextProvider> provider; | ||
|
||
public ThreadContextProviderBuildItem(Class<? extends ThreadContextProvider> provider) { | ||
this.provider = provider; | ||
} | ||
|
||
public Class<? extends ThreadContextProvider> getProvider() { | ||
return provider; | ||
} | ||
|
||
} |