Hi all,
I am trying to create an external command handler called SplitService – I have some commands that need to operate on more than one aggregate. I can explain the details around this if necessary, but I think they don’t matter.
I have read the documentation about External Command Handlers and have configured my command handler like:
`
@Configuration
public class AxonConfig {
…
@Autowired
public void configure(Configurer configurer, CommandGateway commandGateway, QueryGateway queryGateway,
EventBus eventBus) {
configurer.registerCommandHandler(ch -> new SplitService(commandGateway, queryGateway, eventBus));
}
…
}
`
The service in question looks like this:
`
@Component
public class SplitService {
private CommandGateway commandGateway;
private QueryGateway queryGateway;
private EventBus eventBus;
@Autowired
public SplitService(CommandGateway commandGateway, QueryGateway queryGateway, EventBus eventBus) {
this.commandGateway = commandGateway;
this.queryGateway = queryGateway;
this.eventBus = eventBus;
}
@CommandHandler
public String handle(SplitPackage command) {
PackageRecord parentPackageRecord;
try {
parentPackageRecord =
queryGateway.query(new PackageRecordByIdQuery(command.getParentPackageId()), PackageRecord.class).get();
}
catch (InterruptedException | ExecutionException e) {
// TODO: research proper handling for this scenario
throw new RuntimeException(e);
}
// content id mappings
LinkedHashMap<String, String> newChildContentIdToSplitParentContentId = new LinkedHashMap<>();
String childPackageId = buildAdnStoreChild(parentPackageRecord, command, newChildContentIdToSplitParentContentId);
updateParent(parentPackageRecord, command);
// Publish milestone event…
eventBus.publish(new GenericEventMessage(new PackageSplit(command.getExecutorInfo(),
command.getParentPackageId(), command.getChildPackageId(), newChildContentIdToSplitParentContentId)));
return childPackageId;
}
/**
- Store the child package, returning it’s identifier when successful otherwise a RuntimeException is raised.