
io.vertx.groovy.core.Context.groovy Maven / Gradle / Ivy
/*
* Copyright 2014 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.vertx.groovy.core;
import groovy.transform.CompileStatic
import io.vertx.lang.groovy.InternalHelper
import io.vertx.core.json.JsonObject
import java.util.List
import io.vertx.core.json.JsonObject
import io.vertx.core.AsyncResult
import io.vertx.core.Handler
/**
* The execution context of a {@link io.vertx.groovy.core.Handler} execution.
*
* When Vert.x provides an event to a handler or calls the start or stop methods of a {@link io.vertx.groovy.core.Verticle},
* the execution is associated with a Context
.
*
* Usually a context is an *event-loop context* and is tied to a specific event loop thread. So executions for that
* context always occur on that exact same event loop thread.
*
* In the case of worker verticles and running inline blocking code a worker context will be associated with the execution
* which will use a thread from the worker thread pool.
*
* When a handler is set by a thread associated with a specific context, the Vert.x will guarantee that when that handler
* is executed, that execution will be associated with the same context.
*
* If a handler is set by a thread not associated with a context (i.e. a non Vert.x thread). Then a new context will
* be created for that handler.
*
* In other words, a context is propagated.
*
* This means that when a verticle is deployed, any handlers it sets will be associated with the same context - the context
* of the verticle.
*
* This means (in the case of a standard verticle) that the verticle code will always be executed with the exact same
* thread, so you don't have to worry about multi-threaded acccess to the verticle state and you can code your application
* as single threaded.
*
* This class also allows arbitrary data to be {@link io.vertx.groovy.core.Context#put} and {@link io.vertx.groovy.core.Context#get} on the context so it can be shared easily
* amongst different handlers of, for example, a verticle instance.
*
* This class also provides {@link io.vertx.groovy.core.Context#runOnContext} which allows an action to be executed asynchronously using the same context.
*/
@CompileStatic
public class Context {
private final def io.vertx.core.Context delegate;
public Context(Object delegate) {
this.delegate = (io.vertx.core.Context) delegate;
}
public Object getDelegate() {
return delegate;
}
/**
* Is the current thread a worker thread?
*
* NOTE! This is not always the same as calling {@link io.vertx.groovy.core.Context#isWorkerContext}. If you are running blocking code
* from an event loop context, then this will return true but {@link io.vertx.groovy.core.Context#isWorkerContext} will return false.
* @return true if current thread is a worker thread, false otherwise
*/
public static boolean isOnWorkerThread() {
def ret = io.vertx.core.Context.isOnWorkerThread();
return ret;
}
/**
* Is the current thread an event thread?
*
* NOTE! This is not always the same as calling {@link io.vertx.groovy.core.Context#isEventLoopContext}. If you are running blocking code
* from an event loop context, then this will return false but {@link io.vertx.groovy.core.Context#isEventLoopContext} will return true.
* @return true if current thread is a worker thread, false otherwise
*/
public static boolean isOnEventLoopThread() {
def ret = io.vertx.core.Context.isOnEventLoopThread();
return ret;
}
/**
* Is the current thread a Vert.x thread? That's either a worker thread or an event loop thread
* @return true if current thread is a Vert.x thread, false otherwise
*/
public static boolean isOnVertxThread() {
def ret = io.vertx.core.Context.isOnVertxThread();
return ret;
}
/**
* Run the specified action asynchronously on the same context, some time after the current execution has completed.
* @param action the action to run
*/
public void runOnContext(Handler action) {
this.delegate.runOnContext(action);
}
/**
* Safely execute some blocking code.
*
* Executes the blocking code in the handler blockingCodeHandler
using a thread from the worker pool.
*
* When the code is complete the handler resultHandler
will be called with the result on the original context
* (e.g. on the original event loop of the caller).
*
* A Future
instance is passed into blockingCodeHandler
. When the blocking code successfully completes,
* the handler should call the {@link io.vertx.groovy.core.Future#complete} or {@link io.vertx.groovy.core.Future#complete} method, or the {@link io.vertx.groovy.core.Future#fail}
* method if it failed.
* @param blockingCodeHandler handler representing the blocking code to run
* @param ordered if true then if executeBlocking is called several times on the same context, the executions for that context will be executed serially, not in parallel. if false then they will be no ordering guarantees
* @param resultHandler handler that will be called when the blocking code is complete
*/
public void executeBlocking(Handler> blockingCodeHandler, boolean ordered, Handler> resultHandler) {
this.delegate.executeBlocking(new Handler>() {
public void handle(io.vertx.core.Future event) {
blockingCodeHandler.handle(new io.vertx.groovy.core.Future(event));
}
}, ordered, new Handler>() {
public void handle(AsyncResult