-
Notifications
You must be signed in to change notification settings - Fork 63
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
specify optional integration with MicroProfile Context Propagation #565
Closed
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0d6d252
specify optional integration with MicroProfile Context Propagation
Ladicek be6adc2
apply suggestions from code review
Ladicek 5d86138
add TCK test for MicroProfile Context Propagation integration
Ladicek 7a5f97d
add a note about request context being active in presence of Context …
Ladicek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
29 changes: 0 additions & 29 deletions
29
spec/src/main/asciidoc/optional-container-integration.asciidoc
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// Copyright (c) 2020 Contributors to the Eclipse Foundation | ||
// | ||
// See the NOTICE file(s) distributed with this work for additional | ||
// information regarding copyright ownership. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// You may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// Contributors: | ||
// Ladislav Thon | ||
|
||
[[optional-integrations]] | ||
|
||
== Recommendations for Optional Integrations | ||
|
||
=== Jakarta EE | ||
|
||
This section describes the expected behaviors when the implementation runs in a Jakarta EE container. | ||
|
||
==== `@Asynchronous` | ||
|
||
Threads that are servicing `@Asynchronous` invocations should, for the duration of the invocation, have the correct security context and naming context associated. | ||
|
||
=== MicroProfile Context Propagation | ||
|
||
This section describes the expected behaviors when the implementation runs in an environment that provides implementation of MicroProfile Context Propagation. | ||
These behaviors are currently optional because the MicroProfile Context Propagation specification is not part of the MicroProfile Platform. | ||
|
||
==== `@Asynchronous` | ||
|
||
Implementations should use MicroProfile Context Propagation to propagate contexts to threads that are servicing `@Asynchronous` invocations. | ||
Deciding which contexts are propagated and which are cleared is left to the MicroProfile Context Propagation implementation. | ||
|
||
[NOTE] | ||
==== | ||
As of version 1.0.2, MicroProfile Context Propagation doesn't define which contexts are propagated and which are cleared by default, this is left to implementations. | ||
|
||
However, there are configuration properties that you can use to override the default setting. | ||
==== | ||
|
||
If the environment that provides MicroProfile Context Propagation is itself a Jakarta EE container, the _Jakarta EE_ section above doesn't apply and context propagation is left entirely to MicroProfile Context Propagation. |
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
114 changes: 114 additions & 0 deletions
114
.../eclipse/microprofile/fault/tolerance/tck/context/propagation/ContextPropagationTest.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,114 @@ | ||
/* | ||
******************************************************************************* | ||
* Copyright (c) 2020 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*******************************************************************************/ | ||
package org.eclipse.microprofile.fault.tolerance.tck.context.propagation; | ||
|
||
import org.eclipse.microprofile.context.spi.ThreadContextProvider; | ||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.testng.Arquillian; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.EmptyAsset; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
import org.testng.SkipException; | ||
import org.testng.annotations.Test; | ||
|
||
import javax.inject.Inject; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import static org.eclipse.microprofile.fault.tolerance.tck.asynchronous.CompletableFutureHelper.toCompletableFuture; | ||
import static org.testng.Assert.assertEquals; | ||
|
||
public class ContextPropagationTest extends Arquillian { | ||
// configure identical defaults for both ManagedExecutor and ThreadContext, because implementations | ||
// are free to use any of them to implement context propagation | ||
private static final String CONTEXT_PROPAGATION_CONFIG = "" | ||
+ "mp.context.ManagedExecutor.propagated=CDI,MyContext1\n" | ||
+ "mp.context.ManagedExecutor.cleared=MyContext2\n" | ||
+ "mp.context.ThreadContext.propagated=CDI,MyContext1\n" | ||
+ "mp.context.ThreadContext.cleared=MyContext2\n"; | ||
|
||
@Deployment | ||
public static WebArchive deploy() { | ||
JavaArchive jar = ShrinkWrap.create(JavaArchive.class, "ftContextPropagation.jar") | ||
.addClasses(MyAppScopedBean.class, MyReqScopedBean.class) | ||
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); | ||
|
||
|
||
if (contextPropagationPresent()) { | ||
// if this was always added, deployment would fail at runtime | ||
// when MP Context Propagation classes are not present | ||
|
||
jar.addClasses(MyContext1.class, MyContext1Provider.class, MyContext2.class, MyContext2Provider.class) | ||
.addAsServiceProvider(ThreadContextProvider.class, MyContext1Provider.class, MyContext2Provider.class) | ||
.addAsManifestResource(new StringAsset(CONTEXT_PROPAGATION_CONFIG), "microprofile-config.properties"); | ||
|
||
} | ||
|
||
return ShrinkWrap.create(WebArchive.class, "ftContextPropagation.war") | ||
.addAsLibrary(jar); | ||
} | ||
|
||
@Inject | ||
private MyAppScopedBean appScopedBean; | ||
|
||
@Inject | ||
private MyReqScopedBean reqScopedBean; | ||
|
||
@Test | ||
public void contextPropagation() throws InterruptedException, ExecutionException, TimeoutException { | ||
assumeContextPropagationPresence(); | ||
|
||
MyContext1.set("foo"); | ||
MyContext2.set("bar"); | ||
reqScopedBean.set("quux"); | ||
|
||
assertEquals(MyContext1.get(), "foo"); | ||
assertEquals(MyContext2.get(), "bar"); | ||
assertEquals(reqScopedBean.get(), "quux"); | ||
|
||
// in the @Asynchronous method, CDI should be propagated, MyContext1 should be propagated, | ||
// and MyContext2 should be cleared | ||
String value = toCompletableFuture(appScopedBean.get()).get(1, TimeUnit.MINUTES); | ||
assertEquals(value, "foo|[[NONE]]|quux"); | ||
|
||
assertEquals(MyContext1.get(), "foo"); | ||
assertEquals(MyContext2.get(), "bar"); | ||
assertEquals(reqScopedBean.get(), "quux"); | ||
} | ||
|
||
private static void assumeContextPropagationPresence() { | ||
if (!contextPropagationPresent()) { | ||
throw new SkipException("Context Propagation not present, ignoring test"); | ||
} | ||
} | ||
|
||
private static boolean contextPropagationPresent() { | ||
try { | ||
Class.forName("org.eclipse.microprofile.context.ThreadContext"); | ||
return true; | ||
} | ||
catch (ClassNotFoundException e) { | ||
return false; | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...ava/org/eclipse/microprofile/fault/tolerance/tck/context/propagation/MyAppScopedBean.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,39 @@ | ||
/* | ||
******************************************************************************* | ||
* Copyright (c) 2020 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*******************************************************************************/ | ||
package org.eclipse.microprofile.fault.tolerance.tck.context.propagation; | ||
|
||
import org.eclipse.microprofile.faulttolerance.Asynchronous; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
import static java.util.concurrent.CompletableFuture.completedFuture; | ||
|
||
@ApplicationScoped | ||
public class MyAppScopedBean { | ||
@Inject | ||
private MyReqScopedBean reqScopedBean; | ||
|
||
@Asynchronous | ||
public CompletionStage<String> get() { | ||
return completedFuture(MyContext1.get() + "|" + MyContext2.get() + "|" + reqScopedBean.get()); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...ain/java/org/eclipse/microprofile/fault/tolerance/tck/context/propagation/MyContext1.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,38 @@ | ||
/* | ||
******************************************************************************* | ||
* Copyright (c) 2020 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*******************************************************************************/ | ||
package org.eclipse.microprofile.fault.tolerance.tck.context.propagation; | ||
|
||
public class MyContext1 { | ||
private static final ThreadLocal<String> CONTEXTUAL_VALUE = ThreadLocal.withInitial(() -> "<<none>>"); | ||
|
||
private MyContext1() {} // avoid instantiation | ||
|
||
public static String get() { | ||
return CONTEXTUAL_VALUE.get(); | ||
} | ||
|
||
public static void set(String value) { | ||
CONTEXTUAL_VALUE.set(value); | ||
} | ||
|
||
public static void remove() { | ||
CONTEXTUAL_VALUE.remove(); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
.../org/eclipse/microprofile/fault/tolerance/tck/context/propagation/MyContext1Provider.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,55 @@ | ||
/* | ||
******************************************************************************* | ||
* Copyright (c) 2020 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*******************************************************************************/ | ||
package org.eclipse.microprofile.fault.tolerance.tck.context.propagation; | ||
|
||
import org.eclipse.microprofile.context.spi.ThreadContextProvider; | ||
import org.eclipse.microprofile.context.spi.ThreadContextSnapshot; | ||
|
||
import java.util.Map; | ||
|
||
public class MyContext1Provider implements ThreadContextProvider { | ||
@Override | ||
public ThreadContextSnapshot currentContext(Map<String, String> props) { | ||
String captured = MyContext1.get(); | ||
return () -> { | ||
String movedOut = MyContext1.get(); | ||
MyContext1.set(captured); | ||
return () -> { | ||
MyContext1.set(movedOut); | ||
}; | ||
}; | ||
} | ||
|
||
@Override | ||
public ThreadContextSnapshot clearedContext(Map<String, String> props) { | ||
return () -> { | ||
String movedOut = MyContext1.get(); | ||
MyContext1.remove(); | ||
return () -> { | ||
MyContext1.set(movedOut); | ||
}; | ||
}; | ||
} | ||
|
||
@Override | ||
public String getThreadContextType() { | ||
return MyContext1.class.getSimpleName(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...ain/java/org/eclipse/microprofile/fault/tolerance/tck/context/propagation/MyContext2.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,38 @@ | ||
/* | ||
******************************************************************************* | ||
* Copyright (c) 2020 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*******************************************************************************/ | ||
package org.eclipse.microprofile.fault.tolerance.tck.context.propagation; | ||
|
||
public class MyContext2 { | ||
private static final ThreadLocal<String> CONTEXTUAL_VALUE = ThreadLocal.withInitial(() -> "[[NONE]]"); | ||
|
||
private MyContext2() {} // avoid instantiation | ||
|
||
public static String get() { | ||
return CONTEXTUAL_VALUE.get(); | ||
} | ||
|
||
public static void set(String value) { | ||
CONTEXTUAL_VALUE.set(value); | ||
} | ||
|
||
public static void remove() { | ||
CONTEXTUAL_VALUE.remove(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will say:
These behaviours are only required if a runtime provides the implementation for both MicroProfile Fault Tolerance and MicroProfile Context Propagation Specification.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @rhusar, this is what we talked about over email. Wanna share your perspective?