io.vertx.core.Context.put()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(5.9k)|赞(0)|评价(0)|浏览(141)

本文整理了Java中io.vertx.core.Context.put()方法的一些代码示例,展示了Context.put()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Context.put()方法的具体详情如下:
包路径:io.vertx.core.Context
类名称:Context
方法名:put

Context.put介绍

[英]Put some data in the context.

This can be used to share data between different handlers that share a context
[中]在上下文中放置一些数据。
这可用于在共享上下文的不同处理程序之间共享数据

代码示例

代码示例来源:origin: apache/servicecomb-java-chassis

protected void addPool(Context context, CLIENT_POOL pool) {
 context.put(id, pool);
 pools.add(pool);
}

代码示例来源:origin: eclipse-vertx/vert.x

@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();
}

代码示例来源:origin: resteasy/Resteasy

@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();
 }
}

代码示例来源:origin: io.vertx/vertx-rx-java

/**
 * 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);
}

代码示例来源:origin: vert-x3/vertx-rx

/**
 * 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);
}

代码示例来源:origin: org.apache.servicecomb/foundation-vertx

protected void addPool(Context context, CLIENT_POOL pool) {
 context.put(id, pool);
 pools.add(pool);
}

代码示例来源:origin: io.vertx/vertx-lang-groovy

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));
 }
}

代码示例来源:origin: io.vertx/vertx-core

@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();
}

代码示例来源:origin: jponge/vertx-in-action

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"));
  });
 }
}

代码示例来源:origin: org.jboss.resteasy/resteasy-vertx

@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();
 }
}

代码示例来源:origin: vert-x3/vertx-sync

/**
 * 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;
}

代码示例来源:origin: io.vertx/vertx-auth-common

/**
 * 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;
}

代码示例来源:origin: vert-x3/vertx-auth

/**
 * 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;
}

相关文章