Record Class MetadataBasedTenantResolver

java.lang.Object
java.lang.Record
io.axoniq.framework.messaging.multitenancy.api.MetadataBasedTenantResolver
Record Components:
metadataKey - the key to use when extracting the tenant identifier from message metadata, default is "tenantId"
All Implemented Interfaces:
TenantResolver

public record MetadataBasedTenantResolver(String metadataKey) extends Record implements TenantResolver
A TenantResolver implementation that resolves the target tenant from, and attaches it back to, message metadata.

This resolver extracts the tenant identifier from the message's metadata using a configurable key (default: "tenantId"). If the metadata does not contain the expected key, a TenantNotResolvedException is thrown.

The same key is used in both directions: attachTenant(Message, TenantDescriptor) writes the tenant identifier under this instance's own metadataKey, so a message this resolver attached a tenant to always resolves back to that same tenant through this same instance. This is what keeps the tenant of a message being handled attached to a command or query dispatched from within that handler, without any further configuration.

Example usage:


     // Using default metadata key "tenantId"
     TenantResolver resolver = new MetadataBasedTenantResolver();

     // Using custom metadata key
     TenantResolver resolver = new MetadataBasedTenantResolver("customTenantKey");
 
Since:
5.3.0
Author:
Theo Emanuelsson, Jan Galinski
See Also:
  • Field Details

    • DEFAULT_TENANT_METADATA_KEY

      public static final String DEFAULT_TENANT_METADATA_KEY
      The default metadata key used to store the tenant identifier.
      See Also:
  • Constructor Details

    • MetadataBasedTenantResolver

      public MetadataBasedTenantResolver()
      Constructs a MetadataBasedTenantResolver using the default metadata key "tenantId".
    • MetadataBasedTenantResolver

      public MetadataBasedTenantResolver(String metadataKey)
      Constructs a MetadataBasedTenantResolver using the specified metadata key.
      Parameters:
      metadataKey - the key to use when extracting the tenant identifier from message metadata, must not be null or empty
  • Method Details

    • resolveTenant

      public TenantDescriptor resolveTenant(Message message, Collection<TenantDescriptor> tenants)
      Resolves the target tenant by extracting the tenant identifier from the message's metadata.
      Specified by:
      resolveTenant in interface TenantResolver
      Parameters:
      message - the message to resolve the tenant from
      tenants - the available tenants
      Returns:
      the TenantDescriptor for the resolved tenant
      Throws:
      TenantNotResolvedException - if the message metadata does not contain the expected tenant key
    • attachTenant

      public Message attachTenant(Message message, TenantDescriptor tenant)
      Attaches the given tenant to the given message by writing its identifier into the message's metadata under this instance's metadataKey, merged with any metadata the message already carries.
      Specified by:
      attachTenant in interface TenantResolver
      Parameters:
      message - the message to attach the given tenant to
      tenant - the tenant to attach to the given message
      Returns:
      a copy of the given message carrying the given tenant under metadataKey
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • metadataKey

      public String metadataKey()
      Returns the value of the metadataKey record component.
      Returns:
      the value of the metadataKey record component