@Override protected <R> Aggregate<R> doCreateNew(Class<R> aggregateType, Callable<R> factoryMethod) throws Exception { if (repositoryProvider == null) { throw new AxonConfigurationException(format( "Since repository provider is not provided, we cannot spawn a new aggregate for %s", aggregateType.getName())); } Repository<R> repository = repositoryProvider.repositoryFor(aggregateType); if (repository == null) { throw new IllegalStateException(format("There is no configured repository for %s", aggregateType.getName())); } return repository.newInstance(factoryMethod); }
@CommandHandler public void handle(CreditDestinationBankAccountCommand command) { try { Aggregate<BankAccount> bankAccountAggregate = repository.load(command.getBankAccountId()); bankAccountAggregate.execute(bankAccount -> bankAccount .credit(command.getAmount(), command.getBankTransferId())); } catch (AggregateNotFoundException exception) { eventBus.publish(asEventMessage(new DestinationBankAccountNotFoundEvent(command.getBankTransferId()))); } } }
@CommandHandler public void handle(DebitSourceBankAccountCommand command) { try { Aggregate<BankAccount> bankAccountAggregate = repository.load(command.getBankAccountId()); bankAccountAggregate.execute(bankAccount -> bankAccount .debit(command.getAmount(), command.getBankTransferId())); } catch (AggregateNotFoundException exception) { eventBus.publish(asEventMessage(new SourceBankAccountNotFoundEvent(command.getBankTransferId()))); } }