Interface AsyncContext


public interface AsyncContext
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    async(ActorFuture<?> future)
    Use with care
  • Method Details

    • async

      void async(ActorFuture<?> future)
      Use with care

      Note that the following pattern does not work

       void fetch(AsyncContext ctx)
       {
            ActorFuture future = asyncApi.fetchData();
      
            actor.runOnCompletion(future, (someData, err) ->
            {
                // do something
            });
      
            ctx.async(future);
       }
      
       
      The problem is that now, if another component is awaiting on future, both this component and the actor.runOnCompletion are waiting on the same future, and there is no guaranteed order in which these are invoked, which could lead to unexpected behaviour (e.g. the stream processor moves to the next processing stage before the callback runs).

      You can refactor it to this:

       void process(AsyncContext ctx)
       {
            ActorFuture future = asyncApi.fetchData();
            ActorFuture whenProcessingDone = new CompletableActorFuture();
      
            actor.runOnCompletion(future, (someData, err) ->
            {
                // do something
                whenProcessingDone.complete(null);
            });
      
            ctx.async(whenProcessingDone);
       }
       
      Parameters:
      future - the future to pass