eclipse-vertx / vert.x

Vert.x is a tool-kit for building reactive applications on the JVM
http://vertx.io
Other
14.26k stars 2.07k forks source link

Shadow context #5259

Closed vietj closed 1 month ago

vietj commented 2 months ago

A shadow context is a vertx context that references a context belonging to a different vertx instance.

Such context enables the usage of a vertx instance (e.g. a client) from another vertx instance and ensure that callbacks are on the correct thread.

One use case is the usage of a Vert.x client from a Vert.x server with different instances. This provides event-loop segregation between vertx instance ensuring that the server event-loop only process server IO and are disjoint from the client io operations.

Shadow context happens when a context is obtained from a vertx thread of another vertx instance:

Context ctx = vertx1.getOrCreateContext();
ctx.runOnContext(v1 -> {
  Thread expected = Thread.currentThread();
  Context shadowCtx = vertx2.getOrCreateContext();
  shadowCtx.runOnContext(v2 -> {
    assertSame(expect, Thread.currentThread());
    assertSame(ctx, vertx1.getOrCreateContext());
    assertSame(shadowCtx, vertx2.getOrCreateContext());
  });
});

Noticeable changes

Possible extension