Class ExpirationTaskBuilder

java.lang.Object
org.keycloak.expiration.jpa.ExpirationTaskBuilder

public final class ExpirationTaskBuilder extends Object
A builder for creating ExpirationTask instances.

Required properties: withFactory(KeycloakSessionFactory), withAction(ExpirationAction), withEntityId(String), withExecutor(Executor), and withInterval(int, TimeUnit).

Optional properties: withListener(ExpirationListener), withMetrics(boolean), withRealmExpiration(boolean), withMaxRemoval(int) (defaults to 128), and withTimeout(int, TimeUnit) (defaults to the interval).

Example usage:


 ExpirationTask.builder()
     .withFactory(factory)
     .withEntityId("authentication-sessions")
     .withInterval(600, TimeUnit.SECONDS)
     .withTimeout(300, TimeUnit.SECONDS)
     .withAction(myAction)
     .withExecutor(executor)
     .withMetrics(true)
     .withRealmExpiration(true)
     .withMaxRemoval(128)
     .build()
     .schedule();
 
See Also:
  • Method Details

    • withAction

      public ExpirationTaskBuilder withAction(ExpirationAction action)
      Sets the ExpirationAction that performs the actual deletion of expired entries.
    • withListener

      public ExpirationTaskBuilder withListener(ExpirationListener listener)
      Sets an optional ExpirationListener to be notified after each task run. When metrics are also enabled, both the metrics listener and this listener are notified.
    • withExecutor

      public ExpirationTaskBuilder withExecutor(Executor executor)
      Sets the Executor used to run the cleanup work off the timer thread.
    • withEntityId

      public ExpirationTaskBuilder withEntityId(String entityId)
      Sets a unique identifier for the entity type being expired (e.g. "authentication-sessions"). Used in metric tags, log messages, and as the key prefix for distributed coordination via ServerConfigStorageProvider.
    • withMetrics

      public ExpirationTaskBuilder withMetrics(boolean metrics)
      Enables or disables Micrometer metrics for this expiration task. When enabled, a keycloak.expiration timer and a keycloak.expiration.removals distribution summary are registered with tags for the entity type and outcome.
    • withRealmExpiration

      public ExpirationTaskBuilder withRealmExpiration(boolean realmExpiration)
      Enables per-realm expiration. When true, the task iterates over all realms and invokes the ExpirationAction once per realm. When false, the action is invoked once with a null realm ID.
    • withMaxRemoval

      public ExpirationTaskBuilder withMaxRemoval(int maxRemoval)
      Sets the maximum number of entries to remove per batch. Defaults to 128.
      Throws:
      org.keycloak.models.ModelException - if the value is not positive.
    • withInterval

      public ExpirationTaskBuilder withInterval(int interval, TimeUnit timeUnit)
      Sets the interval between expiration task runs.
      Throws:
      ArithmeticException - if the converted value overflows an int.
    • withTimeout

      public ExpirationTaskBuilder withTimeout(int timeout, TimeUnit timeUnit)
      Sets the transaction timeout for each expiration task run. Defaults to the interval if not set.
      Throws:
      ArithmeticException - if the converted value overflows an int.
    • withFactory

      public ExpirationTaskBuilder withFactory(org.keycloak.models.KeycloakSessionFactory factory)
      Sets the KeycloakSessionFactory used to create sessions for each transaction.
    • build

      public ExpirationTask build()
      Builds the ExpirationTask.
      Throws:
      NullPointerException - if any required property is not set.
      org.keycloak.models.ModelException - if the interval is not set or is not positive.