From 70b67a8fc03e014badea1991f4341cb109d9c9a8 Mon Sep 17 00:00:00 2001 From: Nathan Rauh Date: Tue, 24 Sep 2019 09:46:24 -0500 Subject: [PATCH] Issue #166 generic contextualizer methods Signed-off-by: Nathan Rauh --- .../microprofile/context/ThreadContext.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/api/src/main/java/org/eclipse/microprofile/context/ThreadContext.java b/api/src/main/java/org/eclipse/microprofile/context/ThreadContext.java index e1c79de..60b48d4 100644 --- a/api/src/main/java/org/eclipse/microprofile/context/ThreadContext.java +++ b/api/src/main/java/org/eclipse/microprofile/context/ThreadContext.java @@ -479,6 +479,71 @@ public void foo() { */ Supplier contextualSupplier(Supplier supplier); + /** + *

Returns a proxy for one or more interfaces, such that methods run with + * context that is captured, per the configuration of this ThreadContext, + * from the thread that invokes onMethodsOf.

+ * + *

When interface methods are invoked on the resulting proxy, + * context is first established on the thread that will run the method, + * and then the method of the provided instance is invoked. + * Finally, the previous context is restored on the thread, and the result of the + * method is returned to the invoker.

+ * + *

For example, in the case of,

+ *
+     * publisher.subscribe(threadContext.onMethodsOf(subscriber))
+ * + *

the context of the thread that invokes onMethodsOf is made available to the + * subscriber's onSubscribe, onNext, onError, and + * onComplete methods whenever they are used.

+ * + * @param an interface that is implemented by the instance. + * @param type of supplied instance. + * @param instance instance to contextualize. + * @return contextualized proxy for interfaces that are implemented by the supplied instance. + * @throws IllegalArgumentException if an already-contextualized instance is supplied to this method, + * or if the supplied instance does not implement any interfaces that can be proxied, + * or if an instance of CompletionStage is supplied as the parameter. + * @throws UnsupportedOperationException if the instance implements java.io.Serializable. + * Serializing and deserializing captured thread context is not supported. + */ + T onMethodsOf(U instance); + + /** + *

Proxies a publisher interface such that it invokes subscriber methods with context that is captured, + * per the configuration of this ThreadContext, from the thread that invokes + * onSubscriberMethods.

+ * + *

When subscriber methods are invoked by the publisher instance, + * context is first established on the thread that will run the method, + * then the corresponding method is invoked on the actual subscriber instance. + * Finally, the previous context is restored on the thread.

+ * + *

For example, in the case of,

+ *
+     * threadContext.onSubscriberMethods(publisher);
+     * ...
+     * publisher.subscribe(subscriber);
+ * + *

context of the thread that invokes onSubscriberMethods is made available to the + * onSubscribe, onNext, onError, and onComplete + * methods of every subscriber for which publisher.subscribe(subscriber) is + * subsequently invoked on the proxy publisher instance.

+ * + * @param

a publisher. + * @param spec interface of publisher (java.util.concurrent.Flow.Publisher + * or org.reactivestreams.Publisher). + * @param publisher instance to proxy such that thread context is applied to subscribers that are added via the proxy. + * @return Publisher proxy. + * @throws IllegalArgumentException if the supplied instance is already a proxy that was created by onSubscriberMethods. + * or if the supplied instance does not implement java.util.concurrent.Flow.Publisher + * or org.reactivestreams.Publisher. + * @throws UnsupportedOperationException if the publisher instance implements java.io.Serializable. + * Serializing and deserializing captured thread context is not supported. + */ + Publisher onSubscriberMethods(P publisher); + /** *

Returns a new CompletableFuture that is completed by the completion of the * specified stage.