@@ -36,7 +36,7 @@ public void injectServices(ServiceRegistryImplementor serviceRegistry) {
36
36
37
37
@ Override
38
38
public <T > void put (Key <T > key , T instance ) {
39
- final io . vertx . core . Context context = Vertx . currentContext ();
39
+ final ContextInternal context = currentContext ();
40
40
if ( context != null ) {
41
41
if ( trace ) LOG .tracef ( "Putting key,value in context: [%1$s, %2$s]" , key , instance );
42
42
context .putLocal ( key , instance );
@@ -47,9 +47,13 @@ public <T> void put(Key<T> key, T instance) {
47
47
}
48
48
}
49
49
50
+ private static ContextInternal currentContext () {
51
+ return (ContextInternal ) Vertx .currentContext ();
52
+ }
53
+
50
54
@ Override
51
55
public <T > T get (Key <T > key ) {
52
- final io . vertx . core . Context context = Vertx . currentContext ();
56
+ final ContextInternal context = currentContext ();
53
57
if ( context != null ) {
54
58
T local = context .getLocal ( key );
55
59
if ( trace ) LOG .tracef ( "Getting value %2$s from context for key %1$s" , key , local );
@@ -63,7 +67,7 @@ public <T> T get(Key<T> key) {
63
67
64
68
@ Override
65
69
public void remove (Key <?> key ) {
66
- final io . vertx . core . Context context = Vertx . currentContext ();
70
+ final ContextInternal context = currentContext ();
67
71
if ( context != null ) {
68
72
boolean removed = context .removeLocal ( key );
69
73
if ( trace ) LOG .tracef ( "Key %s removed from context: %s" , key , removed );
@@ -75,14 +79,15 @@ public void remove(Key<?> key) {
75
79
76
80
@ Override
77
81
public void execute (Runnable runnable ) {
78
- final io .vertx .core .Context currentContext = Vertx . currentContext ();
82
+ final io .vertx .core .Context currentContext = currentContext ();
79
83
if ( currentContext == null ) {
80
84
if ( trace ) LOG .tracef ( "Not in a Vert.x context, checking the VertxInstance service" );
81
85
final io .vertx .core .Context newContext = vertxInstance .getVertx ().getOrCreateContext ();
82
86
// Ensure we don't run on the root context, which is globally scoped:
83
87
// that could lead to unintentionally share the same session with other streams.
84
88
ContextInternal newContextInternal = (ContextInternal ) newContext ;
85
89
final ContextInternal duplicate = newContextInternal .duplicate ();
90
+
86
91
if ( trace ) LOG .tracef ( "Using duplicated context from VertxInstance: %s" , duplicate );
87
92
duplicate .runOnContext ( x -> runnable .run () );
88
93
}
0 commit comments