Class InMemorySchemaRegistry
- All Implemented Interfaces:
SchemaRegistry
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintidFor(SchemaDefinition schema) register(int id, SchemaDefinition schema) Registers a schema under an identifier of the caller's choosing.schemaFor(int id) intsize()
-
Constructor Details
-
InMemorySchemaRegistry
public InMemorySchemaRegistry()
-
-
Method Details
-
register
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 useschema- the schema it stands for- Returns:
- this registry, for chaining
-
idFor
- Specified by:
idForin interfaceSchemaRegistry- 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
- Specified by:
schemaForin interfaceSchemaRegistry- Parameters:
id- an identifier read from a message- Returns:
- the schema it stands for
-
size
public int size()- Returns:
- how many schemas are registered
-