6  Part V — Replay the dynamic idea across processes

Steps 35 to 38 take the subscription idea out of the single JVM. The services become separate programs again, but instead of mediators copying files, each service watches the mailbox it was told to watch. One assumption of the first filesystem helper does not survive the move, and fixing it is what this part is really about.

6.1 One blocking watch is not enough

FileMessageBoard.watch(topic, receiver) registers one directory and blocks forever. That was enough while every process watched one fixed mailbox. A dynamically wired service needs more:

always watch its own control inbox
AND
add a watched directory at runtime
AND
possibly remove it later

The old signature cannot express that, and the consequence was already visible earlier: the configurable mediator accepted exactly one ROUTE, because the moment it began watching its source it went deaf to its own control inbox. The limitation was never whose directory — it was one, fixed, forever.

6.2 The helper: FileSubscriptions

One WatchService can serve many directories. register(...) returns a WatchKey identifying one of them, watcher.take() returns whichever key has events, and key.cancel() deregisters it. The helper keeps both directions, name → key and key → name, so an event can be traced back to the mailbox it came from.

Events do not reach a callback directly. They go onto a queue, read with take(), carrying

record IncomingMessage(String mailbox, Message message) {}

Two reasons, and order of arrival is only the third. First, the observing thread must stay responsive for every subscribed directory: a callback that transformed, published or blocked would stall the observation of all the others. Second, the mailbox name travels with the message, so one loop in the service can decide what a message means — control inbox, so this is Intent to act on; subscribed source, so this is data to transform — and that same loop is free to add or remove subscriptions, because it is not sitting inside a callback.

6.3 Two publications instead of four

Run SensorService, DynamicProcessorService and DynamicActuatorService — and no mediators — then let Configure.java publish the two TOPIC messages. A line typed afterwards leaves exactly two application publications in the board:

mailbox content what it is
consoleIn/outbox TEXT hello the input
rot13/outbox TEXT uryyb the result
rot13/inbox TOPIC consoleIn/outbox Intent, published once
consoleOut/inbox TOPIC rot13/outbox Intent, published once

consoleOut/inbox holds no payload at all: ConsoleOut takes straight from rot13/outbox. Its inbox changed job, from carrying messages to carrying wiring.

A line typed before Configure.java ran stays in consoleIn/outbox forever. A subscription that does not exist yet misses what came before it — exactly what the restarted mediator showed when it delivered nothing.

version application publications processes in the data path
mediated (step 18) 4 5
configurable mediator (step 27) 4 5
dynamic (step 38) 2 3