Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Licensed 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
   * 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.
This utility class represents the entry point to the library by acting as a factory of routine builders.

There are mainly two ways to create a routine object:

Routine by invocation customization
The first approach consists in implementing an invocation object. Invocations mimic the scope of a function call. Objects are instantiated when needed and recycled for successive invocations.

Routine by method invocation
The second approach is based on the asynchronous invocation of a method of an existing class or object via reflection.
It is possible to annotate selected methods to be asynchronously invoked, or to simply select a method through its signature. It is also possible to build a proxy object whose methods will in turn asynchronously invoke the target object ones.
Note that a proxy object can be simply defined as an interface implemented by the target, but also as a completely unrelated one mirroring the target methods. In this way it is possible to apply the library functionality to objects defined by third party libraries which are not under direct control.
A mirror interface adds the possibility to override input and output parameters with output channels, so that data are transferred asynchronously, avoiding the need to block execution while waiting for them to be available.
Finally, it also possible to create a wrapper class to enable asynchronous invocation of methods, through annotation pre-processing and compile-time code generation. In order to activate the processing of annotations, it is simply necessary to include the proxy artifact or module in the project dependencies.

This class provides also a way to build transport channel instances, which can be used to pass data without the need to start a routine invocation.

Some usage examples

Example 1: Asynchronously merge the output of two routines.


         final TransportChannel<Result> channel = JRoutine.transport().buildChannel();
Or simply:

         final OutputChannel<Result> output1 = doSomething1.asyncCall();
         final OutputChannel<Result> output2 = doSomething2.asyncCall();
(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.



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(myCallback)
         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 TransportChannel<Result> channel = JRoutine.transport().buildChannel();

         new Thread() {

             public void run() {

                 channel.pass(new Result()).close();


         final Routine<Result, Result> routine =

Created by davide-maestroni on 9/7/14.

public class JRoutine {

Avoid direct instantiation.
    protected JRoutine() {

Returns a routine builder wrapping the specified target class.

target the target class.
the routine builder instance.
java.lang.IllegalArgumentException if the specified class represents an interface.
    public static ClassRoutineBuilder on(@Nonnull final Class<?> target) {
        return new DefaultClassRoutineBuilder(target);

Returns a routine builder based on the specified invocation factory.
In order to prevent undesired leaks, the class of the specified factory must be static.

factory the invocation factory.
<INPUT> the input data type.
<OUTPUT> the output data type.
the routine builder instance.
java.lang.IllegalArgumentException if the class of the specified factory is not static.
    public static <INPUT, OUTPUT> RoutineBuilder<INPUT, OUTPUT> on(
            @Nonnull final InvocationFactory<INPUT, OUTPUT> factory) {
        return new DefaultRoutineBuilder<INPUT, OUTPUT>(factory);

Returns a routine builder wrapping a weak reference to 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.

target the target object.
the routine builder instance.
    public static ObjectRoutineBuilder on(@Nonnull final Object target) {
        return new DefaultObjectRoutineBuilder(target);

Returns a transport channel builder.

the transport channel builder instance.
    public static TransportChannelBuilder transport() {
        return new DefaultTransportChannelBuilder();
New to GrepCode? Check out our FAQ X