JBoss.orgCommunity Documentation

Chapter 39. Asynchronous Injection

39.1. org.jboss.resteasy.spi.ContextInjector Interface
39.2. Single<Foo> Example
39.3. Async Injector With Annotations Example

Pluggable Asynchronous Injection, also referred to as Asynch Injection, is a feature that allows users to create custom injectable asynchronous types. For example it is now possible to declare an injector for Single<Foo> and inject it into an endpoint as a class variable or as a method parameter using @Context Foo. The response will be made asynchronous automatically and the resource method will only be invoked once the Single<Foo> object is resolved to Foo. Resolution is done in a non-blocking manner.

Note. Asynch injection is only attempted at points where asynchronous injection is permitted, such as on resource creation and resource method invocation. It is not enabled at points where the API does not allow for suspending the request, for example on ResourceContext.getResource(Foo.class).

The org.jboss.resteasy.spi.ContextInjector interface must be implemented on any custom asynch injector object. The implementation class must be tagged with the @Provider annotation.

/**
 * @param <WrappedType> A class that wraps a data type or data object
 *                            (e.g. Single<Foo>)
 * @param <UnwrappedType> The data type or data object declared in the
 *                              WrappedType (e.g. Foo)
*/
public interface ContextInjector<WrappedType, UnwrappedType> {
/**
 * This interface allows users to create custom injectable asynchronous types.
 *
 * Asynch injection is only attempted at points where asynchronous injection is
 * permitted, such as on resource creation and resource method invocation. It
 * is not enabled at points where the API does not allow for suspending the
 * request
 *
 * @param rawType
 * @param genericType
 * @param annotations The annotation list is useful to parametrize the injection.
 * @return
 */
 public WrappedType resolve(
            Class<? extends WrappedType> rawType,
            Type genericType,
            Annotation[] annotations);
  }
        
package my.test;

public class Foo {
   private String value = "PRE-SET-VALUE";

   public void setValue(String s) {
      this.value = s;
   }

   public String getValue() {
      return this.value;
   }
}
        
package my.test.asyc.resources;

import io.reactivex.Single;
import javax.ws.rs.ext.Provider;
import org.jboss.resteasy.spi.ContextInjector;
import my.test.Foo;

@Provider
public class FooAsychInjectorProvider implements
            ContextInjector<Single<Foo>,Foo> {

   public Single<Foo> resolve(Class<? extends Single<Foo>> rawType,
            Type genericType,
            Annotation[] annotations)
   {
      Foo value = new Foo();
      return Single.just(value.setValue("made it"));
   }
}
        

A convenience interface to provide annotation parameter designators

@Retention(RUNTIME)
@Target({ FIELD, METHOD, PARAMETER })
public @interface AsyncInjectionPrimitiveInjectorSpecifier
{
   public enum Type {
      VALUE, NULL, NO_RESULT;
   }

   Type value() default Type.VALUE;
}
    

@Provider
public class AsyncInjectionFloatInjector implements
            ContextInjector<CompletionStage<Float>, Float>
{

   @Override
   public CompletionStage<Float> resolve(
      Class<? extends CompletionStage<Float>> rawType,
            Type genericType,
            Annotation[] annotations)
    {
       for (Annotation annotation : annotations)
       {
           if(annotation.annotationType() ==
              AsyncInjectionPrimitiveInjectorSpecifier.class) {
             AsyncInjectionPrimitiveInjectorSpecifier.Type value =
               ((AsyncInjectionPrimitiveInjectorSpecifier)annotation).value();
             switch(value) {
               case NO_RESULT:
                  return null;
               case NULL:
                  return CompletableFuture.completedFuture(null);
               case VALUE:
                  return CompletableFuture.completedFuture(4.2f);
            }
            break;
          }
       }
       return CompletableFuture.completedFuture(4.2f);
    }
}