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

Java 构造函数SimpleCommandBus()不可见-Axon,java,spring-boot,axon,Java,Spring Boot,Axon,我正在开发springboot+Axon示例。我已经参考了。在本例中,当我将axon-core版本更新为4.0-M2时。当我更新Axon版本时,我发现我的主要方法是在第23行和第25行给出一些错误 依照 构造函数SimpleCommand总线()不可见 构造函数DefaultCommandGateway(CommandBus)未定义 MessageRunner.java public class MessagesRunner { public static void main(Stri

我正在开发
springboot+Axon
示例。我已经参考了。在本例中,当我将
axon-core
版本更新为
4.0-M2
时。当我更新Axon版本时,我发现我的主要方法是在第23行和第25行给出一些错误

依照

构造函数SimpleCommand总线()不可见

构造函数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));
    }
}
错误-

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$11(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
;)执行哪些操作

希望这有帮助

干杯,
米兰

这是一个完全不同的问题,然后建筑商采用@PAA的方法,因此与您创建的原始问题没有真正的联系。尽管如此,我还是建议尝试Axon 4.0,而不是里程碑式的尝试。里程碑不能完全保证没有bug。此外,MemoryEventsStorage Engine中的
通常仅用于测试场景。如果您真的想保留您的事件,我建议转到JPA、JDBC或更好的

编辑

很抱歉回复太晚,让我对您共享的代码段进行调整:

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));
    }
}
公共类消息运行器{
公共静态void main(字符串[]args){
//调整-使用生成器而不是构造函数
CommandBus CommandBus=SimpleCommandBus.builder().build();
//调整-使用生成器而不是构造函数
CommandGateway CommandGateway=DefaultCommandGateway.builder()
.commandBus(commandBus)
.build();
//调整-使用生成器而不是构造函数
EventStore EventStore=EmbeddedEventStore.builder()
.storageEngine(新的InMemoryEventsStorageEngine())
.build();
//调整-使用生成器而不是构造函数
EventSourcingRepository存储库=
EventSourcingRepository.builder(MessagesAggregate.class)
.eventStore(eventStore)
.build();
//调整-使用生成器而不是构造函数
AggregateAnnotationCommandHandler消息AggregateAnnotationCommandHandler=
AggregateNotationCommandHandler.builder()
.aggregateType(MessagesAggregate.class)
.repository(存储库)
.build();
messagesaggergegateaggregateannotationcommandhandler.subscribe(commandBus);
//调整-重命名的AnnotationEventListenerAdapter到AnnotationEventHandlerAdapter
最终注释EventHandlerAdapter注释EventListenerAdapter=
新注释EventHandlerAdapter(新消息Seventhandler());
订阅(eventMessages->eventMessages.forEach(e->{
试一试{
annotationEventListenerAdapter.handle(e);
}捕获(异常e1){
抛出新的运行时异常(e1);
}
}
));
最后一个字符串itemId=UUID.randomUUID().toString();
send(新的CreateMessageCommand(itemId,“你好,今天好吗?”:-)));
send(新的MarkReadMessageCommand(itemId));
}
}
我希望这能澄清我和米兰一直试图分享的解决方案!:-)
如果没有,一定要评论

好的,谢谢。你用上面提到的代码片段测试过了吗?@Steven-你能不能把答案粘贴得更详细一些,最好添加一段代码片段。啊,很抱歉@PAA,完全错过了你的请求。我刚刚用您的代码片段的副本更新了我的响应,在这里我调整了所有必要的位以符合Axon 4。
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));
    }
}