Class CosIdIdentifierFactory

java.lang.Object
org.axonframework.common.IdentifierFactory
me.ahoo.cosid.axon.CosIdIdentifierFactory

public class CosIdIdentifierFactory extends org.axonframework.common.IdentifierFactory
Axon's IdentifierFactory on a runtime generator (07-runtime-and-spring.md ยง5).

The one static state of CosId. Axon loads its identifier factory through ServiceLoader (this module's META-INF/services entry) into a JVM-wide static, so the factory it creates cannot be given a runtime. It uses the runtime bound most recently and not yet unbound instead: the Spring Boot auto-configuration binds the context's runtime once it is started and unbinds it before it is closed; other code binds with try (var binding = CosIdIdentifierFactory.bind(runtime, null)) { ... }. Closing one of several runtimes unbinds only its own binding. With nothing bound, generating an identifier throws an IllegalStateException: having this module means Axon's identifiers come from CosId (as in 4.x).

The generator is the one a binding names, else the one of the ID_KEY system property, else default. A factory created with a runtime ignores the bindings.

  • Field Details

    • ID_KEY

      public static final String ID_KEY
      The system property of the generator name.
      See Also:
    • DEFAULT_GENERATOR

      public static final String DEFAULT_GENERATOR
      The generator when neither a binding nor the system property names one.
      See Also:
  • Constructor Details

    • CosIdIdentifierFactory

      public CosIdIdentifierFactory()
      The factory ServiceLoader creates: the ids of the bound runtime, failing without one.
    • CosIdIdentifierFactory

      public CosIdIdentifierFactory(me.ahoo.cosid.runtime.CosIdRuntime runtime, String generator)
      A factory of one runtime, regardless of the bindings.
      Parameters:
      runtime - the runtime
      generator - the generator name; blank or null for the ID_KEY system property's
  • Method Details

    • bind

      public static CosIdIdentifierFactory.Binding bind(me.ahoo.cosid.runtime.CosIdRuntime runtime, String generator)
      Binds a runtime for the factory ServiceLoader creates; the most recent binding wins.
      Parameters:
      runtime - the runtime
      generator - the generator name; blank or null for the ID_KEY system property's
      Returns:
      the binding; close it to unbind
    • unbind

      public static boolean unbind(CosIdIdentifierFactory.Binding binding)
      Removes exactly this binding, leaving the others in place.
      Parameters:
      binding - the binding
      Returns:
      true if it was bound, false if it was already unbound
    • generateIdentifier

      public String generateIdentifier()
      Specified by:
      generateIdentifier in class org.axonframework.common.IdentifierFactory