Interface HasMessage

All Superinterfaces:
HasMetadata
All Known Implementing Classes:
DefaultEntityHelper.DeserializingMessageWithEntity, DefaultEntityHelper.MessageWithEntity, DeserializingMessage, Message, Schedule, WebRequest, WebResponse

public interface HasMessage extends HasMetadata
Interface for objects that expose a backing Message instance.

This abstraction allows access to common message properties (payload, metadata, timestamp, ID, routing key) without requiring the caller to know if they're working with a Message, Schedule, or a wrapped domain object.

Implementations must return a consistent toMessage() representation and may override routing logic using annotations such as RoutingKey.

Responsibilities

See Also:
  • Field Details

  • Method Details

    • toMessage

      Message toMessage()
      Returns the underlying Message representation of this object.
      Returns:
      the Message backing this instance
    • getPayload

      default <R> R getPayload()
      Retrieves the message payload, deserializing if necessary, cast to the expected type.

      By default, this delegates to toMessage().getPayload().

      Type Parameters:
      R - the expected payload type
      Returns:
      the deserialized payload
    • getPayloadAs

      default <R> R getPayloadAs(Type type)
      Retrieves the message payload, deserializing if necessary and optionally converted to the given type.

      By default, this performs a conversion of the payload using JsonUtils.

      Type Parameters:
      R - the expected payload type
      Returns:
      the payload converted to the given type
    • getPayloadClass

      default Class<?> getPayloadClass()
      Returns the runtime class of the payload object, or Void.class if the payload is null.
      Returns:
      the payload's class
    • getMessageId

      default String getMessageId()
      Returns the unique ID of the underlying message.
      Returns:
      the message ID
    • getTimestamp

      default Instant getTimestamp()
      Returns the timestamp at which the message was created or published.
      Returns:
      the message timestamp
    • computeRoutingKey

      default Optional<String> computeRoutingKey()
      Computes the default routing key for this message.

      The key is extracted based on the RoutingKey annotation on the payload type or one of its properties. If none is found, and the message is a Schedule, the schedule ID is used instead.

      Returns:
      the resolved routing key, if available
    • getRoutingKey

      default Optional<String> getRoutingKey(String propertyName)
      Attempts to resolve the routing key from the given property name.

      This checks metadata first, then the payload itself. Logs a warning the first time a property is missing for a given payload class.

      Parameters:
      propertyName - the name of the field, method, or metadata key to use
      Returns:
      the routing key if found
    • getRoutingKey

      default Optional<String> getRoutingKey(String propertyName, boolean warnIfMissing)
      Attempts to resolve the routing key from the specified property name.

      Optionally logs a warning if the key is missing, but only once per class-property combination.

      Parameters:
      propertyName - the property to use for routing
      warnIfMissing - whether to log a warning on first failure
      Returns:
      the routing key if resolved