public void setStateListener(final KafkaStreams.StateListener listener) {
        synchronized (stateLock) {
            if (state.hasNotStarted()) {
                stateListener = listener;
            } else {
                throw new IllegalStateException("Can only set StateListener before calling start(). Current state is: " + state);
            }
        }
    }
An app can set a single {@link KafkaStreams.StateListener} so that the app is notified when state changes.

@param listener a new state listener
@throws IllegalStateException if this {@code KafkaStreams} instance has already been started.
streams/src/main/java/org/apache/kafka/streams/KafkaStreams.java