Annotation Interface Trigger


@Target({PARAMETER,METHOD}) @Retention(RUNTIME) @Documented public @interface Trigger
Injects the **triggering message** that caused the current message to be published or handled.

This annotation is typically used in:

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:
  • 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 classes
  • messageType(): Only allow triggers of the given MessageType
  • 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 Elements
    Modifier and Type
    Optional Element
    Description
    Restricts the trigger to messages sent by specific consumer(s).
    Restricts the trigger by its MessageType.
    Class<?>[]
    Restricts the trigger message by payload type.
  • Element Details

    • value

      Class<?>[] value
      Restricts the trigger message by payload type. If left empty, any compatible payload will be injected.
      Default:
      {}
    • messageType

      MessageType[] messageType
      Restricts the trigger by its MessageType.
      Default:
      {}
    • consumer

      String[] consumer
      Restricts the trigger to messages sent by specific consumer(s).
      Default:
      {}