Interface TenantComponentProvider<T>

Type Parameters:
T - the type of component provided per tenant
All Superinterfaces:
DescribableComponent, MultiTenantAwareComponent, TenantDescriptors

public interface TenantComponentProvider<T> extends MultiTenantAwareComponent, TenantDescriptors
Provides the per-tenant instance of a single application component of type T.

A provider holds one instance of T per tenant, so message handlers always receive the resource belonging to the tenant of the message being handled (for example a per-tenant SQL datasource or repository). Not to be confused with the TenantProvider, which provisions the tenants themselves: a TenantComponentProvider provides an application component scoped to each of those tenants.

A single provider is generic over one component type. To expose several tenant-scoped component types, register a separate provider per type. Each provider is matched to handler parameters by its component type.

The default provider, obtained through withFactory(Class, TenantComponentFactory), creates instances lazily through a TenantComponentFactory on first access for a tenant, and releases them again when that tenant is unregistered.

As a MultiTenantAwareComponent the provider participates in tenant lifecycle management. Registering a tenant makes it eligible for component instances and unregistering it releases them.

Since:
5.3.0
Author:
Theo Emanuelsson, Jan Galinski, Laura Devriendt
See Also:
  • Method Details

    • withFactory

      static <T> TenantComponentProvider<T> withFactory(Class<T> componentType, TenantComponentFactory<T> factory)
      Creates a TenantComponentProvider for the given componentType, building and destroying the per-tenant instances through the given factory.

      The returned provider creates each tenant's instance lazily on first access and caches it. It only serves registered tenants: requesting the component of an unknown tenant is rejected with a TenantNotResolvedException, so no tenant-scoped resource is ever built for a tenant the application does not know. Unregistering a tenant removes and destroys its cached instance.

      Component creation runs inside the provider's cache update. The given factory's create method must therefore not register or unregister tenants on, nor request components from, the returned provider on the creating thread.

      Type Parameters:
      T - the type of component provided per tenant
      Parameters:
      componentType - the type of component provided per tenant, used to match against handler parameters, must not be null
      factory - the factory building and destroying the per-tenant instances, must not be null
      Returns:
      a provider serving one lazily created instance of componentType per registered tenant
      Throws:
      NullPointerException - if the given componentType or factory is null
    • componentFor

      T componentFor(TenantDescriptor tenant)
      Returns the component instance for the given tenant.

      Implementations may reject tenants they do not serve with a TenantNotResolvedException. The provider returned by withFactory(Class, TenantComponentFactory) only serves registered tenants and creates each tenant's instance lazily on first access.

      Parameters:
      tenant - the tenant to provide the component instance for
      Returns:
      the component instance belonging to the given tenant
      Throws:
      TenantNotResolvedException - if the given tenant is not served by this provider
    • componentType

      Class<T> componentType()
      Returns the type of component provided per tenant.

      Used to match this provider against message-handler parameters during parameter resolution. Matching is based on the raw type: generic type parameters of the component type are not distinguished, so components differing only in their generics need distinct wrapper types.

      Returns:
      the component type provided per tenant