Axon & CompletableFuture

那年仲夏 提交于 2019-12-24 12:12:48

问题


I've faced with problems when i try to use CompletableFuture with Axon. For example:

CompletableFuture future = CompletableFuture.supplyAsync(() -> {

            log.info("Start processing target: {}", target.toString());
            return new Event();

        }, threadPool);

future.thenAcceptAsync(event -> {
            log.info("Send Event");
            AggregateLifecycle.apply(event);
}, currentExecutor);

in thenAcceptAsync - AggregateLifecycle.apply(event) has unexpected behavior. Some of my @EventSourcingHandler handlers start handling event twice. Does anybody know how to fix it?

I have been reading docs and everything that i got is:

In most cases, the DefaultUnitOfWork will provide you with the functionality you need. It expects processing to happen within a single thread.

so, it seems i should use somehow CurrentUnitOfWork.get/set methods but still can't understand Axon API.


回答1:


You should not apply() events asynchronously. The apply() method will call the aggregate's internal @EventSourcingHandler methods and schedule the event for publication when the unit of work completes (successfully). The way Axon works with the Unit of Work (which coordinates activity of a single message handler invocation), the apply() method must be invoked in the thread that manages that Unit of Work.

If you want asynchronous publication of Events, use an Event Bus that uses an Async Transport, and use Tracking Processors.



来源:https://stackoverflow.com/questions/52937596/axon-completablefuture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!