Implementation:Apache Spark StreamingContextState
| Knowledge Sources | |
|---|---|
| Domains | Streaming, Lifecycle |
| Last Updated | 2026-02-08 22:00 GMT |
Overview
Enum representing the three lifecycle states of a Spark StreamingContext: INITIALIZED, ACTIVE, and STOPPED.
Description
⚠️ DEPRECATED: DStream-based Spark Streaming is deprecated. Use Structured Streaming for new applications. See Heuristic:Apache_Spark_Warning_Deprecated_DStream_Streaming.
StreamingContextState is a Java enum annotated with `@DeveloperApi` that models the finite state machine of a `StreamingContext`. The three states are:
- INITIALIZED - The context has been created but not started. DStreams, transformations, and output operations can be configured.
- ACTIVE - The context is running. No new DStreams or transformations can be added.
- STOPPED - The context has been stopped and cannot be reused.
This enum is used by `StreamingContext.getState()` to expose the current lifecycle stage.
Usage
Check this enum when you need to conditionally execute code based on the StreamingContext lifecycle. For example, verify the context is ACTIVE before performing streaming operations, or check for INITIALIZED before adding new DStreams.
Code Reference
Source Location
- Repository: Apache_Spark
- File: streaming/.../StreamingContextState.java
- Lines: 1-45
Signature
@DeveloperApi
public enum StreamingContextState {
/** Created but not started. DStreams can be configured. */
INITIALIZED,
/** Started and running. No new DStreams can be created. */
ACTIVE,
/** Stopped. Cannot be reused. */
STOPPED
}
Import
import org.apache.spark.streaming.StreamingContextState;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| (none) | — | — | Enum values are constants; no inputs required |
Outputs
| Name | Type | Description |
|---|---|---|
| INITIALIZED | StreamingContextState | Context created but not started |
| ACTIVE | StreamingContextState | Context running |
| STOPPED | StreamingContextState | Context terminated |
Usage Examples
Check StreamingContext State
import org.apache.spark.streaming.StreamingContext;
import org.apache.spark.streaming.StreamingContextState;
StreamingContext ssc = new StreamingContext(sparkConf, Durations.seconds(1));
// Before starting
assert ssc.getState() == StreamingContextState.INITIALIZED;
ssc.start();
assert ssc.getState() == StreamingContextState.ACTIVE;
ssc.stop(true, true);
assert ssc.getState() == StreamingContextState.STOPPED;