@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(); } }
@Test public void testCreateOrder() throws InterruptedException { OrderId orderId = new OrderId(); commandGateway.send(new CreateOrderCommand(orderId, "TestOrder")); UnitOfWork unitOfWork = unitOfWorkProvider.get(); Assert.assertEquals( "Order is not created", orderRepository.load(orderId).getIdentifier(), orderId); unitOfWork.commit(); ItemId itemId1 = new ItemId(); commandGateway.send(new AddOrderItemCommand(orderId, itemId1, 10)); final AtomicBoolean injectionSuccessful = new AtomicBoolean(); commandGateway.send( new RemoveOrderItemCommand(orderId, itemId1), new CommandCallback<Boolean>() { @Override public void onSuccess(Boolean result) { injectionSuccessful.set(true); } @Override public void onFailure(Throwable cause) { logger.error("Error", cause); } }); Assert.assertTrue( "Injection into @CommandHandler method is not successful", injectionSuccessful.get()); }
@SuppressWarnings("unchecked") @Override public void run() { EventMessage<?> eventMessage = createMessage(); if (logger.isInfoEnabled()) { logger.info( "Triggered the publication of event [{}]", eventMessage.getPayloadType().getSimpleName()); } UnitOfWork unitOfWork = unitOfWorkFactory.createUnitOfWork(); try { unitOfWork.publishEvent(eventMessage, eventBus); unitOfWork.commit(); } finally { tokens.remove(tokenId); } }
@Override public Object handle(CommandMessage<?> command, UnitOfWork unitOfWork, InterceptorChain chain) throws Throwable { AuditingUnitOfWorkListener auditListener = new AuditingUnitOfWorkListener(command, auditDataProvider, auditLogger); unitOfWork.registerListener(auditListener); Object returnValue = chain.proceed(); auditListener.setReturnValue(returnValue); return returnValue; }
@After public void tearDown() { if (unitOfWork.isStarted()) { unitOfWork.rollback(); } }