I am playing around with axon server locally. I am running a docker container on my local machine via the command docker run -d --name axonserver -p 8024:8024 -p 8124:8124 axoniq/axonserver
.
When I start my spring-boot application, my event handler that is external to an aggregate reruns all previous events so I see this stream of log statements on startup.
The same event handler also publishes a command to an aggregate, which then appends a new event to the aggregate that processes the commands. So my aggregate ends up getting a few events tacked on the end of it each time I reboot the application when I would only want or expected one.
flsh.axon.LetterSchedulingHandler : Sending letter 0e94035a-ec4b-4fdc-b8b6-4c0bc1927c8f...
flsh.axon.LetterSchedulingHandler : Sending letter 6b4f6966-85ea-46e0-9c49-21bcd501a1b5...
flsh.axon.LetterSchedulingHandler : Sending letter fc36292f-c7bd-4575-b56f-130624a87466...
flsh.axon.Letter : LetterScheduledEvent 0e94035a-ec4b-4fdc-b8b6-4c0bc1927c8f SCHEDULED
flsh.axon.Letter : LetterScheduledEvent 6b4f6966-85ea-46e0-9c49-21bcd501a1b5 SCHEDULED
flsh.axon.Letter : LetterScheduledEvent fc36292f-c7bd-4575-b56f-130624a87466 SCHEDULED
flsh.axon.Letter : Letter sent fc36292f-c7bd-4575-b56f-130624a87466 SENT
flsh.axon.Letter : Letter sent 0e94035a-ec4b-4fdc-b8b6-4c0bc1927c8f SENT
flsh.axon.Letter : Letter sent 6b4f6966-85ea-46e0-9c49-21bcd501a1b5 SENT
flsh.axon.Letter : Letter sent 0e94035a-ec4b-4fdc-b8b6-4c0bc1927c8f SENT
flsh.axon.Letter : Letter sent 0e94035a-ec4b-4fdc-b8b6-4c0bc1927c8f SENT
flsh.axon.Letter : Letter sent fc36292f-c7bd-4575-b56f-130624a87466 SENT
flsh.axon.Letter : Letter sent 6b4f6966-85ea-46e0-9c49-21bcd501a1b5 SENT
My event handler looks like this:
@Slf4j
@Component
public class LetterSchedulingHandler {
private final CommandGateway commandGateway;
public LetterSchedulingHandler(CommandGateway commandGateway) {
this.commandGateway = commandGateway;
}
@DisallowReplay //this doesn't seem to work
@EventHandler
public void handle(BeginSendLetterEvent event) {
log.info("Sending letter {}...", event.getLetterId());
commandGateway.send(new LetterSentCommand(event.getLetterId()));
}
}
@CommandHandler
public void handle(LetterSentCommand cmd) {
AggregateLifecycle.apply(new LetterSentEvent(cmd.getLetterId()));
}
These events are being published via a scheduler...that is is run in an aggregate via a different @CommandHandler
and do otherwise get successfully run as expected.
@CommandHandler
public Letter(ScheduleLetterCommand cmd, EventScheduler scheduler) {
String id = cmd.getLetterId();
log.info("Received schedule command for letter id {}", id);
ScheduleToken scheduleToken = scheduler.schedule(Duration.ofSeconds(5), new BeginSendLetterEvent(id));
AggregateLifecycle.apply(new LetterScheduledEvent(id, scheduleToken));
}
The @DisallowReplay
annotation doesn't seem to prevent this. Also, I tried to follow the directions here to make the handler a Subscribing
event processor but either I didn't do it right or it also didn't solve the problem.
axon:
axonserver:
servers: localhost
eventhandling:
processors:
LetterSchedulingHandler:
mode: subscribing