Esempio n. 1
0
 private void removeEntry(final String sagaIdentifier, final Map<String, ?> sagaMap) {
   if (!CurrentUnitOfWork.isStarted()) {
     sagaMap.remove(sagaIdentifier);
   } else {
     CurrentUnitOfWork.get()
         .registerListener(
             new UnitOfWorkListenerAdapter() {
               @Override
               public void afterCommit(UnitOfWork unitOfWork) {
                 sagaMap.remove(sagaIdentifier);
               }
             });
   }
 }
Esempio n. 2
0
 private void doReleaseLock(final String sagaId, final Saga sagaInstance) {
   if (sagaInstance == null || !CurrentUnitOfWork.isStarted()) {
     lock.releaseLock(sagaId);
   } else if (CurrentUnitOfWork.isStarted()) {
     CurrentUnitOfWork.get()
         .registerListener(
             new UnitOfWorkListenerAdapter() {
               @Override
               public void onCleanup(UnitOfWork unitOfWork) {
                 // a reference to the saga is maintained to prevent it from GC until after the UoW
                 // commit
                 lock.releaseLock(sagaInstance.getSagaIdentifier());
               }
             });
   }
 }
  /**
   * 根据聚合标识符和 指定 版本加载 聚合
   *
   * @param aggregateIdentifier the identifier of the aggregate to load
   * @param expectedVersion The expected version of the loaded aggregate
   * @return the fully initialized aggregate
   */
  @Override
  protected T doLoad(ID aggregateIdentifier, final Long expectedVersion) {
    AggregateEventStream events = null;
    AggregateEventStream originalStream = null;
    try {
      try {
        events = AggregateEventStore.readEvents(getTypeIdentifier(), aggregateIdentifier);
      } catch (EventStreamNotFoundException e) {
        throw new AggregateNotFoundException(aggregateIdentifier, "The aggregate was not found", e);
      }
      originalStream = events;
      for (EventStreamDecorator decorator : eventStreamDecorators) {
        events = decorator.decorateForRead(getTypeIdentifier(), aggregateIdentifier, events);
      }

      final T aggregate = aggregateFactory.createAggregate(aggregateIdentifier, events.peek());
      List<AggregateEvent> unseenEvents = new ArrayList<AggregateEvent>();
      aggregate.initializeState(new CapturingEventStream(events, unseenEvents, expectedVersion));
      if (aggregate.isDeleted()) {
        throw new AggregateDeletedException(aggregateIdentifier);
      }
      CurrentUnitOfWork.get()
          .registerListener(new ConflictResolvingListener(aggregate, unseenEvents));

      return aggregate;
    } finally {
      IOUtils.closeQuietlyIfCloseable(events);
      // if a decorator doesn't implement closeable, we still want to be sure we close the original
      // stream
      IOUtils.closeQuietlyIfCloseable(originalStream);
    }
  }
 /**
  * 解决 聚合 的事件 并发 竞争
  *
  * @param aggregate The aggregate containing the potential conflicts
  * @param unseenEvents The events that have been concurrently applied
  */
 protected void resolveConflicts(T aggregate, AggregateEventStream unseenEvents) {
   CurrentUnitOfWork.get()
       .registerListener(new ConflictResolvingListener(aggregate, asList(unseenEvents)));
 }