@@ -134,17 +134,46 @@ public void close() {
134134
135135 public class Request {
136136 private final okhttp3 .Request .Builder req ;
137+ private SneakyThrows .Consumer <okhttp3 .Request .Builder > configurer ;
137138
138139 public Request (okhttp3 .Request .Builder req ) {
139140 this .req = req ;
140141 }
141142
142143 public Request prepare (SneakyThrows .Consumer <okhttp3 .Request .Builder > configurer ) {
143- configurer . accept ( req ) ;
144+ this . configurer = configurer ;
144145 return this ;
145146 }
146147
147148 public void execute (SneakyThrows .Consumer <Response > callback ) {
149+ execute (1 , callback );
150+ }
151+
152+ public void execute (int concurrency , SneakyThrows .Consumer <Response > callback ) {
153+ if (configurer != null ) {
154+ configurer .accept (req );
155+ }
156+ if (concurrency > 1 ) {
157+ var futures = new ArrayList <CompletableFuture <String >>();
158+ for (var i = 0 ; i < concurrency ; i ++) {
159+ futures .add (
160+ CompletableFuture .supplyAsync (
161+ () -> {
162+ executeCall (callback );
163+ return "success" ;
164+ }));
165+ try {
166+ CompletableFuture .allOf (futures .toArray (new CompletableFuture [0 ])).join ();
167+ } catch (CompletionException x ) {
168+ throw SneakyThrows .propagate (x .getCause ());
169+ }
170+ }
171+ } else {
172+ executeCall (callback );
173+ }
174+ }
175+
176+ private void executeCall (SneakyThrows .Consumer <Response > callback ) {
148177 okhttp3 .Request r = req .build ();
149178 try (Response rsp = client .newCall (r ).execute ()) {
150179 callback .accept (rsp );
@@ -168,16 +197,12 @@ public WebClient(String scheme, int port, boolean followRedirects) {
168197 try {
169198 this .scheme = scheme ;
170199 this .port = port ;
171- Dispatcher dispatcher = new Dispatcher ();
172- dispatcher .setMaxRequests (100 ); // Maximum 20 concurrent requests overall
173- dispatcher .setMaxRequestsPerHost (100 );
174200 OkHttpClient .Builder builder =
175201 new OkHttpClient .Builder ()
176202 .connectTimeout (5 , TimeUnit .MINUTES )
177203 .writeTimeout (5 , TimeUnit .MINUTES )
178204 .readTimeout (5 , TimeUnit .MINUTES )
179- .followRedirects (followRedirects )
180- .dispatcher (dispatcher );
205+ .followRedirects (followRedirects );
181206 if (scheme .equalsIgnoreCase ("https" )) {
182207 configureSelfSigned (builder );
183208 }
@@ -225,32 +250,6 @@ public Request get(String path) {
225250 return invoke ("GET" , path , null );
226251 }
227252
228- public void concurrent (String path , int concurrency , SneakyThrows .Consumer <Response > callback ) {
229- var futures = new ArrayList <Future <String >>();
230- try (var executor = Executors .newFixedThreadPool (concurrency + 5 )) {
231- for (var i = 0 ; i < concurrency ; i ++) {
232- futures .add (
233- executor .submit (
234- () -> {
235- okhttp3 .Request .Builder req = new okhttp3 .Request .Builder ();
236- req .url (scheme + "://localhost:" + port + path );
237- new Request (req ).execute (callback );
238- return "OK" ;
239- }));
240- }
241- for (Future <String > future : futures ) {
242- try {
243- var result = future .get ();
244- if (!"OK" .equals (result )) {
245- throw new IllegalStateException (result );
246- }
247- } catch (Exception e ) {
248- SneakyThrows .propagate (e );
249- }
250- }
251- }
252- }
253-
254253 public ServerSentMessageIterator sse (String path ) {
255254 okhttp3 .Request .Builder req = new okhttp3 .Request .Builder ();
256255 setRequestHeaders (req );
0 commit comments