Workbench / The TiffinBox Build
S47 · V3·46
Wired, Not Reached For
Volume 3, chapter 46. TiffinBox priced every order in a time zone that was a static constant on Server, which Pages also read directly: no test could price in another zone and no deployment could choose one. Its settings came from positional arguments only, so a port typo was a NumberFormatException and exit code 1, and `--zone=Europe/London` was taken as the web directory. This stage makes the zone a dependency of a Pricer built in Main's composition root, gives the server a Parts record instead of telescoping constructors, and adds Config: defaults, a properties file, environment variables and the command line, in that order, each value with its origin, all problems reported at once and exit 78. Zones go through ZoneId.of, because the JDK's older TimeZone turns a typo into GMT. Stage 44's input trace caught the new test reading the rules file.
Stage 47: wired, not reached for
The brief
What you'll build.
Pricer: the rules and the business's zone;price(order, firstOrder).ServerandPagesprice through it;Server.BUSINESS_ZONEis gone.Server.Parts: a record of everything a server is made from (port, log, menu, pricer, clock, limits, web root, access log), and aServer(Parts)constructor; the old constructors delegate to it.Config:data,port,rules,web,zone,idle,head-deadline,max-connections,access-log-capacity, from defaults,DATA/tiffinbox.properties,TIFFINBOX_*environment variables and the command line (positionalDIR PORT RULES [WEB]as before, plus--key=value), later sources winning. Typed parsing (ZoneId.of, durations with a unit), every problem at once, exit 78 (EX_CONFIG). Unknown options and unknown file keys are errors.tiffinbox config: prints the effective settings and where each came from.- The composition root:
Main.serve(Config)builds the access log, the pricer and the server's parts from the settings. - Tests:
ConfigTest(precedence and origins, all problems at once, pricing in two zones);PagesTestusesPricer.DEFAULT_ZONE. - Measurements in
exp/47:Zones.java(zones.out),config-before.out,config-after.out,trace-caught.out.
Why a real company needs this. A second city, a second customer, a staging environment: each needs different settings, and each wrong setting found at startup is an incident avoided. Settings that are constants can't be changed without a release; settings with no origin can't be debugged.
Why now. Chapter 46 is Spring Core: inversion of control, the container, lifecycle, configuration and environment, proxies. Stage 35 left "Server is still large" and stage 38 "BUSINESS_ZONE is a constant" for this stage.
What you'll have at the end.
- prices that depend on a configured zone, tested in two;
- settings with origins, refused all at once when wrong;
- a composition root that a test can imitate;
- ADR-0049: how TiffinBox is configured and wired.
Theory to own
- Composition root. Where are objects created and connected, and what may other code never do? (Companion, Gaps 2 and Theory 1; the chapter, "IoC and DI".)
- Property sources. In what order are they read, and what happens to a value lower down? (The chapter, "Environment, profiles and property sources"; Companion, Gaps 3 and Code walkthrough.)
- Typed configuration. Why parse
zoneas aZoneIdand refuseidle=5? (Companion, Gaps 1 and Theory 2.) - Zones and offsets. Why configure
Europe/Londonrather than+00:00? (Companion, Gaps 4 and Theory 4.)
Build it
Step 1: what the constant costs
Write exp/47/Zones.java: the two JDK APIs on four zone strings, one order priced at two instants in three zones with rules/november.rules, and the length of business days in three zones around their transitions.
Checkpoint. exp/47/zones.out.
Step 2: the pricer and the parts
Write Pricer, replace Server.BUSINESS_ZONE and every Interpreter.price call in Server and Pages, and add Server.Parts and Server(Parts).
Checkpoint. golden master: 47 exchanges, identical; aServerPricesInItsConfiguredZone.
Step 3: settings with origins
Write Config and ConfigTest, the config command, and Main.config with exit 78. Compare serve with a bad port and an unknown option before and after.
Checkpoint. exp/47/config-before.out, config-after.out.
Step 4: the build notices
Run the release check.
Checkpoint. exp/47/trace-caught.out; the release check passes after rules/november.rules is added to the server's EXTRA inputs in tools/build.sh.
Senior and architect lens
1. A dependency is anything that can differ. Between deployments, between tests, over time. The zone qualified on all three; it was a constant because TiffinBox had one kitchen city.
2. ADR-0049: how TiffinBox is configured and wired. Write it with these points:
- Wiring: one composition root (
Main.serve), constructor injection,Server.Partsfor the server's dependencies; no static access to settings. - Sources: defaults in code,
DATA/tiffinbox.properties,TIFFINBOX_*, command line, in that order; each value keeps its origin, andtiffinbox configshows them. - Validation: typed parsing (
ZoneId, durations with units, ranges), unknown keys and options refused, every problem at once, exit 78. - Time: the business zone is a region, configured per deployment; per kitchen when there's more than one city.
- No container yet: about twenty objects; a container would add startup cost and reflection for no new capability (the chapter's lab measures the cost; chapter 47's Spring Boot is where its services pay off).
3. Make the wrong value impossible to miss. Parsing to a type that refuses nonsense, collecting every problem, and printing origins all move mistakes from 3 am to the deploy.
What breaks first, and when it's addressed:
| Pressure | What breaks | Where it's addressed |
|---|---|---|
| kitchens in more than one zone | one zone per deployment | Volume 23's stages (internationalisation: per-kitchen zones and locales) |
a misspelt TIFFINBOX_* variable |
ignored silently | stage 48 (Spring Boot: warn on unknown variables with the prefix) |
replay and report |
still use the default zone | stage 48 (Spring Boot: one configuration for every command) |
Server still holds orders, kitchens and HTTP |
649 lines | stage 49 (Spring Web: controllers and services) |
| the route table was to gain versioned routes (stage 35's plan) | not needed: the API evolves by expand and contract, checked by tools/contract_diff.py (ADR-0048) |
decided in stage 46 |
Evidence
exp/47/zones.out,config-before.out,config-after.out,trace-caught.out.Pricer,Server.Parts,Config, theconfigcommand,Main.serve(Config).- Core 52, server 57 and tbx 21 tests;
golden master: 47 exchanges, identical; the release check passes. - ADR-0049 is committed.
Carry forward
- New settings go into
Config, typed, with a default and a test. - New dependencies go into
Parts(or a service's constructor), built inMain. - Times are evaluated in the configured zone, never the JVM's default.