8
8
import org .apache .hc .client5 .http .impl .async .CloseableHttpAsyncClient ;
9
9
import org .apache .hc .core5 .concurrent .FutureCallback ;
10
10
import org .apache .hc .core5 .http .HttpResponse ;
11
+ import org .slf4j .Logger ;
12
+ import org .slf4j .LoggerFactory ;
11
13
12
14
/** Utility class to execute HTTP requests with callbacks */
13
15
public class HttpRequestSender {
16
+ private static final Logger log = LoggerFactory .getLogger (HttpRequestSender .class );
14
17
15
18
/**
16
19
* Executes the provided HTTP request as well as the callback function.
@@ -19,12 +22,27 @@ public class HttpRequestSender {
19
22
* @param callback the callback function
20
23
* @return True, if the HTTP request returned the status code 200
21
24
*/
22
- public static boolean send (SimpleHttpRequest request , FutureCallback <SimpleHttpResponse > callback )
23
- throws ExecutionException , InterruptedException {
25
+ public static boolean send (
26
+ SimpleHttpRequest request , FutureCallback <SimpleHttpResponse > callback ) {
27
+ if (Objects .isNull (request )) return false ;
28
+
24
29
CloseableHttpAsyncClient client = HttpClientHolder .getClient ();
25
30
Future <SimpleHttpResponse > future = client .execute (request , callback );
26
- HttpResponse response = future .get ();
27
31
32
+ HttpResponse response = handleFuture (future );
28
33
return Objects .nonNull (response ) && 200 == response .getCode ();
29
34
}
35
+
36
+ private static HttpResponse handleFuture (Future <SimpleHttpResponse > future ) {
37
+ HttpResponse response = null ;
38
+ try {
39
+ response = future .get ();
40
+ } catch (ExecutionException e ) {
41
+ log .error ("Error executing HTTP request" , e );
42
+ } catch (InterruptedException e ) {
43
+ log .error ("Sending of HTTP request was interrupted" , e );
44
+ Thread .currentThread ().interrupt ();
45
+ }
46
+ return response ;
47
+ }
30
48
}
0 commit comments