/** * Subscribe a handler for the given aggregate type to the given command bus. * * @param aggregateType The type of aggregate * @param repository The repository providing access to aggregate instances * @param commandBus The command bus to register command handlers to * @param <T> The type of aggregate this handler handles commands for * @return the Adapter created for the command handler target. Can be used to unsubscribe. */ public static <T extends AggregateRoot> AggregateAnnotationCommandHandler subscribe( Class<T> aggregateType, Repository<T> repository, CommandBus commandBus) { AggregateAnnotationCommandHandler adapter = new AggregateAnnotationCommandHandler<T>(aggregateType, repository, commandBus); adapter.subscribe(); return adapter; }
@Test public void testDisruptorCommandBusRepositoryNotAvailableOutsideOfInvokerThread() { DisruptorCommandBus commandBus = new DisruptorCommandBus(eventStore, eventBus); Repository<Aggregate> repository = commandBus.createRepository(new GenericAggregateFactory<Aggregate>(Aggregate.class)); AggregateAnnotationCommandHandler<Aggregate> handler = new AggregateAnnotationCommandHandler<Aggregate>(Aggregate.class, repository); AggregateAnnotationCommandHandler.subscribe(handler, commandBus); DefaultCommandGateway gateway = new DefaultCommandGateway(commandBus); // Create the aggregate String aggregateId = "" + System.currentTimeMillis(); gateway.sendAndWait(new CreateCommandAndEvent(aggregateId)); // Load the aggretate from the repository -- from "worker" thread UnitOfWork uow = DefaultUnitOfWork.startAndGet(); try { Aggregate aggregate = repository.load(aggregateId); fail("Expected IllegalStateException"); } catch (IllegalStateException e) { assertTrue(e.getMessage().contains("DisruptorCommandBus")); } finally { uow.rollback(); } }
@Bean public AggregateAnnotationCommandHandler<SalesChannel> salesChannelCommandHandler() { AggregateAnnotationCommandHandler<SalesChannel> commandHandler = AggregateAnnotationCommandHandler.subscribe( SalesChannel.class, salesChannelRepository(), commandBus()); return commandHandler; }
@Override public void afterPropertiesSet() throws Exception { if (parameterResolverFactory == null) { parameterResolverFactory = AnnotationConfiguration.readFor(aggregateType).getParameterResolverFactory(); } handler = new AggregateAnnotationCommandHandler<T>( aggregateType, repository, commandTargetResolver, parameterResolverFactory); for (String cmd : handler.supportedCommands()) { commandBus.subscribe(cmd, handler); } }