@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); }
@Override public Aggregate<T> load(String aggregateIdentifier) { CurrentUnitOfWork.get().onRollback(u -> this.rolledBack = true); aggregate = delegate.load(aggregateIdentifier, null); validateIdentifier(aggregateIdentifier, aggregate); return aggregate; }
@CommandHandler public MyAggregate handle(MyCommand command) throws Exception { Aggregate<MyAggregate> aggregate; try { aggregate = repository.load(command.getAggregateIdentifier()); } catch (AggregateNotFoundException e) { aggregate = repository.newInstance(() -> new MyAggregate(command.getAggregateIdentifier())); } aggregate.execute(a -> a.handle(command, commandGateway)); return aggregate.invoke(Function.identity()); } }
@Override public Aggregate<T> newInstance(Callable<T> factoryMethod) throws Exception { CurrentUnitOfWork.get().onRollback(u -> this.rolledBack = true); aggregate = delegate.newInstance(factoryMethod); return aggregate; }
@Override public Aggregate<T> load(String aggregateIdentifier, Long expectedVersion) { CurrentUnitOfWork.get().onRollback(u -> this.rolledBack = true); aggregate = delegate.load(aggregateIdentifier, expectedVersion); validateIdentifier(aggregateIdentifier, aggregate); return aggregate; }
@CommandHandler public void handleIncomingMessage(String message) throws Exception { messageRepository.newInstance(() -> new Message(message)); }
@CommandHandler public void handleStubAggregateLooping(LoopingCommand command) { repository.load(command.getAggregateId().toString()) .execute(StubAggregate::makeALoopingChange); }
@CommandHandler public void handleStubAggregateCreated(CreateStubAggregateCommand command) throws Exception { repository.newInstance(() -> new StubAggregate(command.getAggregateId())); }
@CommandHandler public void handleStubAggregateUpdated(UpdateStubAggregateCommand command) { repository.load(command.getAggregateId().toString(), command.getAggregateVersion()) .execute(StubAggregate::makeAChange); }
@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 handleProblematicCommand(ProblematicCommand command) { repository.load(command.getAggregateId().toString(), command.getAggregateVersion()) .execute(StubAggregate::causeTrouble); }
private void detectIllegalStateChanges(MatchAllFieldFilter fieldFilter, Aggregate<T> workingAggregate) { logger.debug("Starting separate Unit of Work for the purpose of checking illegal state changes in Aggregate"); if (aggregateIdentifier != null && workingAggregate != null && reportIllegalStateChange) { UnitOfWork<?> uow = DefaultUnitOfWork.startAndGet(null); try { Aggregate<T> aggregate2 = repository.delegate.load(aggregateIdentifier); if (workingAggregate.isDeleted()) { throw new AxonAssertionError("The working aggregate was considered deleted, " + "but the Repository still contains a non-deleted copy of " + "the aggregate. Make sure the aggregate explicitly marks " + "itself as deleted in an EventHandler."); } assertValidWorkingAggregateState(aggregate2, fieldFilter, workingAggregate); } catch (AggregateNotFoundException notFound) { if (!workingAggregate.isDeleted()) { throw new AxonAssertionError("The working aggregate was not considered deleted, " //NOSONAR + "but the Repository cannot recover the state of the " + "aggregate, as it is considered deleted there."); } } catch (Exception e) { throw new FixtureExecutionException("An Exception occurred while reconstructing the Aggregate from " + "given and published events. This may be an indication " + "that the aggregate cannot be recreated from its events.", e); } finally { // rollback to prevent changes bing pushed to event store uow.rollback(); } } }
@CommandHandler @SuppressWarnings("unchecked") public void changeCounter(ChangeCounterCommand command) { repository.load(command.getAggregateId()).execute(r -> r.setCounter(command.getNewValue())); } }
@CommandHandler public void handleStubAggregateUpdatedWithExtraEvent(UpdateStubAggregateWithExtraEventCommand command, UnitOfWork<CommandMessage<?>> unitOfWork) { Aggregate<StubAggregate> aggregate = repository.load(command.getAggregateId().toString()); aggregate.execute(StubAggregate::makeAChange); eventBus.publish(new GenericEventMessage<>(new MyEvent())); aggregate.execute(StubAggregate::makeAChange); }
@Test public void orderInEventSourcedAggregate() { Repository<MyAggregate> repository = configuration.repository(MyAggregate.class); configuration.commandGateway().sendAndWait(command); UnitOfWork unitOfWork = DefaultUnitOfWork.startAndGet(GenericCommandMessage.asCommandMessage("loading")); MyAggregate loadedAggregate = repository.load(aggregateIdentifier).invoke(Function.identity()); unitOfWork.commit(); assertEquals(expectedDescriptions(command), loadedAggregate.getHandledCommands()); }