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.Internal;
36 import org.apache.hc.core5.annotation.ThreadingBehavior;
37 import org.apache.hc.core5.concurrent.CancellableDependency;
38 import org.apache.hc.core5.http.HttpException;
39 import org.apache.hc.core5.http.HttpRequest;
40 import org.apache.hc.core5.http.nio.AsyncEntityProducer;
41 import org.apache.hc.core5.util.Args;
42 import org.apache.hc.core5.util.TimeValue;
43
44 /**
45 * Represents a single element in the client side asynchronous request execution chain.
46 *
47 * @since 5.0
48 */
49 @Contract(threading = ThreadingBehavior.STATELESS)
50 public interface AsyncExecChain {
51
52 /**
53 * Request execution scope that includes the unique message exchange ID,
54 * the connection route, the original request message, the execution
55 * context and the internal execution runtime.
56 */
57 final class Scope {
58
59 public final String exchangeId;
60 public final HttpRoute route;
61 public final HttpRequest originalRequest;
62 public final CancellableDependency cancellableDependency;
63 public final HttpClientContext clientContext;
64 public final AsyncExecRuntime execRuntime;
65 public final Scheduler scheduler;
66 public final AtomicInteger execCount;
67
68 /**
69 * @since 5.1
70 */
71 public Scope(
72 final String exchangeId,
73 final HttpRoute route,
74 final HttpRequest originalRequest,
75 final CancellableDependency cancellableDependency,
76 final HttpClientContext clientContext,
77 final AsyncExecRuntime execRuntime,
78 final Scheduler scheduler,
79 final AtomicInteger execCount) {
80 this.exchangeId = Args.notBlank(exchangeId, "Exchange id");
81 this.route = Args.notNull(route, "Route");
82 this.originalRequest = Args.notNull(originalRequest, "Original request");
83 this.cancellableDependency = Args.notNull(cancellableDependency, "Dependency");
84 this.clientContext = Args.notNull(clientContext, "HTTP context");
85 this.execRuntime = Args.notNull(execRuntime, "Exec runtime");
86 this.scheduler = scheduler;
87 this.execCount = execCount != null ? execCount : new AtomicInteger(1);
88 }
89
90 /**
91 * @deprecated Use {@link Scope#Scope(String, HttpRoute, HttpRequest, CancellableDependency, HttpClientContext,
92 * AsyncExecRuntime, Scheduler, AtomicInteger)}
93 */
94 @Deprecated
95 public Scope(
96 final String exchangeId,
97 final HttpRoute route,
98 final HttpRequest originalRequest,
99 final CancellableDependency cancellableDependency,
100 final HttpClientContext clientContext,
101 final AsyncExecRuntime execRuntime) {
102 this(exchangeId, route, originalRequest, cancellableDependency, clientContext, execRuntime,
103 null, new AtomicInteger(1));
104 }
105
106 }
107
108 /**
109 * Request execution scheduler
110 *
111 * @since 5.1
112 */
113 interface Scheduler {
114
115 /**
116 * Schedules request re-execution immediately or after a delay.
117 * @param request the actual request.
118 * @param entityProducer the request entity producer or {@code null} if the request
119 * does not enclose an entity.
120 * @param scope the execution scope.
121 * @param asyncExecCallback the execution callback.
122 * @param delay re-execution delay. Can be {@code null} if the request is to be
123 * re-executed immediately.
124 */
125 void scheduleExecution(
126 HttpRequest request,
127 AsyncEntityProducer entityProducer,
128 AsyncExecChain.Scope scope,
129 AsyncExecCallback asyncExecCallback,
130 TimeValue delay);
131
132 /**
133 * Schedules request re-execution of the given execution chain immediately or
134 * after a delay.
135 * @param request the actual request.
136 * @param entityProducer the request entity producer or {@code null} if the request
137 * does not enclose an entity.
138 * @param scope the execution scope.
139 * @param chain the execution chain.
140 * @param asyncExecCallback the execution callback.
141 * @param delay re-execution delay. Can be {@code null} if the request is to be
142 * re-executed immediately.
143 *
144 * @since 5.3
145 */
146 @Internal
147 default void scheduleExecution(
148 HttpRequest request,
149 AsyncEntityProducer entityProducer,
150 AsyncExecChain.Scope scope,
151 AsyncExecChain chain,
152 AsyncExecCallback asyncExecCallback,
153 TimeValue delay) {
154 scheduleExecution(request, entityProducer, scope, asyncExecCallback, delay);
155 }
156
157 }
158
159 /**
160 * Proceeds to the next element in the request execution chain.
161 *
162 * @param request the actual request.
163 * @param entityProducer the request entity producer or {@code null} if the request
164 * does not enclose an entity.
165 * @param scope the execution scope .
166 * @param asyncExecCallback the execution callback.
167 */
168 void proceed(
169 HttpRequest request,
170 AsyncEntityProducer entityProducer,
171 Scope scope,
172 AsyncExecCallback asyncExecCallback) throws HttpException, IOException;
173
174 }