Skip to content

Commit

Permalink
VertxCurrentContextFactory - use a string constant as a key
Browse files Browse the repository at this point in the history
  • Loading branch information
mkouba committed Nov 3, 2022
1 parent e8a5f01 commit 4293f37
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import io.quarkus.vertx.core.runtime.config.VertxConfiguration;
import io.quarkus.vertx.core.runtime.context.VertxContextSafetyToggle;
import io.quarkus.vertx.mdc.provider.LateBoundMDCProvider;
import io.quarkus.vertx.runtime.VertxCurrentContextFactory;
import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.Handler;
Expand Down Expand Up @@ -561,24 +562,15 @@ public void runWith(Runnable task, Object context) {
if (context != null && context != currentContext) {
// Only do context handling if it's non-null
ContextInternal vertxContext = (ContextInternal) context;
// The request scope must not be propagated
Object requestScope = null;
// The CDI request context must not be propagated
ConcurrentMap<Object, Object> local = vertxContext.localContextData();
for (Object k : local.keySet()) {
if (k.getClass().getName()
.equals("io.quarkus.vertx.runtime.VertxCurrentContextFactory$VertxCurrentContext")) {
requestScope = k;
break;
}
}
if (requestScope != null) {
// Duplicate the context, copy the data, remove the request scope
if (local.containsKey(VertxCurrentContextFactory.LOCAL_KEY)) {
// Duplicate the context, copy the data, remove the request context
vertxContext = vertxContext.duplicate();
vertxContext.localContextData().putAll(local);
vertxContext.localContextData().remove(requestScope);
vertxContext.localContextData().remove(VertxCurrentContextFactory.LOCAL_KEY);
VertxContextSafetyToggle.setContextSafe(vertxContext, true);
}

vertxContext.beginDispatch();
try {
task.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

public class VertxCurrentContextFactory implements CurrentContextFactory {

public static final String LOCAL_KEY = "io.quarkus.vertx.cdi-current-context";

@Override
public <T extends InjectableContext.ContextState> CurrentContext<T> create(Class<? extends Annotation> scope) {
return new VertxCurrentContext<>();
Expand All @@ -27,7 +29,7 @@ private static final class VertxCurrentContext<T extends ContextState> implement
public T get() {
Context context = Vertx.currentContext();
if (context != null && VertxContext.isDuplicatedContext(context)) {
return context.getLocal(this);
return context.getLocal(LOCAL_KEY);
}
return fallback.get();
}
Expand All @@ -37,7 +39,7 @@ public void set(T state) {
Context context = Vertx.currentContext();
if (context != null && VertxContext.isDuplicatedContext(context)) {
VertxContextSafetyToggle.setContextSafe(context, true);
context.putLocal(this, state);
context.putLocal(LOCAL_KEY, state);
} else {
fallback.set(state);
}
Expand All @@ -48,7 +50,7 @@ public void remove() {
Context context = Vertx.currentContext();
if (context != null && VertxContext.isDuplicatedContext(context)) {
// NOOP - the DC should not be shared.
// context.removeLocal(this);
// context.removeLocal(LOCAL_KEY);
} else {
fallback.remove();
}
Expand Down

0 comments on commit 4293f37

Please sign in to comment.