Annotation Interface Trigger
Injects the **triggering message** that caused the current message to be published or handled.
This annotation is typically used in:
@HandleResult
handlers to access the original request@HandleError
handlers to inspect the command or query that failed
It can be placed on:
- A handler method parameter: to inject the message or payload that triggered the current one
- A handler method itself: to restrict invocation to certain types of trigger messages
Injection Behavior
- The triggering message is injected if its structure matches the parameter type.
- Supported parameter types include:
- The triggering payload type (e.g.
MyCommand
) Message
DeserializingMessage
- The triggering payload type (e.g.
- If no match is found, the handler is skipped.
Filtering Options
You can restrict the handler’s applicability using:value()
: Only inject if the trigger’s payload type is assignable to one of the specified classesmessageType()
: Only allow triggers of the givenMessageType
consumer()
: Only match triggers from the specified publishing consumers
Example: Handling a result with access to the triggering command
@HandleResult
void handleResult(SuccessResponse result, @Trigger MyCommand originalCommand) {
log.info("Command {} completed with result: {}", originalCommand.getId(), result);
}
Example: Retrying failed commands using a consumer-specific trigger
@HandleError
@Trigger(consumer = "my-app", messageType = MessageType.COMMAND)
void retryFailedCommand(MyCommand failedCommand) {
FluxCapacitor.sendCommand(failedCommand);
}
Advanced Use Case: Building a dynamic dead-letter queue
Because trigger metadata is preserved, you can replay past failures even if no handler existed when the message originally failed. For example, after discovering a bug days later, you can deploy a consumer that:
- Replays failed commands from the error log
- Uses
@Trigger
to inject and reissue them - Recovers gracefully without needing manual inspection of logs
- See Also:
-
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionString[]
Restricts the trigger to messages sent by specific consumer(s).Restricts the trigger by itsMessageType
.Class<?>[]
Restricts the trigger message by payload type.
-
Element Details
-
value
Class<?>[] valueRestricts the trigger message by payload type. If left empty, any compatible payload will be injected.- Default:
{}
-
messageType
MessageType[] messageTypeRestricts the trigger by itsMessageType
.- Default:
{}
-
consumer
String[] consumerRestricts the trigger to messages sent by specific consumer(s).- Default:
{}
-