From 4293f3797365134f84280834167399d37e4c6e5a Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Thu, 3 Nov 2022 17:29:06 +0100 Subject: [PATCH] VertxCurrentContextFactory - use a string constant as a key --- .../vertx/core/runtime/VertxCoreRecorder.java | 18 +++++------------- .../runtime/VertxCurrentContextFactory.java | 8 +++++--- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/core/runtime/VertxCoreRecorder.java b/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/core/runtime/VertxCoreRecorder.java index f2347ec69aa02..5ac0b5769c9d4 100644 --- a/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/core/runtime/VertxCoreRecorder.java +++ b/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/core/runtime/VertxCoreRecorder.java @@ -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; @@ -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 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(); diff --git a/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/runtime/VertxCurrentContextFactory.java b/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/runtime/VertxCurrentContextFactory.java index a2806ebc62899..88db266deac81 100644 --- a/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/runtime/VertxCurrentContextFactory.java +++ b/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/runtime/VertxCurrentContextFactory.java @@ -14,6 +14,8 @@ public class VertxCurrentContextFactory implements CurrentContextFactory { + public static final String LOCAL_KEY = "io.quarkus.vertx.cdi-current-context"; + @Override public CurrentContext create(Class scope) { return new VertxCurrentContext<>(); @@ -27,7 +29,7 @@ private static final class VertxCurrentContext 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(); } @@ -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); } @@ -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(); }