I am building a monolith project using Axon framework.
I have a method here that dispatches an event
@CommandHandler
public void createRoom(CreateRoomCommand createRoomCommand){
log.info("Receiving create room command: {}",createRoomCommand);
ChatRoom chatRoom = new ChatRoom(
createRoomCommand.getRoomId(),
createRoomCommand.getName()
);
chatRoom.create();
List<DomainEvent> domainEvents = chatRoom.getDomainEvents();
domainEvents.forEach(domainEvent -> eventBus.publish(GenericEventMessage.asEventMessage(domainEvent)));
chatRoomRepository.save(chatRoom);
}
And this is my event handler that might throw an exception
@EventHandler
public void on(RoomCreatedEvent event){
//throw exception here
}
My question is, how should I handle the exception?
I thought of two things:
Should I dispatch an event from the event handler to reverse the transaction?
How do I make eventBus.publish synchronous so that they can fail as one transaction?