Modular monolith project
What you will learn
Build a fictional order application with an inventory module and an order module. A composition root connects the contracts. Test the feature behavior independently and then verify the actual module graph.
- Build a small order workflow with explicit rejection and ownership.
- Use the companion JPMS lab to test exported APIs and hidden packages.
- Record an architecture decision and the limits of an in-memory implementation.
Plain Java examples: save as Main.java in a fresh directory, then run javac --release 21 Main.java and java Main. Standalone Java 21 models; the companion JPMS project has separate build instructions.
Reserve before reporting acceptance
public class Main { static class Stock { private int count; Stock(int count) { if (count < 0) throw new IllegalArgumentException(); this.count=count; } synchronized boolean reserve(int n) { if (n <= 0) throw new IllegalArgumentException("quantity"); if (n > count) return false; count -= n; return true; } synchronized int left() { return count; } } static String place(Stock stock,int n) { return stock.reserve(n) ? "ACCEPTED" : "REJECTED"; } public static void main(String[] args) { var stock = new Stock(3); System.out.println(place(stock,2)); System.out.println(place(stock,2)); System.out.println(stock.left()); }}Expected output:
ACCEPTEDREJECTED1The rejected reservation leaves stock unchanged. A separate order write is not modeled; this result is not proof of durable transaction behavior.
Project map and workflow
Text diagram: shop.app → shop.orders → shop.inventory; shop.app also requires shop.inventory to construct the adapter. Inventory exports shop.inventory.api only. Orders exports shop.orders.api only. Internal classes remain hidden. The order application requests a reservation rather than modifying stock itself. The full companion lab lives under lab-fixtures/projects/v48-order-monolith. Follow its README to compile and run it.
Consistency and failure
A synchronized inventory reservation checks quantity and decrements stock under one monitor. That protects the local inventory operation, not a transaction spanning a database and order persistence. The teaching application returns a result and does not persist orders or promise exactly-once processing. Production design would need an explicit transaction or compensation contract, durable idempotency, authentication and recovery.
Architecture decision record
ADR: retain a single deployment for the fictional team because the use case has no measured need for independent deployment or scale. Alternatives: a layered single project with architecture tests, or independently deployed services. JPMS provides compiler-enforced exports with extra build complexity. Revisit if teams require independent release schedules, fault isolation or substantially different scaling; measure coupling and operating costs rather than count classes. Do not split merely to follow a fashion.
Test the order feature with a fake inventory
public class Main { interface Reserve { boolean reserve(int n); } static String place(Reserve stock,int n) { if (n <= 0) throw new IllegalArgumentException("quantity"); return stock.reserve(n) ? "ACCEPTED" : "REJECTED"; } public static void main(String[] args) { System.out.println(place(n -> true, 2)); System.out.println(place(n -> false, 2)); }}Expected output:
ACCEPTEDREJECTEDA fake isolates the order result mapping from inventory implementation. Integration tests must still cover real inventory state.
Count cross-feature calls for a design decision
public class Main { interface Reserve { boolean reserve(int n); } static boolean place(Reserve stock,int n) { if (n <= 0) throw new IllegalArgumentException("quantity"); return stock.reserve(n); } public static void main(String[] args) { int[] calls = {0}; Reserve stock = n -> { calls[0]++; return n <= 3; }; System.out.println(place(stock,2)); System.out.println(calls[0]); }}Expected output:
true1The counter verifies this workflow makes one port call. It is not a latency measurement; use production-representative telemetry before changing deployment topology.
Common mistakes
Avoid claiming one deployment guarantees atomic business effects. Never expose stock internals just to simplify a test. Do not mistake an in-memory fake for database or distributed integration.
Practice
Write a stock reservation model starting with four units. Reserve three, then two; print acceptance, rejection and remaining stock. Reject non-positive quantities.
Expected output:
truefalse1Compare with the manual review criteria.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Modular monolith project — knowledge check
Modular architecture and contract reasoning.
Who owns the stock mutation?
What does a synchronized reservation guarantee here?
Why use a fake inventory in order tests?
What belongs in an ADR?
When should a service split be reconsidered?