Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
public class VertxContext implements Context, ServiceRegistryAwareService {

private static final Log LOG = LoggerFactory.make( Log.class, MethodHandles.lookup() );
private static final boolean trace = LOG.isTraceEnabled();

private VertxInstance vertxInstance;

Expand All @@ -37,11 +38,11 @@ public void injectServices(ServiceRegistryImplementor serviceRegistry) {
public <T> void put(Key<T> key, T instance) {
final io.vertx.core.Context context = Vertx.currentContext();
if ( context != null ) {
LOG.tracef( "Putting key,value in context: [%1$s, %2$s]", key, instance );
if ( trace ) LOG.tracef( "Putting key,value in context: [%1$s, %2$s]", key, instance );
context.putLocal( key, instance );
}
else {
LOG.tracef( "Context is null for key,value: [%1$s, %2$s]", key, instance );
if ( trace ) LOG.tracef( "Context is null for key,value: [%1$s, %2$s]", key, instance );
throw LOG.notVertxContextActive();
}
}
Expand All @@ -51,11 +52,11 @@ public <T> T get(Key<T> key) {
final io.vertx.core.Context context = Vertx.currentContext();
if ( context != null ) {
T local = context.getLocal( key );
LOG.tracef( "Getting value %2$s from context for key %1$s", key, local );
if ( trace ) LOG.tracef( "Getting value %2$s from context for key %1$s", key, local );
return local;
}
else {
LOG.tracef( "Context is null. Returning null for key %s", key );
if ( trace ) LOG.tracef( "Context is null. Returning null for key %s", key );
return null;
}
}
Expand All @@ -65,28 +66,28 @@ public void remove(Key<?> key) {
final io.vertx.core.Context context = Vertx.currentContext();
if ( context != null ) {
boolean removed = context.removeLocal( key );
LOG.tracef( "Key %s removed from context: %s", key, removed );
if ( trace ) LOG.tracef( "Key %s removed from context: %s", key, removed );
}
else {
LOG.tracef( "Context is null, nothing to remove for key %s", key );
if ( trace ) LOG.tracef( "Context is null, nothing to remove for key %s", key );
}
}

@Override
public void execute(Runnable runnable) {
final io.vertx.core.Context currentContext = Vertx.currentContext();
if ( currentContext == null ) {
LOG.tracef( "Not in a Vert.x context, checking the VertxInstance service" );
if ( trace ) LOG.tracef( "Not in a Vert.x context, checking the VertxInstance service" );
final io.vertx.core.Context newContext = vertxInstance.getVertx().getOrCreateContext();
// Ensure we don't run on the root context, which is globally scoped:
// that could lead to unintentionally share the same session with other streams.
ContextInternal newContextInternal = (ContextInternal) newContext;
final ContextInternal duplicate = newContextInternal.duplicate();
LOG.tracef( "Using duplicated context from VertxInstance: %s", duplicate );
if ( trace ) LOG.tracef( "Using duplicated context from VertxInstance: %s", duplicate );
duplicate.runOnContext( x -> runnable.run() );
}
else {
LOG.tracef( "Running in the current Vert.x context %s", currentContext );
if ( trace ) LOG.tracef( "Running in the current Vert.x context %s", currentContext );
runnable.run();
}
}
Expand Down