构造函数 SimpleCommandBus() 不可见 -Axon

The constructor SimpleCommandBus() is not visible -Axon

我正在开发 Spring Boot + Axon 示例。我参考了https://www.baeldung.com/axon-cqrs-event-sourcing。在此示例中,当我将 axon-core 版本更新为 4.0-M2 时。当我更新 Axon 版本时,我发现我的主要方法在第 23 和 25 行给出了一些错误。

根据

构造函数 SimpleCommandBus() 不可见

构造函数 DefaultCommandGateway(CommandBus) 未定义

MessageRunner.java

public class MessagesRunner {

    public static void main(String[] args) {
        CommandBus commandBus = new SimpleCommandBus(); //Line-23

        CommandGateway commandGateway = new DefaultCommandGateway(commandBus); // Line-25

        EventStore eventStore = new EmbeddedEventStore(new InMemoryEventStorageEngine());

        EventSourcingRepository<MessagesAggregate> repository =
                new EventSourcingRepository<>(MessagesAggregate.class, eventStore);


        AggregateAnnotationCommandHandler<MessagesAggregate> messagesAggregateAggregateAnnotationCommandHandler =
                new AggregateAnnotationCommandHandler<MessagesAggregate>(MessagesAggregate.class, repository);
        messagesAggregateAggregateAnnotationCommandHandler.subscribe(commandBus);

        final AnnotationEventListenerAdapter annotationEventListenerAdapter =
                new AnnotationEventListenerAdapter(new MessagesEventHandler());
        eventStore.subscribe(eventMessages -> eventMessages.forEach(e -> {
                    try {
                        annotationEventListenerAdapter.handle(e);
                    } catch (Exception e1) {
                        throw new RuntimeException(e1);

                    }
                }

        ));

        final String itemId = UUID.randomUUID().toString();
        commandGateway.send(new CreateMessageCommand(itemId, "Hello, how is your day? :-)"));
        commandGateway.send(new MarkReadMessageCommand(itemId));
    }
}

错误 -

Exception in thread "main" java.lang.Error: Unresolved compilation problems: The constructor SimpleCommandBus() is not visible The constructor DefaultCommandGateway(CommandBus) is undefined at com.example.demo.MessagesRunner.main(MessagesRunner.java:23)

编辑-1

@ Milan Savic - 我更新了如下代码,但在最后一行执行时出现以下错误。我错过了什么吗?

CommandBus commandBus = new SimpleCommandBus.Builder().build();
CommandGateway commandGateway = DefaultCommandGateway.builder().commandBus(commandBus).build();

错误 -

00:27:40.704 [main] WARN org.axonframework.eventsourcing.eventstore.AbstractEventStore - Error reading snapshot for aggregate [67f0747f-a0fd-4089-9cc3-fb1fe4662cca]. Reconstructing from entire event stream.
java.lang.NullPointerException: null
    at org.axonframework.eventsourcing.eventstore.inmemory.InMemoryEventStorageEngine.readSnapshot(InMemoryEventStorageEngine.java:105)
    at org.axonframework.eventsourcing.eventstore.AbstractEventStore.readEvents(AbstractEventStore.java:80)
    at org.axonframework.eventsourcing.EventSourcingRepository.readEvents(EventSourcingRepository.java:427)
    at org.axonframework.eventsourcing.EventSourcingRepository.doLoadWithLock(EventSourcingRepository.java:404)
    at org.axonframework.eventsourcing.EventSourcingRepository.doLoadWithLock(EventSourcingRepository.java:48)
    at org.axonframework.commandhandling.model.LockingRepository.doLoad(LockingRepository.java:195)
    at org.axonframework.commandhandling.model.LockingRepository.doLoad(LockingRepository.java:50)
    at org.axonframework.commandhandling.model.AbstractRepository.lambda$load(AbstractRepository.java:151)
    at java.util.HashMap.computeIfAbsent(Unknown Source)
    at org.axonframework.commandhandling.model.AbstractRepository.load(AbstractRepository.java:150)
    at org.axonframework.commandhandling.AggregateAnnotationCommandHandler$AggregateCommandHandler.handle(AggregateAnnotationCommandHandler.java:219)
    at org.axonframework.commandhandling.AggregateAnnotationCommandHandler$AggregateCommandHandler.handle(AggregateAnnotationCommandHandler.java:213)
    at org.axonframework.commandhandling.AggregateAnnotationCommandHandler.handle(AggregateAnnotationCommandHandler.java:175)
    at org.axonframework.commandhandling.AggregateAnnotationCommandHandler.handle(AggregateAnnotationCommandHandler.java:44)
    at org.axonframework.messaging.DefaultInterceptorChain.proceed(DefaultInterceptorChain.java:57)
    at org.axonframework.messaging.unitofwork.DefaultUnitOfWork.executeWithResult(DefaultUnitOfWork.java:69)
    at org.axonframework.commandhandling.SimpleCommandBus.handle(SimpleCommandBus.java:176)
    at org.axonframework.commandhandling.SimpleCommandBus.doDispatch(SimpleCommandBus.java:146)
    at org.axonframework.commandhandling.SimpleCommandBus.dispatch(SimpleCommandBus.java:110)
    at org.axonframework.commandhandling.gateway.AbstractCommandGateway.send(AbstractCommandGateway.java:75)
    at org.axonframework.commandhandling.gateway.DefaultCommandGateway.send(DefaultCommandGateway.java:75)
    at org.axonframework.commandhandling.gateway.DefaultCommandGateway.send(DefaultCommandGateway.java:123)
    at com.example.demo.MessagesRunner.main(MessagesRunner.java:52)

在 4.0 版中,我们决定为复杂的基础结构组件引入构建器模式。这样做的原因是使用框架更具可读性,另一方面,在向基础结构组件添加新字段时,它为我们提供了更大的灵活性。

因此,构建 SimpleCommandBus 将如下所示:SimpleCommandBus.builder().build();。你可以猜出 DefaultCommandGateway 需要做什么 ;)

希望对您有所帮助!

干杯, 米兰

这与 Builder 方法 @PAA 完全不同,因此与您创建的原始问题没有真正的联系。尽管如此,我还是建议试用 Axon 4.0 而不是 Milestone。不能完全保证里程碑没有错误。此外,InMemoryEventStorageEngine 通常仅用于测试场景。如果你真的想保留你的事件,我建议转移到 JPA,JDBC,或者更好的,Axon Server

编辑

抱歉回复晚了,让我对您分享的代码片段进行调整:

public class MessagesRunner {

    public static void main(String[] args) {
        // Adjusted - Used builder instead of constructor
        CommandBus commandBus = SimpleCommandBus.builder().build();

        // Adjusted - Used builder instead of constructor
        CommandGateway commandGateway = DefaultCommandGateway.builder()
                .commandBus(commandBus)
                .build();

        // Adjusted - Used builder instead of constructor
        EventStore eventStore = EmbeddedEventStore.builder()
                .storageEngine(new InMemoryEventStorageEngine())
                .build();

        // Adjusted - Used builder instead of constructor
        EventSourcingRepository<MessagesAggregate> repository =
                EventSourcingRepository.builder(MessagesAggregate.class)
                        .eventStore(eventStore)
                        .build();


        // Adjusted - Used builder instead of constructor
        AggregateAnnotationCommandHandler<MessagesAggregate> messagesAggregateAggregateAnnotationCommandHandler =
                AggregateAnnotationCommandHandler.<MessagesAggregate>builder()
                        .aggregateType(MessagesAggregate.class)
                        .repository(repository)
                        .build();

        messagesAggregateAggregateAnnotationCommandHandler.subscribe(commandBus);

        // Adjusted - Renamed AnnotationEventListenerAdapter to AnnotationEventHandlerAdapter
        final AnnotationEventHandlerAdapter annotationEventListenerAdapter =
                new AnnotationEventHandlerAdapter(new MessagesEventHandler());
        eventStore.subscribe(eventMessages -> eventMessages.forEach(e -> {
                    try {
                        annotationEventListenerAdapter.handle(e);
                    } catch (Exception e1) {
                        throw new RuntimeException(e1);

                    }
                }

        ));

        final String itemId = UUID.randomUUID().toString();
        commandGateway.send(new CreateMessageCommand(itemId, "Hello, how is your day? :-)"));
        commandGateway.send(new MarkReadMessageCommand(itemId));
    }
}

我希望这能澄清我和 Milan 一直试图分享的解决方案! :-) 如果没有,一定要评论它!