0

I am developing Spring Boot + Axon example. I have taken reference from https://www.baeldung.com/axon-cqrs-event-sourcing. In this example, when I updated axon-core version to 4.0-M2. When I updated Axon version I see that my main method is giving some error on line No-23 & 25.

As per

The constructor SimpleCommandBus() is not visible

The constructor DefaultCommandGateway(CommandBus) is undefined

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));
    }
}

Error -

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)

Edit-1

@ Milan Savic - I updated code like below, but I am getting below error on very last line execution. Did I missed anything here ?

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

Error -

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)
PAA
  • 1
  • 46
  • 174
  • 282

2 Answers2

1

In version 4.0 we've decided to introduce the Builder pattern for our complex infrastructure components. The reason for this is that using the framework is more readable and on the other side it gives us greater flexibility when it comes to adding new fields to the infrastructure components.

So, constructing SimpleCommandBus would look like this: SimpleCommandBus.builder().build();. You can guess what needs to be done for DefaultCommandGateway ;)

Hope this helps!

Cheers, Milan

0

That's a different issue entirely then the Builder approach @PAA, so not really tied to the original issue you've created. Nonetheless, I'd suggest trying out Axon 4.0 rather than a Milestone. Milestones are not fully guaranteed to be bug free. Additionally, the InMemoryEventStorageEngine is typically only used for testing scenarios. If you actually want to retain your events, I suggest moving over to JPA, JDBC, or nicer, Axon Server.

Edit

Sorry for the late reply, let me give you an adjustment of the code snippet you've shared:

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));
    }
}

I hope this clarifies the solution me and Milan have been trying to share! :-) And if not, definitely comment on it!

Steven
  • 6,936
  • 1
  • 16
  • 31
  • Ok Sure Thanks will do. Did you tested this with the said code snippet ? – PAA Nov 07 '18 at 10:05
  • @ Steven - Could you please paste the answer in little details and better add piece of code snippet. – PAA Nov 20 '18 at 14:37
  • Ah sorry for that @PAA, totally missed your request. I've just updated my response with a copy of your code snippet, where I've adjusted all the necessary bits to be compliant with Axon 4. – Steven Nov 21 '18 at 17:30