6 The running hexagon: From pieces to system
The chapter shifts from isolated hexagonal building blocks to a working system, showing how the pieces of WriteFlow are assembled into a runnable application without collapsing the architectural boundaries. It emphasizes that the domain, ports, handlers, and adapters were designed separately for clarity, but in practice they must be connected iteratively by an assembly layer that knows about everything while the core remains unaware of the framework around it.
That assembly begins with module structure: Maven modules enforce dependency direction at compile time so inner layers cannot accidentally depend on outer ones. The application core is split into domain, input ports, output ports, and application services, while adapters for REST, persistence, and notifications depend only on the inner modules they are allowed to see. A Spring Boot assembly module then becomes the wiring point, discovering beans through custom stereotypes, creating the command and query buses, and configuring any external infrastructure, all while keeping framework code out of the core.
The chapter then explains runtime concerns that only appear once the system runs, especially transactions and events. Transaction boundaries belong on application service handlers, not repositories or adapters, and a custom bridge lets the Architecture Kernel’s technology-agnostic transactional annotation work with Spring while supporting rollback based on Either values rather than exceptions. Domain events are delivered in-process, often after commit when they should not affect the main use case, while integration events are treated as durable contracts for external systems and are published through the outbox pattern to avoid dual-write failures. By the end, WriteFlow is not just well structured—it starts, executes use cases atomically, reacts to local events, and publishes reliable external events.
Module dependency graph. Inner modules never reference outer ones. The assembly module is the only component that sees everything—its job is to wire, not to contain logic.
Assembly and runtime flow. At startup, Spring discovers beans through stereotype annotations, and the bus assembler creates buses backed by the application context. At runtime, the bus resolves handlers by command type—adapters never reference handlers directly.
The assembled WriteFlow hexagon. The assembly module wires the application at startup, while the runtime structure still respects the hexagonal boundary: input adapters enter through input ports, the bus dispatches to handlers, handlers orchestrate domain behaviour and output ports, and output adapters provide the infrastructure implementations.
Synchronous listeners execute inside the transaction; after-commit listeners execute after. The after-commit listener enters the application as a new use case through the command bus.
From handler to external delivery. The handler calls an output port whose adapter writes the integration event to the outbox within the same transaction. A relay process delivers it to external consumers. Consistency is guaranteed because the outbox write shares the domain state's transaction.
Summary
- Maven modules enforce hexagonal boundaries at compile time. If a dependency is not in the POM, the compiler rejects the import—no runtime discipline required.
- The spring-boot-assembly module is the only component that sees all others. It discovers hexagonal stereotypes (@ApplicationService, @Adapter, @DomainService) through annotation-based component scanning and wires the command and query buses backed by Spring's ApplicationContext.
- The Architecture Kernel's @Transactional annotation declares transactional intent without importing framework concerns. A custom transaction interceptor bridges the gap at runtime—and solves the Either problem: rollback by value, not by exception.
- Transaction boundaries always live at the handler level. The handler is the atomic unit of work—everything it orchestrates succeeds or fails together.
- Domain events decouple secondary effects from the primary use case. After-commit listeners execute only when the primary transaction succeeds, preventing secondary failures from rolling back the primary operation.
- Event listeners are input adapters. They receive a domain event and enter the application core through the command bus—same pattern as a REST controller, different trigger.
- Integration events escape the process boundary. An output port writes them to an outbox table within the same transaction as the domain state change. A relay process delivers them externally. Consumers must be idempotent.
- The assembly module draws a clear line: the application core compiles, runs its logic, and returns results without knowing which framework assembled it.
FAQ
What is the main purpose of Chapter 6, “The running hexagon: From pieces to system”?
This chapter shows how the isolated parts of the hexagonal architecture are assembled into a running application. It covers module boundaries, Spring-based wiring, transaction handling, domain event delivery, and integration event publication.
Why does the book recommend using Maven modules in a hexagonal architecture?
Maven modules make architectural boundaries enforceable at compile time. They prevent unwanted dependencies—such as the domain importing Spring types—by turning boundary violations into build failures instead of relying only on conventions.
What is the role of the application core modules in WriteFlow?
The application core contains the inner hexagon: domain, input ports, output ports, and application handlers. The domain holds entities, value objects, events, and repository interfaces; input ports define use cases; output ports define non-repository outbound contracts; and application contains the handlers that orchestrate use cases.
What is the purpose of the spring-boot-assembly module?
The assembly module is the only module that sees everything. Its job is to wire the application together using Spring Dependency Injection, without containing business logic. It discovers beans, connects ports to adapters, configures infrastructure, and starts the application.
How does Spring discover handlers and adapters that use custom architecture annotations?
The assembly module configures component scanning to include the Architecture Kernel stereotypes such as @ApplicationService, @Adapter, and @DomainService. That way, Spring can discover and register those classes as beans even though they are not annotated with Spring’s default stereotypes.
How are command and query buses wired in this architecture?
The assembly layer creates the command and query bus beans explicitly. It uses the Spring ApplicationContext to build a registry of handlers, then creates bus implementations that resolve handlers by message type at runtime.
How does transaction handling work when handlers return Either instead of throwing exceptions?
A custom transactional starter bridges the Architecture Kernel’s @Transactional annotation to Spring’s transaction system. After a handler returns, the interceptor checks whether the result is a Left value and, if it matches rollback rules, marks the transaction rollback-only.
Why should transaction boundaries be placed on application handlers rather than repositories or adapters?
Handlers orchestrate the full use case, so they are the correct unit of work. Putting the transaction boundary on a repository would split the use case into smaller transactions and break atomicity across the entire operation.
What is the difference between domain events and integration events?
Domain events are internal to the application or bounded context and can use rich domain types. Integration events are external contracts meant for other services or systems, so they should be stable, versionable, and based on simple data like IDs and timestamps.
Why is the outbox pattern needed for integration events?
The outbox pattern solves the dual-write problem by storing the integration event in the same database transaction as the domain change. A separate relay process later publishes the event to external consumers, ensuring consistency and reliable at-least-once delivery.
Clean Applications with Hexagonal Architecture ebook for free