private void registerAggregateCommandHandlers() { ensureRepositoryConfiguration(); if (!explicitCommandHandlersSet) { AggregateAnnotationCommandHandler.Builder<T> builder = AggregateAnnotationCommandHandler.<T>builder() .aggregateType(aggregateType) .parameterResolverFactory(parameterResolverFactory) .repository(this.repository); if (commandTargetResolver != null) { builder.commandTargetResolver(commandTargetResolver); } AggregateAnnotationCommandHandler<T> handler = builder.build(); handler.subscribe(commandBus); } }
/** * Initializes a {@link AggregateAnnotationCommandHandler} as specified through this Builder. * * @return a {@link AggregateAnnotationCommandHandler} as specified through this Builder */ public AggregateAnnotationCommandHandler<T> build() { return new AggregateAnnotationCommandHandler<>(this); }
/** * Instantiate a {@link AggregateAnnotationCommandHandler} based on the fields contained in the {@link Builder}. * <p> * Will assert that the {@link Repository} and {@link CommandTargetResolver} are not {@code null}, and will throw * an {@link AxonConfigurationException} if either of them is {@code null}. Next to that, the provided Builder's * goal is to create an {@link AggregateModel} (describing the structure of a given aggregate). To instantiate this * AggregateModel, either an {@link AggregateModel} can be provided directly or an {@code aggregateType} of type * {@link Class} can be used. The latter will internally resolve to an AggregateModel. Thus, either the * AggregateModel <b>or</b> the {@code aggregateType} should be provided. An AxonConfigurationException is thrown * if this criteria is not met. * * @param builder the {@link Builder} used to instantiate a {@link AggregateAnnotationCommandHandler} instance */ protected AggregateAnnotationCommandHandler(Builder<T> builder) { builder.validate(); this.repository = builder.repository; this.commandTargetResolver = builder.commandTargetResolver; this.supportedCommandNames = new HashSet<>(); this.handlers = initializeHandlers(builder.buildAggregateModel()); }
@Override public void start() { registrations.add(commandHandler.get().subscribe(parent.commandBus())); }
}); commandHandler = new Component<>(() -> parent, "aggregateCommandHandler<" + aggregate.getSimpleName() + ">", c -> AggregateAnnotationCommandHandler.<A>builder() .repository(repository.get()) .commandTargetResolver(commandTargetResolver.get())
/** * Subscribe this command handler to the given {@code commandBus}. The command handler will be subscribed * for each of the supported commands. * * @param commandBus The command bus instance to subscribe to * @return A handle that can be used to unsubscribe */ public Registration subscribe(CommandBus commandBus) { List<Registration> subscriptions = supportedCommandNames() .stream() .map(supportedCommand -> commandBus.subscribe(supportedCommand, this)) .filter(Objects::nonNull).collect(Collectors.toList()); return () -> subscriptions.stream().map(Registration::cancel).reduce(Boolean::logicalOr).orElse(false); }
@Override public void start() { registrations.add(commandHandler.get().subscribe(parent.commandBus())); }
}); commandHandler = new Component<>(() -> parent, "aggregateCommandHandler<" + aggregate.getSimpleName() + ">", c -> AggregateAnnotationCommandHandler.<A>builder() .repository(repository.get()) .commandTargetResolver(commandTargetResolver.get())
/** * Subscribe this command handler to the given {@code commandBus}. The command handler will be subscribed * for each of the supported commands. * * @param commandBus The command bus instance to subscribe to * @return A handle that can be used to unsubscribe */ public Registration subscribe(CommandBus commandBus) { Collection<Registration> subscriptions = new ArrayList<>(); for (String supportedCommand : supportedCommandNames()) { Registration subscription = commandBus.subscribe(supportedCommand, this); if (subscription != null) { subscriptions.add(subscription); } } return () -> { subscriptions.forEach(Registration::cancel); return true; }; }
@SuppressWarnings("unchecked") @Before public void setUp() { eventStore = spy(EmbeddedEventStore.builder().storageEngine(new InMemoryEventStorageEngine()).build()); Repository<MyAggregate> myAggregateRepository = EventSourcingRepository.builder(MyAggregate.class) .eventStore(eventStore) .build(); CommandBus commandBus = SimpleCommandBus.builder().build(); commandGateway = DefaultCommandGateway.builder().commandBus(commandBus).build(); AggregateAnnotationCommandHandler<MyAggregate> myAggregateCommandHandler = AggregateAnnotationCommandHandler.<MyAggregate>builder() .aggregateType(MyAggregate.class) .repository(myAggregateRepository) .build(); myAggregateCommandHandler.subscribe(commandBus); }
/** * Initializes a {@link AggregateAnnotationCommandHandler} as specified through this Builder. * * @return a {@link AggregateAnnotationCommandHandler} as specified through this Builder */ public AggregateAnnotationCommandHandler<T> build() { return new AggregateAnnotationCommandHandler<>(this); }
/** * Instantiate a {@link AggregateAnnotationCommandHandler} based on the fields contained in the {@link Builder}. * <p> * Will assert that the {@link Repository} and {@link CommandTargetResolver} are not {@code null}, and will throw * an {@link AxonConfigurationException} if either of them is {@code null}. Next to that, the provided Builder's * goal is to create an {@link AggregateModel} (describing the structure of a given aggregate). To instantiate this * AggregateModel, either an {@link AggregateModel} can be provided directly or an {@code aggregateType} of type * {@link Class} can be used. The latter will internally resolve to an AggregateModel. Thus, either the * AggregateModel <b>or</b> the {@code aggregateType} should be provided. An AxonConfigurationException is thrown * if this criteria is not met. * * @param builder the {@link Builder} used to instantiate a {@link AggregateAnnotationCommandHandler} instance */ protected AggregateAnnotationCommandHandler(Builder<T> builder) { builder.validate(); this.repository = builder.repository; this.commandTargetResolver = builder.commandTargetResolver; this.supportedCommandNames = new HashSet<>(); this.handlers = initializeHandlers(builder.buildAggregateModel()); }