有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java构造函数SimpleCommandBus()不可见

我正在开发Spring Boot + Axon示例。我引用了https://www.baeldung.com/axon-cqrs-event-sourcing中的参考文献。在本例中,当我将axon-core版本更新为4.0-M2时。当我更新Axon版本时,我发现我的主要方法是在第23行&;二十五,

依照

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

错误-

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$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)

共 (2) 个答案

  1. # 1 楼答案

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

    因此,构造SimpleCommandBus将如下所示:SimpleCommandBus.builder().build();。您可以猜测需要为DefaultCommandGateway;)做什么

    希望这有帮助

    干杯, 米兰

  2. # 2 楼答案

    这与构建器方法@PAA是完全不同的问题,因此与您创建的原始问题没有真正的联系。尽管如此,我还是建议尝试Axon 4.0,而不是里程碑式的尝试。里程碑不能完全保证没有bug。此外,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));
        }
    }
    

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