JdbcProjection connects an event-stream source to a JDBC-accessible database. It reads tagged events, passes them to a handler, and writes the resulting read model through a repository and database session. section 10.3.3 section 15.1.1
A practical projection assembles these parts:
- An EventSourcedProvider.eventsByTag source selects events from the journal through a JDBC read-journal plugin. section 10.3.3 section 15.1.1
- A handler interprets each event and invokes repository operations with the supplied database session. section 15.1.1
- A session factory supplies ScalikeJdbcSession instances, while a projection identifier distinguishes each tagged projection. section 15.1.1
- Tags enable parallel projections; separate instances can run on different nodes to scale out and isolate failures. section 10.3.3
Bet projection
A betting projection selects Bet.Event entries by tag. Its handler persists only Bet.Opened events by calling addBet with the event fields and session; other events are ignored. JdbcProjection.exactlyOnce combines the source provider, handler, repository-backed database writes, and session factory into one projection. section 15.1.1 section 15.1.1
Listing 10.8 CargosPerContainerProjection implementation
object CargosPerContainerProjection {
def createProjectionFor( #1
system: ActorSystem[_],
repository: CargosPerContainerRepository,
indexTag: Int): ExactlyOnceProjection[
Offset,
EventEnvelope[SPContainer.Event]] = {
val tag = "container-tag-" + indexTag #2
val sourceProvider = #3
EventSourcedProvider.eventsByTag[SPContainer.Event](
system = system,
readJournalPluginId = JdbcReadJournal.Identifier,
tag = tag)
JdbcProjection.exactlyOnce( #4
projectionId =
ProjectionId("CargosPerContainerProjection", tag),
sourceProvider = sourceProvider,
handler =
() => new CPCProjectionHandler(repository),
sessionFactory = () => new ScalikeJdbcSession())(system)
}
}
The implementation shows the complete construction pattern: derive a tag, create an events-by-tag source, and pass it to JdbcProjection.exactlyOnce with a handler and session factory. section 10.3.3
