@Test public void testPutGetRemoveData() throws Exception { SomeObject obj = new SomeObject(); vertx.runOnContext(v -> { Context ctx = Vertx.currentContext(); ctx.put("foo", obj); ctx.runOnContext(v2 -> { assertEquals(obj, ctx.get("foo")); assertTrue(ctx.remove("foo")); ctx.runOnContext(v3 -> { assertNull(ctx.get("foo")); testComplete(); }); }); }); await(); }
protected CLIENT_POOL findByContext(Context targetContext) { Context currentContext = targetContext != null ? targetContext : Vertx.currentContext(); if (currentContext != null && currentContext.owner() == vertx && currentContext.isEventLoopContext()) { // standard reactive mode CLIENT_POOL clientPool = currentContext.get(id); if (clientPool != null) { return clientPool; } // this will make "client.thread-count" bigger than which in microservice.yaml // maybe it's better to remove "client.thread-count", just use "rest/highway.thread-count" return createClientPool(currentContext); } // not in correct context: // 1.normal thread // 2.vertx worker thread // 3.other vertx thread // select a existing context int idx = reactiveNextIndex.getAndIncrement() % pools.size(); if (idx < 0) { idx = -idx; } return pools.get(idx); }
@Override public CompletionStage<Object> createResource(HttpRequest request, HttpResponse response, ResteasyProviderFactory factory) { Context ctx = Vertx.currentContext(); if (ctx != null) { Object resource = ctx.get(id); if (resource == null) { return delegate.createResource(request, response, factory).thenApply(newResource -> { ctx.put(id, newResource); return newResource; }); } return CompletableFuture.completedFuture(resource); } else { throw new IllegalStateException(); } }
/** * Get some data from the context. * @param key the key of the data * @return the data */ public <T> T get(String key) { T ret = (T) delegate.get(key); return ret; }
/** * Get some data from the context. * @param key the key of the data * @return the data */ public <T> T get(String key) { T ret = (T) delegate.get(key); return ret; }
@Test public void testPutGetRemoveData() throws Exception { SomeObject obj = new SomeObject(); vertx.runOnContext(v -> { Context ctx = Vertx.currentContext(); ctx.put("foo", obj); ctx.runOnContext(v2 -> { assertEquals(obj, ctx.get("foo")); assertTrue(ctx.remove("foo")); ctx.runOnContext(v3 -> { assertNull(ctx.get("foo")); testComplete(); }); }); }); await(); }
public static <T>java.lang.Object get(io.vertx.core.Context j_receiver, java.lang.String key) { return io.vertx.core.impl.ConversionHelper.fromObject(j_receiver.get(key)); } public static void put(io.vertx.core.Context j_receiver, java.lang.String key, java.lang.Object value) {
private static void dataAndExceptions() { Vertx vertx = Vertx.vertx(); Context ctx = vertx.getOrCreateContext(); ctx.put("foo", "bar"); ctx.exceptionHandler(t -> { if ("Tada".equals(t.getMessage())) { logger.info("Got a _Tada_ exception"); } else { logger.error("Woops", t); } }); ctx.runOnContext(v -> { throw new RuntimeException("Tada"); }); ctx.runOnContext(v -> { logger.info("foo = {}", (String) ctx.get("foo")); }); } }
@Override public Object createResource(HttpRequest request, HttpResponse response, ResteasyProviderFactory factory) { Context ctx = Vertx.factory.context(); if (ctx != null) { Object resource = ctx.get(id); if (resource == null) { resource = delegate.createResource(request, response, factory); ctx.put(id, resource); } return resource; } else { throw new IllegalStateException(); } }
/** * Get or create a secure non blocking random number generator using the provided vert.x context. This method will not * throw an exception. * * @param context a Vert.x context. * @return A secure non blocking random number generator. */ @GenIgnore static VertxContextPRNG current(final Context context) { final String contextKey = "__vertx.VertxContextPRNG"; // attempt to load a PRNG from the current context PRNG random = context.get(contextKey); if (random == null) { synchronized (context) { // attempt to reload to avoid double creation when we were // waiting for the lock random = context.get(contextKey); if (random == null) { // there was no PRNG in the context, create one random = new PRNG(context.owner()); // need to make the random final final PRNG rand = random; // save to the context context.put(contextKey, rand); } } } return random; }
/** * Get or create a secure non blocking random number generator using the provided vert.x context. This method will not * throw an exception. * * @param context a Vert.x context. * @return A secure non blocking random number generator. */ @GenIgnore static VertxContextPRNG current(final Context context) { final String contextKey = "__vertx.VertxContextPRNG"; // attempt to load a PRNG from the current context PRNG random = context.get(contextKey); if (random == null) { synchronized (context) { // attempt to reload to avoid double creation when we were // waiting for the lock random = context.get(contextKey); if (random == null) { // there was no PRNG in the context, create one random = new PRNG(context.owner()); // need to make the random final final PRNG rand = random; // save to the context context.put(contextKey, rand); } } } return random; }
protected CLIENT_POOL findByContext(Context targetContext) { Context currentContext = targetContext != null ? targetContext : Vertx.currentContext(); if (currentContext != null && currentContext.owner() == vertx && currentContext.isEventLoopContext()) { // standard reactive mode CLIENT_POOL clientPool = currentContext.get(id); if (clientPool != null) { return clientPool; } // this will make "client.thread-count" bigger than which in microservice.yaml // maybe it's better to remove "client.thread-count", just use "rest/highway.thread-count" return createClientPool(currentContext); } // not in correct context: // 1.normal thread // 2.vertx worker thread // 3.other vertx thread // select a existing context int idx = reactiveNextIndex.getAndIncrement() % pools.size(); if (idx < 0) { idx = -idx; } return pools.get(idx); }
/** * 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; }