Class InMemorySchemaRegistry

java.lang.Object
org.acemq.amqp.codec.avro.InMemorySchemaRegistry
All Implemented Interfaces:
SchemaRegistry

public final class InMemorySchemaRegistry extends Object implements SchemaRegistry
A registry held in this process's memory.

Format-neutral, and so no longer anything to do with Avro; it lives here only until the registry moves to a repository of its own. For tests, and for the case where every schema is known at start up and registered from resources on the classpath. Not for anything else, and the reason is not squeamishness: the identifiers it hands out are assigned in the order schemas happen to be registered, so two instances of the same service can disagree about what identifier three means, and a restart can make yesterday's messages unreadable. A registry shared between writers and readers, and durable across restarts, is the only kind that works in production.

Registering every schema up front, in a fixed order, on every instance, is what makes this safe to use for real. That is a discipline the class cannot enforce, so it is written down here instead.

  • Constructor Details

    • InMemorySchemaRegistry

      public InMemorySchemaRegistry()
  • Method Details

    • register

      public InMemorySchemaRegistry register(int id, SchemaDefinition schema)
      Registers a schema under an identifier of the caller's choosing.

      How this class is made deterministic: assign the numbers yourself, from configuration or a constant, and every instance agrees.

      Parameters:
      id - the identifier to use
      schema - the schema it stands for
      Returns:
      this registry, for chaining
    • idFor

      public int idFor(SchemaDefinition schema)
      Specified by:
      idFor in interface SchemaRegistry
      Parameters:
      schema - the schema a message is about to be written with
      Returns:
      its identifier, registering it if the registry has not seen it. Registering the same schema twice must return the same identifier
    • schemaFor

      public SchemaDefinition schemaFor(int id)
      Specified by:
      schemaFor in interface SchemaRegistry
      Parameters:
      id - an identifier read from a message
      Returns:
      the schema it stands for
    • size

      public int size()
      Returns:
      how many schemas are registered