public interface AsyncContext
| Modifier and Type | Method and Description |
|---|---|
void |
async(ActorFuture<?> future)
Use with care
|
void async(ActorFuture<?> future)
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);
}
future - the future to passCopyright © 2017–2019 camunda services GmbH. All rights reserved.