@CommandHandler public TestAggregate(Create cmd) { apply(cmd); }
/** * Apply a {@link org.axonframework.eventhandling.DomainEventMessage} with given payload and metadata (metadata * from interceptors will be combined with the provided metadata). Applying events means they are immediately * applied (published) to the aggregate and scheduled for publication to other event handlers. * <p/> * The event is applied on all entities part of this aggregate. If the event is applied from an event handler of the * aggregate and additional events need to be applied that depends on state changes brought about by the first event * use the returned {@link ApplyMore} instance. * * @param payload the payload of the event to apply * @param metaData any meta-data that must be registered with the Event * @return a gizmo to apply additional events after the given event has been processed by the entire aggregate * @see ApplyMore */ public static ApplyMore apply(Object payload, MetaData metaData) { return AggregateLifecycle.getInstance().doApply(payload, metaData); }
/** * Creates a new aggregate instance. In order for new aggregate to be created, a {@link Repository} should be * available to the current aggregate. {@link Repository} of an aggregate to be created is exposed to the current * aggregate via {@link RepositoryProvider}. * * @param <T> type of new aggregate to be created * @param aggregateType type of new aggregate to be created * @param factoryMethod factory method which creates new aggregate * @return a new aggregate instance * @throws Exception thrown if something goes wrong during instantiation of new aggregate */ public static <T> Aggregate<T> createNew(Class<T> aggregateType, Callable<T> factoryMethod) throws Exception { if (!isLive()) { throw new UnsupportedOperationException( "Aggregate is still initializing its state, creation of new aggregates is not possible"); } return getInstance().doCreateNew(aggregateType, factoryMethod); }
/** * Marks this aggregate as deleted, instructing a repository to remove that aggregate at an appropriate time. * <p/> * Note that different repository implementations may react differently to aggregates marked for deletion. * Typically, Event Sourced Repositories will ignore the marking and expect deletion to be provided as part of Event * information. */ public static void markDeleted() { getInstance().doMarkDeleted(); }
/** * Activates this lifecycle instance. Any invocations to static AggregateLifecycle methods will use this instance * until {@link #close()} is called. */ public void activate() { super.startScope(); this.registration = () -> super.endScope(); }
/** * Indicates whether this Aggregate instance is 'live'. Events applied to a 'live' Aggregate represent events that * are currently happening, as opposed to events representing historic decisions used to reconstruct the * Aggregate's state. * * @return {@code true} if the aggregate is 'live', {@code false} if the aggregate is initializing state based on * historic events */ public static boolean isLive() { return AggregateLifecycle.getInstance().getIsLive(); }
public void delete() { AggregateLifecycle.apply(new StubDomainEvent()); AggregateLifecycle.markDeleted(); } }
/** * Gets the version of the aggregate. * * @return the current version of the aggregate */ public static Long getVersion() { return getInstance().version(); }
/** * Executes the given task. While the task is being executed the current aggregate will be registered with the * current thread as the 'current' aggregate. * * @param task the task to execute on the aggregate */ protected void execute(Runnable task) { try { executeWithResult(() -> { task.run(); return null; }); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new AggregateInvocationException("Exception while invoking a task for an aggregate", e); } }
/** * Marks this aggregate as deleted, instructing a repository to remove that aggregate at an appropriate time. * <p/> * Note that different repository implementations may react differently to aggregates marked for deletion. * Typically, Event Sourced Repositories will ignore the marking and expect deletion to be provided as part of Event * information. */ public static void markDeleted() { getInstance().doMarkDeleted(); }
/** * Indicates whether this Aggregate instance is 'live'. Events applied to a 'live' Aggregate represent events that * are currently happening, as opposed to events representing historic decisions used to reconstruct the * Aggregate's state. * * @return {@code true} if the aggregate is 'live', {@code false} if the aggregate is initializing state based on * historic events */ public static boolean isLive() { return AggregateLifecycle.getInstance().getIsLive(); }
/** * Gets the version of the aggregate. * * @return the current version of the aggregate */ public static Long getVersion() { return getInstance().version(); }
/** * Executes the given task. While the task is being executed the current aggregate will be registered with the * current thread as the 'current' aggregate. * * @param task the task to execute on the aggregate */ protected void execute(Runnable task) { try { executeWithResult(() -> { task.run(); return null; }); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new AggregateInvocationException("Exception while invoking a task for an aggregate", e); } }
public void setMessage(String newMessage) { this.message = newMessage; AggregateLifecycle.apply(new StubDomainEvent(message)); }
/** * Creates a new aggregate instance. In order for new aggregate to be created, a {@link Repository} should be * available to the current aggregate. {@link Repository} of an aggregate to be created is exposed to the current * aggregate via {@link RepositoryProvider}. * * @param <T> type of new aggregate to be created * @param aggregateType type of new aggregate to be created * @param factoryMethod factory method which creates new aggregate * @return a new aggregate instance * @throws Exception thrown if something goes wrong during instantiation of new aggregate */ public static <T> Aggregate<T> createNew(Class<T> aggregateType, Callable<T> factoryMethod) throws Exception { if (!isLive()) { throw new UnsupportedOperationException( "Aggregate is still initializing its state, creation of new aggregates is not possible"); } return getInstance().doCreateNew(aggregateType, factoryMethod); }
/** * Apply a {@link org.axonframework.eventhandling.DomainEventMessage} with given payload without metadata (though * interceptors can also be used to provide metadata). Applying events means they are immediately applied * (published) to the aggregate and scheduled for publication to other event handlers. * <p/> * The event is applied on all entities part of this aggregate. If the event is applied from an event handler of the * aggregate and additional events need to be applied that depends on state changes brought about by the first event * use the returned {@link ApplyMore} instance. * * @param payload the payload of the event to apply * @return a gizmo to apply additional events after the given event has been processed by the entire aggregate * @see ApplyMore */ public static ApplyMore apply(Object payload) { return AggregateLifecycle.getInstance().doApply(payload, MetaData.emptyInstance()); }
public Message(String messageContents) { identifier = UUID.randomUUID(); apply(new MessageCreatedEvent(messageContents)); }
/** * Apply a {@link org.axonframework.eventhandling.DomainEventMessage} with given payload and metadata (metadata * from interceptors will be combined with the provided metadata). Applying events means they are immediately * applied (published) to the aggregate and scheduled for publication to other event handlers. * <p/> * The event is applied on all entities part of this aggregate. If the event is applied from an event handler of the * aggregate and additional events need to be applied that depends on state changes brought about by the first event * use the returned {@link ApplyMore} instance. * * @param payload the payload of the event to apply * @param metaData any meta-data that must be registered with the Event * @return a gizmo to apply additional events after the given event has been processed by the entire aggregate * @see ApplyMore */ public static ApplyMore apply(Object payload, MetaData metaData) { return AggregateLifecycle.getInstance().doApply(payload, metaData); }
@CommandHandler public void handle(Add cmd, CommandGateway gw) { apply(cmd); }
/** * Apply a {@link org.axonframework.eventhandling.DomainEventMessage} with given payload without metadata (though * interceptors can also be used to provide metadata). Applying events means they are immediately applied * (published) to the aggregate and scheduled for publication to other event handlers. * <p/> * The event is applied on all entities part of this aggregate. If the event is applied from an event handler of the * aggregate and additional events need to be applied that depends on state changes brought about by the first event * use the returned {@link ApplyMore} instance. * * @param payload the payload of the event to apply * @return a gizmo to apply additional events after the given event has been processed by the entire aggregate * @see ApplyMore */ public static ApplyMore apply(Object payload) { return AggregateLifecycle.getInstance().doApply(payload, MetaData.emptyInstance()); }