/** * Initializes a {@link EventSourcingRepository} or {@link CachingEventSourcingRepository} as specified through * this Builder. Will return a CachingEventSourcingRepository if {@link #cache(Cache)} has been set. Otherwise * builds a regular EventSourcingRepository * * @param <R> a generic extending {@link EventSourcingRepository}, so allowing both an EventSourcingRepository * and {@link CachingEventSourcingRepository} return type * @return a {@link EventSourcingRepository} or {@link CachingEventSourcingRepository} (if {@link #cache(Cache)} * has been set) as specified through this Builder */ @SuppressWarnings("unchecked") public <R extends EventSourcingRepository<T>> R build() { return cache != null ? (R) new CachingEventSourcingRepository<>(this) : (R) new EventSourcingRepository<>(this); }
@Override protected EventSourcedAggregate<T> doCreateNewForLock(Callable<T> factoryMethod) throws Exception { return EventSourcedAggregate.initialize(factoryMethod, aggregateModel(), eventStore, repositoryProvider, snapshotTriggerDefinition.prepareTrigger(getAggregateType())); }
/** * Perform the actual loading of an aggregate. The necessary locks have been obtained. * * @param aggregateIdentifier the identifier of the aggregate to load * @param expectedVersion The expected version of the loaded aggregate * @return the fully initialized aggregate * * @throws AggregateDeletedException in case an aggregate existed in the past, but has been deleted * @throws AggregateNotFoundException when an aggregate with the given identifier does not exist */ @Override protected EventSourcedAggregate<T> doLoadWithLock(String aggregateIdentifier, Long expectedVersion) { DomainEventStream eventStream = readEvents(aggregateIdentifier); SnapshotTrigger trigger = snapshotTriggerDefinition.prepareTrigger(aggregateFactory.getAggregateType()); if (!eventStream.hasNext()) { throw new AggregateNotFoundException(aggregateIdentifier, "The aggregate was not found in the event store"); } EventSourcedAggregate<T> aggregate = EventSourcedAggregate .initialize(aggregateFactory.createAggregateRoot(aggregateIdentifier, eventStream.peek()), aggregateModel(), eventStore, repositoryProvider, trigger); aggregate.initializeState(eventStream); if (aggregate.isDeleted()) { throw new AggregateDeletedException(aggregateIdentifier); } return aggregate; }
/** * Our aggregate root is now created from stream of events and not from a representation in a persistent mechanism, * thus we need a repository that can handle the retrieving of our aggregate root from the stream of events. * * We configure the EventSourcingRepository which does exactly this. We supply it with the event store * @return a {@link EventSourcingRepository} implementation of {@link Repository} */ @Bean public Repository<Account> eventSourcingRepository() { EventSourcingRepository eventSourcingRepository = new EventSourcingRepository(Account.class, jdbcEventStore()); eventSourcingRepository.setEventBus(clusteringEventBus()); return eventSourcingRepository; }
@Override protected void validateOnLoad(Aggregate<T> aggregate, Long expectedVersion) { CurrentUnitOfWork.get().onRollback(u -> cache.remove(aggregate.identifierAsString())); super.validateOnLoad(aggregate, expectedVersion); }
@Override protected void doSaveWithLock(EventSourcedAggregate<T> aggregate) { super.doSaveWithLock(aggregate); cache.put(aggregate.identifierAsString(), new AggregateCacheEntry<>(aggregate)); }
@Override protected EventSourcedAggregate<T> doCreateNewForLock(Callable<T> factoryMethod) throws Exception { EventSourcedAggregate<T> aggregate = super.doCreateNewForLock(factoryMethod); CurrentUnitOfWork.get().onRollback(u -> cache.remove(aggregate.identifierAsString())); cache.put(aggregate.identifierAsString(), new AggregateCacheEntry<>(aggregate)); return aggregate; }
@Override protected void doDeleteWithLock(EventSourcedAggregate<T> aggregate) { super.doDeleteWithLock(aggregate); cache.put(aggregate.identifierAsString(), new AggregateCacheEntry<>(aggregate)); }
/** * Perform the actual loading of an aggregate. The necessary locks have been obtained. If the aggregate is * available in the cache, it is returned from there. Otherwise the underlying persistence logic is called to * retrieve the aggregate. * * @param aggregateIdentifier the identifier of the aggregate to load * @param expectedVersion The expected version of the aggregate * @return the fully initialized aggregate */ @Override protected EventSourcedAggregate<T> doLoadWithLock(String aggregateIdentifier, Long expectedVersion) { EventSourcedAggregate<T> aggregate = null; AggregateCacheEntry<T> cacheEntry = cache.get(aggregateIdentifier); if (cacheEntry != null) { aggregate = cacheEntry.recreateAggregate(aggregateModel(), eventStore, repositoryProvider, snapshotTriggerDefinition); } if (aggregate == null) { aggregate = super.doLoadWithLock(aggregateIdentifier, expectedVersion); } else if (aggregate.isDeleted()) { throw new AggregateDeletedException(aggregateIdentifier); } return aggregate; } }
/** * Perform the actual loading of an aggregate. The necessary locks have been obtained. * * @param aggregateIdentifier the identifier of the aggregate to load * @param expectedVersion The expected version of the loaded aggregate * @return the fully initialized aggregate * * @throws AggregateDeletedException in case an aggregate existed in the past, but has been deleted * @throws AggregateNotFoundException when an aggregate with the given identifier does not exist */ @Override protected EventSourcedAggregate<T> doLoadWithLock(String aggregateIdentifier, Long expectedVersion) { DomainEventStream eventStream = readEvents(aggregateIdentifier); SnapshotTrigger trigger = snapshotTriggerDefinition.prepareTrigger(aggregateFactory.getAggregateType()); if (!eventStream.hasNext()) { throw new AggregateNotFoundException(aggregateIdentifier, "The aggregate was not found in the event store"); } EventSourcedAggregate<T> aggregate = EventSourcedAggregate .initialize(aggregateFactory.createAggregateRoot(aggregateIdentifier, eventStream.peek()), aggregateModel(), eventStore, repositoryProvider, trigger); aggregate.initializeState(eventStream); if (aggregate.isDeleted()) { throw new AggregateDeletedException(aggregateIdentifier); } return aggregate; }
@Override protected void validateOnLoad(Aggregate<T> aggregate, Long expectedVersion) { CurrentUnitOfWork.get().onRollback(u -> cache.remove(aggregate.identifierAsString())); super.validateOnLoad(aggregate, expectedVersion); }
@Override protected void doSaveWithLock(EventSourcedAggregate<T> aggregate) { super.doSaveWithLock(aggregate); cache.put(aggregate.identifierAsString(), new AggregateCacheEntry<>(aggregate)); }
@Override protected EventSourcedAggregate<T> doCreateNewForLock(Callable<T> factoryMethod) throws Exception { EventSourcedAggregate<T> aggregate = super.doCreateNewForLock(factoryMethod); CurrentUnitOfWork.get().onRollback(u -> cache.remove(aggregate.identifierAsString())); cache.put(aggregate.identifierAsString(), new AggregateCacheEntry<>(aggregate)); return aggregate; }
@Override protected void doDeleteWithLock(EventSourcedAggregate<T> aggregate) { super.doDeleteWithLock(aggregate); cache.put(aggregate.identifierAsString(), new AggregateCacheEntry<>(aggregate)); }
/** * Perform the actual loading of an aggregate. The necessary locks have been obtained. If the aggregate is * available in the cache, it is returned from there. Otherwise the underlying persistence logic is called to * retrieve the aggregate. * * @param aggregateIdentifier the identifier of the aggregate to load * @param expectedVersion The expected version of the aggregate * @return the fully initialized aggregate */ @Override protected EventSourcedAggregate<T> doLoadWithLock(String aggregateIdentifier, Long expectedVersion) { EventSourcedAggregate<T> aggregate = null; AggregateCacheEntry<T> cacheEntry = cache.get(aggregateIdentifier); if (cacheEntry != null) { aggregate = cacheEntry.recreateAggregate(aggregateModel(), eventStore, repositoryProvider, snapshotTriggerDefinition); } if (aggregate == null) { aggregate = super.doLoadWithLock(aggregateIdentifier, expectedVersion); } else if (aggregate.isDeleted()) { throw new AggregateDeletedException(aggregateIdentifier); } return aggregate; }
@Test public void testInterceptorWithDeclaredChainAllowedToDeclareNonVoidReturnType() { EventSourcingRepository.builder(MyAggregateWithDeclaredInterceptorChainInterceptorReturningNonVoid.class) .eventStore(eventStore) .build(); }
@Override protected EventSourcedAggregate<T> doCreateNewForLock(Callable<T> factoryMethod) throws Exception { return EventSourcedAggregate.initialize(factoryMethod, aggregateModel(), eventStore, repositoryProvider, snapshotTriggerDefinition.prepareTrigger(getAggregateType())); }
/** * Perform the actual loading of an aggregate. The necessary locks have been obtained. * * @param aggregateIdentifier the identifier of the aggregate to load * @param expectedVersion The expected version of the loaded aggregate * @return the fully initialized aggregate * @throws AggregateDeletedException in case an aggregate existed in the past, but has been deleted * @throws AggregateNotFoundException when an aggregate with the given identifier does not exist */ @Override protected EventSourcedAggregate<T> doLoadWithLock(String aggregateIdentifier, Long expectedVersion) { DomainEventStream eventStream = readEvents(aggregateIdentifier); SnapshotTrigger trigger = snapshotTriggerDefinition.prepareTrigger(aggregateFactory.getAggregateType()); if (!eventStream.hasNext()) { throw new AggregateNotFoundException(aggregateIdentifier, "The aggregate was not found in the event store"); } EventSourcedAggregate<T> aggregate = EventSourcedAggregate .initialize(aggregateFactory.createAggregateRoot(aggregateIdentifier, eventStream.peek()), aggregateModel(), eventStore, repositoryProvider, trigger); aggregate.initializeState(eventStream); if (aggregate.isDeleted()) { throw new AggregateDeletedException(aggregateIdentifier); } return aggregate; }
/** * Initializes a {@link EventSourcingRepository} or {@link CachingEventSourcingRepository} as specified through * this Builder. Will return a CachingEventSourcingRepository if {@link #cache(Cache)} has been set. Otherwise * builds a regular EventSourcingRepository * * @param <R> a generic extending {@link EventSourcingRepository}, so allowing both an EventSourcingRepository * and {@link CachingEventSourcingRepository} return type * @return a {@link EventSourcingRepository} or {@link CachingEventSourcingRepository} (if {@link #cache(Cache)} * has been set) as specified through this Builder */ @SuppressWarnings("unchecked") public <R extends EventSourcingRepository<T>> R build() { return cache != null ? (R) new CachingEventSourcingRepository<>(this) : (R) new EventSourcingRepository<>(this); }