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 Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe default metadata key used to store the tenant identifier. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs aMetadataBasedTenantResolverusing the default metadata key"tenantId".MetadataBasedTenantResolver(String metadataKey) Constructs aMetadataBasedTenantResolverusing the specified metadata key. -
Method Summary
Modifier and TypeMethodDescriptionattachTenant(Message message, TenantDescriptor tenant) Attaches the giventenantto the givenmessageby writing its identifier into the message's metadata under this instance'smetadataKey, merged with any metadata the message already carries.final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.Returns the value of themetadataKeyrecord component.resolveTenant(Message message, Collection<TenantDescriptor> tenants) Resolves the target tenant by extracting the tenant identifier from the message's metadata.final StringtoString()Returns a string representation of this record class.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface io.axoniq.framework.messaging.multitenancy.api.TenantResolver
resolveTenant
-
Field Details
-
DEFAULT_TENANT_METADATA_KEY
The default metadata key used to store the tenant identifier.- See Also:
-
-
Constructor Details
-
MetadataBasedTenantResolver
public MetadataBasedTenantResolver()Constructs aMetadataBasedTenantResolverusing the default metadata key"tenantId". -
MetadataBasedTenantResolver
Constructs aMetadataBasedTenantResolverusing the specified metadata key.- Parameters:
metadataKey- the key to use when extracting the tenant identifier from message metadata, must not benullor empty
-
-
Method Details
-
resolveTenant
Resolves the target tenant by extracting the tenant identifier from the message's metadata.- Specified by:
resolveTenantin interfaceTenantResolver- Parameters:
message- the message to resolve the tenant fromtenants- the available tenants- Returns:
- the
TenantDescriptorfor the resolved tenant - Throws:
TenantNotResolvedException- if the message metadata does not contain the expected tenant key
-
attachTenant
Attaches the giventenantto the givenmessageby writing its identifier into the message's metadata under this instance'smetadataKey, merged with any metadata the message already carries.- Specified by:
attachTenantin interfaceTenantResolver- Parameters:
message- the message to attach the giventenanttotenant- the tenant to attach to the givenmessage- Returns:
- a copy of the given
messagecarrying the giventenantundermetadataKey
-
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. -
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. -
equals
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 withObjects::equals(Object,Object). -
metadataKey
Returns the value of themetadataKeyrecord component.- Returns:
- the value of the
metadataKeyrecord component
-