Unable to switch back to the vertx event loop thread after switching to executor thread #1933
-
@GET
@Path("/p1")
public Uni<Response> p1() {
Log.info("Entry (event loop)");
return Uni.createFrom()
.item("input")
.onItem().transform(input -> {
Log.info("Light transform");
return input.toUpperCase();
})
.emitOn(Infrastructure.getDefaultWorkerPool()) // ← Switch to worker
.onItem().transform(data -> {
Log.info("Heavy work (worker)");
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
}
return "Processed: " + data;
})
.emitOn(Infrastructure.getDefaultExecutor()) // doesn't work
.onItem().transform(result -> {
Log.info("Back on event loop");
return Response.ok(result).build();
});
}2025-08-11 04:13:58,404 INFO [com.exa.ExampleResource] (vert.x-eventloop-thread-1) Entry (event loop) I am unable to switch back the to the event loop thread by using emitOn(Infrastructure.getDefaultExecutor()). |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
You can do it like that @GET
@Path("/eventloop")
public Uni<RestResponse<String>> testEventLoop(){
final var scheduler = ContextAwareScheduler.delegatingTo(Infrastructure.getDefaultWorkerPool()).withCurrentContext();
return
Uni
.createFrom()
.voidItem()
.invoke(() -> log.info("on the event loop"))
.emitOn(Infrastructure.getDefaultWorkerPool())
.map(_ -> {
log.info("on the executor thread");
//doing some blocking stuff
// ....
return "end of heavy work";
})
.invoke(() -> log.info("Still on executor"))
.emitOn(scheduler)
.invoke(() -> log.info("back on the event loop"))
.map(_ -> RestResponse.ok("eventloop example"))
;
} |
Beta Was this translation helpful? Give feedback.
-
|
@jponge Please could you tell me the difference between these others ways to do. Im really confused between them : @GET
@Path("/eventloop2")
public Uni<RestResponse<String>> testEventLoop2(){
var c = VertxContext.getOrCreateDuplicatedContext(VertxContext.getRootContext(Vertx.currentContext()));
return
Uni
.createFrom()
.voidItem()
.invoke(() -> log.info("on the event loop"))
.emitOn(Infrastructure.getDefaultWorkerPool())
.map(_ -> {
log.info("on the executor thread");
//doing some blocking stuff
// ....
return "end of heavy work";
})
.invoke(() -> log.info("Still on executor"))
.emitOn(io.smallrye.mutiny.vertx.MutinyHelper.executor(c))
.invoke(() -> log.info("back on the event loop"))
.map(_ -> RestResponse.ok("eventloop example"))
;
}
@GET
@Path("/eventloop3")
public Uni<RestResponse<String>> testEventLoop3(){
var c = VertxContext.getOrCreateDuplicatedContext(Vertx.currentContext());
return
Uni
.createFrom()
.voidItem()
.invoke(() -> log.info("on the event loop"))
.emitOn(Infrastructure.getDefaultWorkerPool())
.map(_ -> {
log.info("on the executor thread");
//doing some blocking stuff
// ....
return "end of heavy work";
})
.invoke(() -> log.info("Still on executor"))
.emitOn(io.smallrye.mutiny.vertx.MutinyHelper.executor(c))
.invoke(() -> log.info("back on the event loop"))
.map(_ -> RestResponse.ok("eventloop example"))
;
}What's the difference between :
These 3 ways to do allow to get back on the event loop. But what's the difference, I mean, internally on the vertx engine? Which approach is the best ? |
Beta Was this translation helpful? Give feedback.
You can do it like that