View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.http.async;
28  
29  import java.io.IOException;
30  import java.util.concurrent.atomic.AtomicInteger;
31  
32  import org.apache.hc.client5.http.HttpRoute;
33  import org.apache.hc.client5.http.protocol.HttpClientContext;
34  import org.apache.hc.core5.annotation.Contract;
35  import org.apache.hc.core5.annotation.ThreadingBehavior;
36  import org.apache.hc.core5.concurrent.CancellableDependency;
37  import org.apache.hc.core5.http.HttpException;
38  import org.apache.hc.core5.http.HttpRequest;
39  import org.apache.hc.core5.http.nio.AsyncEntityProducer;
40  import org.apache.hc.core5.util.Args;
41  import org.apache.hc.core5.util.TimeValue;
42  
43  /**
44   * Represents a single element in the client side asynchronous request execution chain.
45   *
46   * @since 5.0
47   */
48  @Contract(threading = ThreadingBehavior.STATELESS)
49  public interface AsyncExecChain {
50  
51      /**
52       * Request execution scope that includes the unique message exchange ID,
53       * the connection route, the original request message, the execution
54       * context and the internal execution runtime.
55       */
56      final class Scope {
57  
58          public final String exchangeId;
59          public final HttpRoute route;
60          public final HttpRequest originalRequest;
61          public final CancellableDependency cancellableDependency;
62          public final HttpClientContext clientContext;
63          public final AsyncExecRuntime execRuntime;
64          public final Scheduler scheduler;
65          public final AtomicInteger execCount;
66  
67          /**
68           * @since 5.1
69           */
70          public Scope(
71                  final String exchangeId,
72                  final HttpRoute route,
73                  final HttpRequest originalRequest,
74                  final CancellableDependency cancellableDependency,
75                  final HttpClientContext clientContext,
76                  final AsyncExecRuntime execRuntime,
77                  final Scheduler scheduler,
78                  final AtomicInteger execCount) {
79              this.exchangeId = Args.notBlank(exchangeId, "Exchange id");
80              this.route = Args.notNull(route, "Route");
81              this.originalRequest = Args.notNull(originalRequest, "Original request");
82              this.cancellableDependency = Args.notNull(cancellableDependency, "Dependency");
83              this.clientContext = clientContext != null ? clientContext : HttpClientContext.create();
84              this.execRuntime = Args.notNull(execRuntime, "Exec runtime");
85              this.scheduler = scheduler;
86              this.execCount = execCount != null ? execCount : new AtomicInteger(1);
87          }
88  
89          /**
90           * @deprecated Use {@link Scope#Scope(String, HttpRoute, HttpRequest, CancellableDependency, HttpClientContext,
91           * AsyncExecRuntime, Scheduler, AtomicInteger)}
92           */
93          @Deprecated
94          public Scope(
95                  final String exchangeId,
96                  final HttpRoute route,
97                  final HttpRequest originalRequest,
98                  final CancellableDependency cancellableDependency,
99                  final HttpClientContext clientContext,
100                 final AsyncExecRuntime execRuntime) {
101             this(exchangeId, route, originalRequest, cancellableDependency, clientContext, execRuntime,
102                     null, new AtomicInteger(1));
103         }
104 
105     }
106 
107     /**
108      * Request execution scheduler
109      *
110      * @since 5.1
111      */
112     interface Scheduler {
113 
114         /**
115          * Schedules request re-execution immediately or after a delay.
116          * @param request the actual request.
117          * @param entityProducer the request entity producer or {@code null} if the request
118          *                      does not enclose an entity.
119          * @param scope the execution scope .
120          * @param asyncExecCallback the execution callback.
121          * @param delay re-execution delay. Can be {@code null} if the request is to be
122          *              re-executed immediately.
123          */
124         void scheduleExecution(
125                 HttpRequest request,
126                 AsyncEntityProducer entityProducer,
127                 AsyncExecChain.Scope scope,
128                 AsyncExecCallback asyncExecCallback,
129                 TimeValue delay);
130 
131     }
132 
133     /**
134      * Proceeds to the next element in the request execution chain.
135      *
136      * @param request the actual request.
137      * @param entityProducer the request entity producer or {@code null} if the request
138      *                      does not enclose an entity.
139      * @param scope the execution scope .
140      * @param asyncExecCallback the execution callback.
141      */
142     void proceed(
143             HttpRequest request,
144             AsyncEntityProducer entityProducer,
145             Scope scope,
146             AsyncExecCallback asyncExecCallback) throws HttpException, IOException;
147 
148 }