private ReplyHandler createReplyHandler() {
        return (reply) -> {
            // Generally, handleReply will run in the context of the
            // underlying transport layer's processing thread(s), so we
            // schedule our own reply handling task to avoid blocking it.
            try {
                // Make concurrent reply handling visible in sender thread, if it's active.
                // See SendCreateVisitorsTask.run() for a rationale.
                incrementScheduledHandleReplyTasks();
                taskExecutor.submitTask(new HandleReplyTask(reply));
            } catch (RejectedExecutionException e) {
                decrementScheduleHandleReplyTasks();
                 // We cannot reliably handle reply tasks failing to be submitted, since
                 // the reply task performs all our internal state handling logic. As such,
                 // we just immediately go into a failure destruction mode as soon as this
                 // happens, in which we do not wait for any active messages to be replied
                 // to.
                log.log(Level.WARNING, "Visitor session '" + sessionName +
                        "': failed to submit reply task to executor service! " +
                        "Session cannot reliably continue; terminating it early.", e);

                synchronized (stateMonitor) {
                    transitionTo(new StateDescription(State.FAILED, "Failed to submit reply task to executor service: " + e.getMessage()));
                    if (!done) {
                        markSessionCompleted();
                    }
                }
            }
        };
    }
Attempt to transition to a new state. Depending on the current state,
some transitions may be disallowed, such as transitioning from ABORTED
to COMPLETED, since failures take precedence. Transitioning multiple
times to the same state is a no-op in order to conserve the textual
description given by the first transition to said state (which most
likely is the most useful one for the end-user).

@param newState State to attempt to transition to.
@return State which is current after the transition. If transition was
  successful, will be equal to newState.
documentapi/src/main/java/com/yahoo/documentapi/messagebus/MessageBusVisitorSession.java