Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideD committed Feb 3, 2025
1 parent 4b1857b commit 51372f1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import java.util.concurrent.Executor;

import io.vertx.core.spi.context.storage.ContextLocal;

/**
* Abstracts away from the Vert.x {@link io.vertx.core.Context}
* object, enabling alternative strategies for associating state
Expand Down Expand Up @@ -53,5 +55,5 @@ public interface Context extends Executor, Service {
*
* @param <T> the type of thing we're storing in the context
*/
interface Key<T> {}
interface Key<T> extends ContextLocal<T> {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ public void injectServices(ServiceRegistryImplementor serviceRegistry) {

@Override
public <T> void put(Key<T> key, T instance) {
final io.vertx.core.Context context = Vertx.currentContext();
final ContextInternal context = currentContext();
if ( context != null ) {
if ( trace ) LOG.tracef( "Putting key,value in context: [%1$s, %2$s]", key, instance );
context.putLocal( );
context.putLocal( key, instance );
}
else {
Expand All @@ -47,9 +48,13 @@ public <T> void put(Key<T> key, T instance) {
}
}

private static ContextInternal currentContext() {
return (ContextInternal) Vertx.currentContext();
}

@Override
public <T> T get(Key<T> key) {
final io.vertx.core.Context context = Vertx.currentContext();
final ContextInternal context = currentContext();
if ( context != null ) {
T local = context.getLocal( key );
if ( trace ) LOG.tracef( "Getting value %2$s from context for key %1$s", key, local );
Expand All @@ -63,7 +68,7 @@ public <T> T get(Key<T> key) {

@Override
public void remove(Key<?> key) {
final io.vertx.core.Context context = Vertx.currentContext();
final ContextInternal context = currentContext();
if ( context != null ) {
boolean removed = context.removeLocal( key );
if ( trace ) LOG.tracef( "Key %s removed from context: %s", key, removed );
Expand All @@ -75,7 +80,7 @@ public void remove(Key<?> key) {

@Override
public void execute(Runnable runnable) {
final io.vertx.core.Context currentContext = Vertx.currentContext();
final io.vertx.core.Context currentContext = currentContext();
if ( currentContext == null ) {
if ( trace ) LOG.tracef( "Not in a Vert.x context, checking the VertxInstance service" );
final io.vertx.core.Context newContext = vertxInstance.getVertx().getOrCreateContext();
Expand Down

0 comments on commit 51372f1

Please sign in to comment.