/** * Get the `FiberScheduler` for the current context. There should be only one instance per context. * @return the scheduler */ @Suspendable public static FiberScheduler getContextScheduler() { Context context = Vertx.currentContext(); if (context == null) { throw new IllegalStateException("Not in context"); } if (!context.isEventLoopContext()) { throw new IllegalStateException("Not on event loop"); } // We maintain one scheduler per context FiberScheduler scheduler = context.get(FIBER_SCHEDULER_CONTEXT_KEY); if (scheduler == null) { Thread eventLoop = Thread.currentThread(); scheduler = new FiberExecutorScheduler("vertx.contextScheduler", command -> { if (Thread.currentThread() != eventLoop) { context.runOnContext(v -> command.run()); } else { // Just run directly command.run(); } }); context.put(FIBER_SCHEDULER_CONTEXT_KEY, scheduler); } return scheduler; }