Resource management: try-with-resources and Execute-Around
Deterministic cleanup in Java, proven to run on every path including exceptions.
Java has no deterministic destructors, so cleanup is a language-structured
concern. This module shows the two tools that give the same guarantee RAII gives
in other languages: try-with-resources over an AutoCloseable, and the
Execute-Around Method idiom, where a library method owns the setup and the
guaranteed teardown around a caller’s lambda.
Shipping-domain resources (a carrier connection, a batch ledger writer, a
tracking-feed handle) implement AutoCloseable. The JUnit 5 tests prove the
release runs on the normal path and when an exception unwinds the block, using a
Mockito spy to confirm close() is called exactly once.
Build, run, observe
cd examples/05-resource-management
mvn -q test
./demo.sh
The module also runs from the aggregator root with
mvn -q -f examples/pom.xml test.