@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();
    }
  }
  public static void main(String[] args) throws InterruptedException {
    CountingEventBus eventBus = new CountingEventBus();
    StubHandler stubHandler = new StubHandler();
    InMemoryEventStore inMemoryEventStore = new InMemoryEventStore();
    DisruptorCommandBus<StubAggregate> commandBus =
        new DisruptorCommandBus<StubAggregate>(
            new GenericAggregateFactory<StubAggregate>(StubAggregate.class),
            inMemoryEventStore,
            eventBus,
            new CommandTargetResolver() {
              @Override
              public VersionedAggregateIdentifier resolveTarget(CommandMessage<?> command) {
                return new VersionedAggregateIdentifier(
                    command.getMetaData().get(TARGET_AGGREGATE_PROPERTY), null);
              }
            });
    commandBus.subscribe(StubCommand.class, stubHandler);
    stubHandler.setRepository(commandBus);
    final String aggregateIdentifier = "MyID";
    inMemoryEventStore.appendEvents(
        StubAggregate.class.getSimpleName(),
        new SimpleDomainEventStream(
            new GenericDomainEventMessage<StubDomainEvent>(
                aggregateIdentifier, 0, new StubDomainEvent())));

    long start = System.currentTimeMillis();
    for (int i = 0; i < COMMAND_COUNT; i++) {
      CommandMessage<StubCommand> command =
          new GenericCommandMessage<StubCommand>(
              new StubCommand(aggregateIdentifier),
              Collections.singletonMap(TARGET_AGGREGATE_PROPERTY, (Object) aggregateIdentifier));
      commandBus.dispatch(command);
    }
    System.out.println("Finished dispatching!");

    inMemoryEventStore.countDownLatch.await(5, TimeUnit.SECONDS);
    long end = System.currentTimeMillis();
    try {
      assertEquals(
          "Seems that some events are not published", 0, eventBus.publisherCountDown.getCount());
      assertEquals(
          "Seems that some events are not stored", 0, inMemoryEventStore.countDownLatch.getCount());
      System.out.println(
          "Did " + ((COMMAND_COUNT * 1000L) / (end - start)) + " commands per second");
    } finally {
      commandBus.stop();
    }
  }