void scheduleProcessNextPolicy()

in sdk/core/azure-core-http/src/main/java/com/azure/android/core/http/HttpCallDispatcher.java [182:228]


    void scheduleProcessNextPolicy(HttpPipelinePolicyChainImpl chain,
                                   HttpRequest httpRequest,
                                   RequestContext requestContext,
                                   NextPolicyCallback callback,
                                   long delay,
                                   TimeUnit timeUnit) {
        Util.requireNonNull(chain, "'chain' is required.");
        Util.requireNonNull(httpRequest, "'httpRequest' is required.");
        Util.requireNonNull(requestContext, "'context' is required.");
        Util.requireNonNull(callback, "'httpCallback' is required.");
        Util.requireNonNull(timeUnit, "'timeUnit' is required.");

        final RootDispatchableCall rootDispatchableCall = this.getRootDispatchableCall(chain);
        final NestedDispatchableCall nestedDispatchableCall = new NestedDispatchableCall(rootDispatchableCall,
            chain,
            httpRequest,
            callback);
        boolean scheduled = false;
        try {
            this.getScheduledExecutorService().schedule(() -> {
                synchronized (HttpCallDispatcher.this) {
                    // The HttpCallDispatcher::executorService executes both 'RootDispatchableCall'
                    // and 'NestedDispatchableCall' calls.
                    // Using HttpCallDispatcher::scheduledExecutorService to hand over
                    // the 'NestedDispatchableCall' to HttpCallDispatcher::executorService.
                    HttpCallDispatcher.this.waitingNestedDispatchableCalls.add(nestedDispatchableCall);
                }
                HttpCallDispatcher.this.dispatchCalls();
            }, delay, timeUnit);
            scheduled = true;
        } catch (RejectedExecutionException e) {
            nestedDispatchableCall
                .onError(new InterruptedIOException("scheduled executor rejected").initCause(e));
        } catch (Throwable t) {
            // The ScheduledExecutorService::execute() is not supposed to throw any exception
            // other than RejectedExecutionException, but if it ever throws other exceptions,
            // let's do the cleanup and then rethrow.
            rootDispatchableCall.markNotRunning(1);
            throw logger.logExceptionAsError(new RuntimeException("ScheduledExecutorService::schedule failed.", t));
        }

        if (scheduled) {
            // Once scheduled successfully, pipeline is in "pause-mode", yield the thread to other
            // executable calls waiting to run.
            rootDispatchableCall.markNotRunning(2);
        }
    }