Back to Transmittable Thread Local

Vertx 4 integration of TTL

ttl-integrations/vertx3-ttl-integration/README-EN.md

2.14.52.0 KB
Original Source

Vertx 4 integration of TTL

1. assure TTL context transmit in callback

1.1 Decorate io.vertx.core.Handler

Use TtlVertxHandler to decorate Handler

1.2 Decorate io.vertx.core.Future

At present, TTL agent has decorated below Vertx callback components(io.vertx.core.Future) implementation:

  • io.vertx.core.Future
  • io.vertx.core.impl.future.FutureImpl
  • io.vertx.core.http.impl.HttpClientImpl
  • decoration implementation code is in VertxFutureTtlTransformlet.java

Sample code:

java
Vertx vertx = Vertx.vertx();

//build channel
ManagedChannel channel = VertxChannelBuilder
  .forAddress(vertx, "localhost", 8080)
  .usePlaintext()
  .build();

// set in parent thread
TransmittableThreadLocal<String> context = new TransmittableThreadLocal<>();
context.set("value-set-in-parent");

//init stub
io.grpc.stub.XXX stub = XXX.newVertxStub(channel);
HelloRequest request = HelloRequest.newBuilder().setName("Julien").build();

//init handler
Handler<AsyncResult<String>> handler = event -> {
  // read in callback, value is "value-set-in-parent"
  context.get();
  if (event.succeeded()) {
    //do something
  } else {
    // find exception
  }
};
// extra work, create decorated TtlVertxHandler object
TtlVertxHandler<AsyncResult<String>> ttlVertxHandler = TtlVertxHandler.get(handler);

//send request
stub.sayHello(request).onComplete(ttlVertxHandler);

2. assure TTL context transmit in eventbus

2. decoratejava.lang.Runnable

Use TtlRunnable to decorateRunnable

2.2 Decorateio.netty.util.concurrent.SingleThreadEventExecutor