@Override public <R> R invoke(Function<T, R> invocation) { try { return executeWithResult(() -> invocation.apply(aggregateRoot)); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new AggregateInvocationException("Exception occurred while invoking an aggregate", e); } }
/** * Registers the aggregate root created by the given {@code aggregateFactory} with this aggregate. Applies any * delayed events that have not been applied to the aggregate yet. * <p> * This is method is commonly called while an aggregate is being initialized. * * @param aggregateFactory the factory to create the aggregate root * @throws Exception if the aggregate factory fails to create the aggregate root */ protected void registerRoot(Callable<T> aggregateFactory) throws Exception { this.aggregateRoot = executeWithResult(aggregateFactory); execute(() -> { applying = true; while (!delayedTasks.isEmpty()) { delayedTasks.remove().run(); } applying = false; }); }
@SuppressWarnings("unchecked") @Override public Object handle(Message<?> message) throws Exception { Callable<Object> messageHandling; if (message instanceof CommandMessage) { messageHandling = () -> handle((CommandMessage) message); } else if (message instanceof EventMessage) { messageHandling = () -> handle((EventMessage) message); } else { throw new IllegalArgumentException("Unsupported message type: " + message.getClass()); } return executeWithResult(messageHandling); }
@Override public <R> R invoke(Function<T, R> invocation) { try { return executeWithResult(() -> invocation.apply(aggregateRoot)); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new AggregateInvocationException("Exception occurred while invoking an aggregate", e); } }
/** * Registers the aggregate root created by the given {@code aggregateFactory} with this aggregate. Applies any * delayed events that have not been applied to the aggregate yet. * <p> * This is method is commonly called while an aggregate is being initialized. * * @param aggregateFactory the factory to create the aggregate root * @throws Exception if the aggregate factory fails to create the aggregate root */ protected void registerRoot(Callable<T> aggregateFactory) throws Exception { this.aggregateRoot = executeWithResult(aggregateFactory); execute(() -> { applying = true; while (!delayedTasks.isEmpty()) { delayedTasks.remove().run(); } applying = false; }); }
@SuppressWarnings("unchecked") @Override public Object handle(Message<?> message) throws Exception { Callable<Object> messageHandling; if (message instanceof CommandMessage) { messageHandling = () -> handle((CommandMessage) message); } else if (message instanceof EventMessage) { messageHandling = () -> handle((EventMessage) message); } else { throw new IllegalArgumentException("Unsupported message type: " + message.getClass()); } return executeWithResult(messageHandling); }