public class JRoutine extends Object
final IOChannel<Result, Result> channel = JRoutine.io().buildChannel();
channel.pass(doSomething1.asyncCall())
.pass(doSomething2.asyncCall())
.close();
.afterMax(seconds(20))
.allInto(results);
Or simply:
final OutputChannel<Result> output1 = doSomething1.asyncCall();
final OutputChannel<Result> output2 = doSomething2.asyncCall();
output1.afterMax(seconds(20)).allInto(results);
output2.afterMax(seconds(20)).allInto(results);
(Note that, the order of the input or the output of the routine is not guaranteed unless properly
configured)
Example 2: Asynchronously concatenate the output of two routines.
doSomething2.asyncCall(doSomething1.asyncCall())).afterMax(seconds(20)).allInto(results);
Example 3: Asynchronously get the output of two routines.
public interface AsyncCallback {
public void onResults(
@Input(Result.class) OutputChannel<Result> result1,
@Input(Result.class) OutputChannel<Result> result2);
}
final AsyncCallback callback = JRoutine.on(instance(myCallback))
.buildProxy(AsyncCallback.class);
callback.onResults(doSomething1.asyncCall(), doSomething2.asyncCall());
Where the object myCallback implements a method
public void onResults(Result result1, Result result2).
Example 4: Asynchronously feed a routine from a different thread.
final IOChannel<Result, Result> channel = JRoutine.io().buildChannel();
new Thread() {
@Override
public void run() {
channel.pass(new Result()).close();
}
}.start();
final Routine<Result, Result> routine =
JRoutine.<Result>on(PassingInvocation.<Result>factoryOf())
.buildRoutine();
routine.asyncCall(channel).afterMax(seconds(20)).allInto(results);
Created by davide-maestroni on 09/07/2014.| Modifier and Type | Method and Description |
|---|---|
static IOChannelBuilder |
io()
Returns an I/O channel builder.
|
static <IN,OUT> RoutineBuilder<IN,OUT> |
on(InvocationFactory<IN,OUT> factory)
Returns a routine builder based on the specified invocation factory.
In order to prevent undesired leaks, the class of the specified factory must have a static context. |
static ObjectRoutineBuilder |
on(InvocationTarget<?> target)
Returns a routine builder wrapping the specified target object.
Note that it is responsibility of the caller to retain a strong reference to the target instance to prevent it from being garbage collected. Note also that the invocation input data will be cached, and the results will be produced only after the invocation channel is closed, so be sure to avoid streaming inputs in order to prevent starvation or out of memory errors. |
@NotNull public static IOChannelBuilder io()
@NotNull public static ObjectRoutineBuilder on(@NotNull InvocationTarget<?> target)
target - the invocation target.IllegalArgumentException - if the specified object class represents an
interface.@NotNull public static <IN,OUT> RoutineBuilder<IN,OUT> on(@NotNull InvocationFactory<IN,OUT> factory)
IN - the input data type.OUT - the output data type.factory - the invocation factory.IllegalArgumentException - if the class of the specified factory is not
static.