ProcessEngine Resource Management
Create a ProcessEngine when the application starts, share it across requests, and close it during shutdown.
The engine is thread-safe and manages its own executors, runtime and compilation caches, and class loaders
for generated code. Most executor pools have explicit capacity limits; parallel branch execution uses a different policy.
1. Choose the right scope
Section titled “1. Choose the right scope”Requests that use the same class loader, extensions, and resource limits can share one engine. It supports all installed flow formats, so TBBPM and BPMN definitions share its caches, executors, and generated-class lifecycle.
You do not need a separate engine for each flow, tenant, or version. Do not create an engine for each request. Create a separate engine when you need a different class-loader scope, extension configuration, or resource limit.
Each engine’s extension manager creates and owns the implementations declared through standard service files.
It closes instantiated AutoCloseable extensions after accepted work and executor tasks actually exit.
The application owns any supplied ProcessComponentResolver and the business objects it returns, including Spring beans.
It also owns its ClassLoader and any ProcessAliasRouteSource supplied through configuration.
Keep these borrowed dependencies available until engine cleanup finishes.
2. Spring Boot lifecycle
Section titled “2. Spring Boot lifecycle”By default, the starter creates an application-scoped ProcessEngine bean and closes it with the application context.
If the application supplies its own ProcessEngine bean, the starter backs off: the application must arrange its shutdown
and keep borrowed dependencies available until cleanup finishes. Setting compileflow.engine.enabled=false also disables
automatic engine creation. The example below shows injection and reuse; the application supplies OrderRequest,
OrderResponse, and the flow resource.
For a complete runnable project, see Quick Start:
import com.alibaba.compileflow.engine.ProcessEngine;import com.alibaba.compileflow.engine.ProcessDefinition;import com.alibaba.compileflow.engine.ProcessModelType;import com.alibaba.compileflow.engine.ProcessExecutionOptions;import com.alibaba.compileflow.engine.ProcessResult;import org.springframework.stereotype.Service;
@Servicepublic final class OrderService {
private final ProcessEngine engine;
public OrderService(ProcessEngine engine) { this.engine = engine; }
public ProcessResult<OrderResponse> process(OrderRequest request) { return engine.execute( ProcessDefinition.classpath(ProcessModelType.TBBPM, "bpm.order.process", "flows/order.process.bpm"), request, OrderResponse.class, ProcessExecutionOptions.defaults()); }}In PROCESS execution mode, Workbench Server reuses the same ProcessEngine for both supported model types.
3. Plain Java lifecycle
Section titled “3. Plain Java lifecycle”Create the engine alongside the application’s other infrastructure and make one component responsible for closing it.
Command-line and batch applications can use a top-level try-with-resources block. This example assumes the TBBPM
module is installed and the named flow resource is present on the classpath:
import com.alibaba.compileflow.engine.ProcessEngine;import com.alibaba.compileflow.engine.ProcessEngineFactory;import com.alibaba.compileflow.engine.ProcessDefinition;import com.alibaba.compileflow.engine.ProcessModelType;
import java.util.Map;
public final class Application {
public static void main(String[] args) { try (ProcessEngine engine = ProcessEngineFactory.create()) { engine.execute( ProcessDefinition.classpath(ProcessModelType.TBBPM, "batch.item.process", "flows/batch/item.process.bpm"), Map.of("itemId", "item-1")) .orElseThrow(); } }}For a long-running service without Spring, keep the engine in the application component that creates it and call
close() from the host’s shutdown callback. Do not call the engine factory for each request.
Isolated tests and short-lived tools can create temporary engines.
Use try-with-resources to close them even when an exception occurs.
4. Capacity and shutdown
Section titled “4. Capacity and shutdown”Most executor pools and the runtime cache have finite default capacities. Tune ProcessExecutorConfig,
ProcessEngineConfig.maxResidentRuntimes, and ProcessEngineConfig.runtimeLoadTimeout using measured load.
The number of flows or tenants does not determine the number of threads required.
The bounded queues for runtime loading, preflight, timed actions, parallel coordination, and event delivery reject
excess submissions. On Java 17, the parallel branch executor has no pending queue and caps its own platform threads
at four times the available processors (at least 8 and at most 64). When all are busy, the submitting thread runs the
branch. On Java 21 and later it uses one virtual thread per task, not a fixed-size thread pool. Neither policy caps
the total number of concurrent downstream calls; apply application-level concurrency limits where needed.
The runtime limit counts entries owned by the engine cache, including those retained by load or Deploy.
An in-flight invocation may still hold an evicted runtime until it finishes, so this setting does not cap all live
runtimes or JVM memory.
As soon as close() starts, the engine rejects new public API calls, including calls through objects previously returned
by runtime() and tooling(). Accepted operations and the follow-up calls they make within the engine may finish.
The caller waits for those operations to complete, executors to exit, and resources to be released.
One timeout covers all three phases: compileflow.engine.shutdown.timeout, which defaults to 15 seconds.
For programmatic configuration, use ProcessEngineConfig.Builder.shutdownTimeout(...).
The result of close() tells the application when it can finish closing other components:
| Result | Engine state and next step |
|---|---|
| Returns normally | Accepted work and executor tasks have exited, and resource cleanup is complete |
Throws IllegalStateException because of timeout or caller interruption |
New work remains rejected; resources still in use stay available while a background cleanup task keeps waiting |
| Called again after timeout or interruption | Waits for the same cleanup task with a fresh caller timeout; a normal return confirms completion |
An extension’s close() fails |
Attempts to close the remaining extensions, then reports failure; closing the engine again observes the same cleanup result and does not call those extensions’ close() again |
The cleanup task releases engine-owned loaders and runtime caches, including their prepared script programs, only after accepted operations and engine-owned executor tasks actually exit. Each resource is released once.
Java cannot safely force arbitrary application code to terminate. Applications and extension implementations must limit
the duration of their own blocking calls. Stop incoming requests before closing the engine, and keep borrowed application
resources available until cleanup finishes. The supplied Spring lifecycle owner waits for the same physical cleanup before
its lifecycle phase or dependency destruction continues. That ownership wait is not bounded by the public close() caller
timeout. Cleanup failures are logged, after which Spring can continue destroying the remaining beans.
The supplied QL executor holds only heap state and has no close operation. Custom extensions release their internal
resources through AutoCloseable; the engine invokes that close as part of its cleanup.
The application’s lifecycle owner calls the engine’s close(). Do not close that engine from a process action, listener,
or an extension’s close() callback. Such calls immediately throw IllegalStateException to avoid waiting for the
current callback itself to finish.
5. Monitor resource use
Section titled “5. Monitor resource use”Monitor:
- threads with the
compileflow-engine-<id>-prefix; - compilation and execution queue saturation or rejection;
- runtime-cache size and eviction behavior;
- shutdown duration and failed close operations;
- heap or class-loader growth after versions are released.
The Spring integration exposes executor capacity metrics through Micrometer when a MeterRegistry is available. See
the Monitoring Guide and Configuration Guide for the supported metrics and capacity
settings.