Class JdbcSchemaRegistry
- All Implemented Interfaces:
SchemaRegistry
The in-memory one is fine for a test and useless for anything else: identifiers must be stable forever, because a message published today may be read next year by a consumer looking up the schema it was written with. A registry that hands out fresh identifiers on restart makes every message written before the restart unreadable, and does it silently.
This is the smallest thing that fixes that. It is not Confluent's registry and does not try to be: there is no compatibility checking, no versioning UI, no REST API. What it does is remember which integer stands for which schema, across restarts and across instances, which is the part the wire format depends on.
JdbcSchemaRegistry registry = new JdbcSchemaRegistry(dataSource);
registry.createSchemaIfAbsent();
mq.publisher("orders", "order.placed", GenericRecord.class)
.as(AvroCodec.registered(registry))
.send(order);
Both directions are cached in memory and never invalidated, because neither answer can change: an identifier stands for one schema forever, and a schema keeps the identifier it was given. Without the cache this would be a database round trip per message, which is not a registry anyone would keep.
-
Constructor Summary
ConstructorsConstructorDescriptionJdbcSchemaRegistry(DataSource dataSource) JdbcSchemaRegistry(DataSource dataSource, String table) -
Method Summary
Modifier and TypeMethodDescriptionvoidCreates the table if it is not already there.intidFor(SchemaDefinition schema) schemaFor(int id) intsize()toString()
-
Constructor Details
-
JdbcSchemaRegistry
- Parameters:
dataSource- where the table lives
-
JdbcSchemaRegistry
- Parameters:
dataSource- where the table livestable- table name; must be a plain SQL identifier
-
-
Method Details
-
createSchemaIfAbsent
public void createSchemaIfAbsent()Creates the table if it is not already there.For development and tests. In production the table belongs in the migration tool that owns the rest of the schema.
- Throws:
AceMqException- if the schema cannot be created
-
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 this registry holds
-
toString
-