protected void addPool(Context context, CLIENT_POOL pool) { context.put(id, pool); pools.add(pool); }
@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(); }
@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(); } }
/** * Put some data in the context. * <p> * This can be used to share data between different handlers that share a context * @param key the key of the data * @param value the data */ public void put(String key, Object value) { delegate.put(key, value); }
/** * Put some data in the context. * <p> * This can be used to share data between different handlers that share a context * @param key the key of the data * @param value the data */ public void put(String key, Object value) { delegate.put(key, value); }
protected void addPool(Context context, CLIENT_POOL pool) { context.put(id, pool); pools.add(pool); }
public static void put(io.vertx.core.Context j_receiver, java.lang.String key, java.lang.Object value) { j_receiver.put(key, io.vertx.core.impl.ConversionHelper.toObject(value)); } }
@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(); }
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 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; }
/** * 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; }