@Retention(value=RUNTIME) @Target(value=METHOD) @Documented @AcceptsFilters @AcceptsExternal public @interface Subscribe
Use it to subscribe to either events, business rejections, or entity state updates.
An event subscriber method:
Subscribe
;
void
;
EventMessage
as the first parameter;
EventContext
as the second parameter.
A rejection subscriber method:
Subscribe
;
void
;
RejectionMessage
as the first parameter;
CommandMessage
as the second parameter;
CommandContext
as the second or the third parameter.
Therefore, if the subscriber method specifies both the command message and
the command context, it must have the parameters exactly is that order, i.e.
(RejectionMessage, CommandMessage, CommandContext)
. Otherwise, an exception may be thrown
at runtime.
The type of the command argument, if specified, acts as a filter, i.e. the subscriber receives the rejection if:
An entity state subscriber method:
Subscribe
;
void
;
(entity)
option as the only
parameter.
If the annotation is applied to a method which doesn't satisfy either of these requirements, this method is not considered a subscriber and is not registered for the command output delivery.
Event subscriber methods are designed to be called by the framework only. Therefore, it is recommended to declare a them as package-private. It discourages a developer from calling these methods directly from anywhere.
Package-private access level still declares that an event reactor method is a part
of the Bounded Context-level API. See the BoundedContext
description on how the packages and Bounded Contexts relate.
When subscribing to events, field filtering is supported.
@Deprecated public abstract boolean external
@External
annotation for the first method parameter.true
, the annotated method receives an event generated from outside of the
Bounded Context to which the annotated method's class belongs.@Deprecated public abstract ByField filter
@Where
annotation for the first method parameter.If an event does not match this filter, it is not passed to the subscriber method.
If the @ByField.path
if empty, the filter is not
applied.