本文整理了Java中io.vertx.core.Context.get()
方法的一些代码示例,展示了Context.get()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Context.get()
方法的具体详情如下:
包路径:io.vertx.core.Context
类名称:Context
方法名:get
[英]Get some data from the context.
[中]从上下文中获取一些数据。
代码示例来源: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: apache/servicecomb-java-chassis
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);
}
代码示例来源: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
/**
* 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;
}
代码示例来源:origin: vert-x3/vertx-rx
/**
* 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;
}
代码示例来源: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: io.vertx/vertx-lang-groovy
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) {
代码示例来源: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: 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;
}
代码示例来源:origin: org.apache.servicecomb/foundation-vertx
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);
}
代码示例来源: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;
}
内容来源于网络,如有侵权,请联系作者删除!