Principle:APIJSON APIJSON Demo Process Supervision
| Knowledge Sources | |
|---|---|
| Domains | Process_Management, DevOps, Reliability |
| Last Updated | 2026-10-14 00:00 GMT |
Overview
Reliability pattern that manages an external process (Node.js server) lifecycle within a Java application, providing automatic startup on application ready, periodic health checks, crash recovery, and graceful shutdown.
Description
Process Supervision wraps an external process — in APIJSON's case, the Node.js APIAuto test server — with lifecycle management. A Spring service starts the process when the application is ready, periodically checks if it is reachable via HTTP, restarts it if it crashes, and cleanly stops it on application shutdown.
This solves:
- External process crashes: Without supervision, a crashed Node.js process would halt automated testing until manual intervention.
- Startup ordering: The external process must start after the Java application is fully ready (not just context loaded).
- Resource cleanup: On application shutdown, the managed process must be terminated to avoid orphan processes.
The supervisor uses three mechanisms:
- ApplicationReadyEvent listener: Starts the process after Spring Boot is fully initialized.
- Scheduled health check: Polls the process's HTTP endpoint every 30 seconds; restarts if unreachable.
- PreDestroy hook: Terminates the process on JVM shutdown.
Usage
Use this principle when:
- A Java application needs to manage a companion process (Node.js, Python, etc.) as part of its service.
- The companion process may crash and needs automatic recovery.
- You want the companion process lifecycle tied to the main application lifecycle.
Theoretical Basis
Lifecycle States:
ApplicationReadyEvent
│
▼
ensureRunning()
├── isNodeServiceReachable()? → HTTP GET statusUrl → 2xx?
│ YES → done (already running)
│ NO → startProcess()
│ ├── Resolve working directory
│ ├── Verify script exists
│ ├── ProcessBuilder(nodeCommand, nodeScript)
│ ├── Redirect output to log file
│ └── process.start()
│
└── waitUntilReachable()
└── Poll until 2xx or startupTimeoutMillis
@Scheduled(fixedDelay = 30000)
keepAlive()
└── ensureRunning() // same as above
@PreDestroy
stopManagedProcess()
└── process.destroy()
Concurrency safety: Uses synchronized(lock) and double-checked isNodeServiceReachable() to prevent multiple threads from starting duplicate processes.