public interface Operation
An operation encapsulates a logical piece of work, which will complete some time in the future.
It is similar to a Promise
except that it does not produce a value.
It merely succeeds, or throws an exception.
The then(Block)
method allows specifying what should happen after the operation completes.
The onError(Action)
method allows specifying what should happen if the operation fails.
Like Promise
, the operation will not start until it is subscribed to, via then(Block)
or then()
.
It is common for methods that would naturally return void
to return an Operation
instead,
to allow the method implementation to be effectively asynchronous.
The caller of the method is then expected to use the then(Block)
method to specify what should happen after the operation
that the method represents finishes.
import com.google.common.collect.Lists;
import ratpack.test.exec.ExecHarness;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class Example {
public static void main(String... args) throws Exception {
List<String> events = Lists.newArrayList();
ExecHarness.runSingle(e ->
e.operation(() ->
e.blocking(() -> events.add("1"))
.then(b -> events.add("2"))
)
.then(() -> events.add("3"))
);
assertEquals(Arrays.asList("1", "2", "3"), events);
}
}
Modifier and Type | Method and Description |
---|---|
default <T> Promise<T> |
flatMap(Factory<? extends Promise<T>> factory) |
default <T> Promise<T> |
flatMap(Promise<T> promise) |
default <T> Promise<T> |
map(Factory<? extends T> factory) |
default Operation |
next(Block operation) |
default Operation |
next(Operation operation) |
static Operation |
noop() |
static Operation |
of(Block block) |
Operation |
onError(Action<? super Throwable> onError) |
Promise<Void> |
promise() |
default void |
then() |
void |
then(Block block) |
@NonBlocking void then(Block block)
@NonBlocking default void then()
static Operation noop()