2
2
3
3
import java .util .List ;
4
4
import java .util .Optional ;
5
+ import java .util .concurrent .Executor ;
5
6
6
7
import jakarta .enterprise .context .ApplicationScoped ;
7
8
import jakarta .inject .Inject ;
11
12
import org .eclipse .microprofile .reactive .messaging .Message ;
12
13
13
14
import io .smallrye .mutiny .Multi ;
15
+ import io .smallrye .mutiny .helpers .ParameterValidation ;
16
+ import io .smallrye .mutiny .operators .multi .AbstractMultiOperator ;
17
+ import io .smallrye .mutiny .operators .multi .MultiOperatorProcessor ;
18
+ import io .smallrye .mutiny .subscription .MultiSubscriber ;
14
19
import io .smallrye .reactive .messaging .PublisherDecorator ;
15
20
import io .smallrye .reactive .messaging .SubscriberDecorator ;
16
21
@@ -32,7 +37,7 @@ public ConnectorContextPropagationDecorator(
32
37
public Multi <? extends Message <?>> decorate (Multi <? extends Message <?>> publisher , List <String > channelName ,
33
38
boolean isConnector ) {
34
39
if (isConnector ) {
35
- return publisher . emitOn ( tc . currentContextExecutor () );
40
+ return new ContextPropagationOperator <>( publisher , tc );
36
41
}
37
42
return publisher ;
38
43
}
@@ -42,4 +47,42 @@ public int getPriority() {
42
47
// Before the io.smallrye.reactive.messaging.providers.locals.ContextDecorator which has the priority 0
43
48
return -100 ;
44
49
}
50
+
51
+ public static class ContextPropagationOperator <T > extends AbstractMultiOperator <T , T > {
52
+
53
+ private final ThreadContext tc ;
54
+
55
+ /**
56
+ * Creates a new {@link AbstractMultiOperator} with the passed {@link Multi} as upstream.
57
+ *
58
+ * @param upstream the upstream, must not be {@code null}
59
+ */
60
+ public ContextPropagationOperator (Multi <? extends T > upstream , ThreadContext tc ) {
61
+ super (upstream );
62
+ this .tc = tc ;
63
+ }
64
+
65
+ @ Override
66
+ public void subscribe (MultiSubscriber <? super T > downstream ) {
67
+ ParameterValidation .nonNullNpe (downstream , "subscriber" );
68
+ upstream .subscribe ().withSubscriber (new ContextPropagationProcessor <>(downstream , tc ));
69
+ }
70
+
71
+ static final class ContextPropagationProcessor <T > extends MultiOperatorProcessor <T , T > {
72
+
73
+ private final Executor tcExecutor ;
74
+
75
+ public ContextPropagationProcessor (MultiSubscriber <? super T > downstream , ThreadContext tc ) {
76
+ super (downstream );
77
+ this .tcExecutor = tc .currentContextExecutor ();
78
+ }
79
+
80
+ @ Override
81
+ public void onItem (T item ) {
82
+ // Even though the executor is called, this is a synchronous call
83
+ tcExecutor .execute (() -> super .onItem (item ));
84
+ }
85
+
86
+ }
87
+ }
45
88
}
0 commit comments