Skip to content

Commit

Permalink
Provide access to context locals.
Browse files Browse the repository at this point in the history
Motivation:

Expose the list of registered context locals of the vertx instance, so it gives knowledge for interacting with context locals.
  • Loading branch information
vietj committed Jan 15, 2025
1 parent 3030490 commit 8cd8606
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/io/vertx/core/impl/VertxImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ private static ThreadFactory virtualThreadFactory() {
private final Map<ServerID, HttpServerImpl> sharedHttpServers = new HashMap<>();
private final Map<ServerID, NetServerImpl> sharedNetServers = new HashMap<>();
private final ContextLocal<?>[] contextLocals;
private final List<ContextLocal<?>> contextLocalsList;
final WorkerPool workerPool;
final WorkerPool internalWorkerPool;
final WorkerPool virtualThreaWorkerPool;
Expand Down Expand Up @@ -194,6 +195,7 @@ private static ThreadFactory virtualThreadFactory() {
ThreadFactory virtualThreadFactory = virtualThreadFactory();

contextLocals = LocalSeq.get();
contextLocalsList = Collections.unmodifiableList(Arrays.asList(contextLocals));
closeFuture = new CloseFuture(log);
maxEventLoopExecTime = maxEventLoopExecuteTime;
maxEventLoopExecTimeUnit = maxEventLoopExecuteTimeUnit;
Expand Down Expand Up @@ -994,6 +996,11 @@ public AddressResolverGroup<InetSocketAddress> nettyAddressResolverGroup() {
return addressResolver.nettyAddressResolverGroup();
}

@Override
public List<ContextLocal<?>> contextLocals() {
return contextLocalsList;
}

@Override
public FileResolver fileResolver() {
return fileResolver;
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/io/vertx/core/impl/VertxInternal.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.vertx.core.net.impl.NetServerImpl;
import io.vertx.core.net.impl.ServerID;
import io.vertx.core.net.impl.TCPServerBase;
import io.vertx.core.spi.context.storage.ContextLocal;
import io.vertx.core.spi.transport.Transport;
import io.vertx.core.spi.cluster.ClusterManager;
import io.vertx.core.spi.file.FileResolver;
Expand All @@ -33,6 +34,7 @@
import java.io.File;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
Expand Down Expand Up @@ -240,6 +242,11 @@ default <T> Future<T> executeBlockingInternal(Callable<T> blockingCodeHandler, b
*/
AddressResolverGroup<InetSocketAddress> nettyAddressResolverGroup();

/**
* @return an immutable list of this vertx instance context locals
*/
List<ContextLocal<?>> contextLocals();

BlockedThreadChecker blockedThreadChecker();

CloseFuture closeFuture();
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/io/vertx/core/impl/VertxWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.vertx.core.net.impl.NetServerImpl;
import io.vertx.core.net.impl.ServerID;
import io.vertx.core.net.impl.TCPServerBase;
import io.vertx.core.spi.context.storage.ContextLocal;
import io.vertx.core.spi.transport.Transport;
import io.vertx.core.shareddata.SharedData;
import io.vertx.core.spi.VerticleFactory;
Expand All @@ -42,6 +43,7 @@
import java.io.File;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -506,6 +508,11 @@ public AddressResolverGroup<InetSocketAddress> nettyAddressResolverGroup() {
return delegate.nettyAddressResolverGroup();
}

@Override
public List<ContextLocal<?>> contextLocals() {
return delegate.contextLocals();
}

@Override
public BlockedThreadChecker blockedThreadChecker() {
return delegate.blockedThreadChecker();
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/io/vertx/core/ContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1253,4 +1253,12 @@ public void testNestedDuplicate() {
assertEquals("bar", duplicate.getLocal("foo"));
assertEquals(expected, duplicate.getLocal(contextLocal));
}

@Test
public void testContextLocals() {
List<ContextLocal<?>> locals = ((VertxInternal) vertx).contextLocals();
assertSame(ContextInternal.LOCAL_MAP, locals.get(0));
assertSame(contextLocal, locals.get(1));
assertSame(locals, ((VertxInternal) vertx).contextLocals());
}
}

0 comments on commit 8cd8606

Please sign in to comment.