Skip to content

Commit

Permalink
Updated callback, test and helper.
Browse files Browse the repository at this point in the history
Added connection closed exception.
  • Loading branch information
sagaofsilence committed Apr 14, 2024
1 parent c3ddc2e commit a8afcc7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.http.message.StatusLine;

/** The pipelined http response callback. */
/** The http response callback. */
@Slf4j
public class PipelinedHttpResponseCallback implements FutureCallback<SimpleHttpResponse> {
public class CustomHttpResponseCallback implements FutureCallback<SimpleHttpResponse> {
/** The Http get request. */
private final SimpleHttpRequest httpRequest;

Expand All @@ -27,7 +27,7 @@ public class PipelinedHttpResponseCallback implements FutureCallback<SimpleHttpR
* @param errorMessage the error message
* @param latch the latch
*/
public PipelinedHttpResponseCallback(
public CustomHttpResponseCallback(
SimpleHttpRequest httpRequest, String errorMessage, CountDownLatch latch) {
this.httpRequest = httpRequest;
this.errorMessage = errorMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ private Map<Long, String> getUserWithParallelRequests(
minimalHttpClient.execute(
SimpleRequestProducer.create(httpGetRequest),
SimpleResponseConsumer.create(),
new PipelinedHttpResponseCallback(
new CustomHttpResponseCallback(
httpGetRequest,
MessageFormat.format("Failed to get user for ID: {0}", userId),
latch));
Expand Down Expand Up @@ -422,8 +422,12 @@ private Map<Long, String> getUserWithParallelRequests(
try {
userResponseMap.put(currentUserId, futureEntry.getValue().get().getBodyText());
} catch (Exception e) {
final String message =
MessageFormat.format("Failed to get user for ID: {0}", currentUserId);
String message;
if (e.getCause() instanceof ConnectionClosedException cce) {
message = "Server does not support HTTP/2 multiplexing.";
} else {
message = MessageFormat.format("Failed to get user for ID: {0}", currentUserId);
}
log.error(message, e);
userResponseMap.put(currentUserId, message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class UserAsyncHttpRequestHelperTests extends BaseAsyncExampleTests {
public boolean matches(String value) {
// value should not be null
// value should not be failure message
return value != null && !value.startsWith("Failed to get user");
return value != null
&& (!value.startsWith("Failed to get user")
|| value.equals("Server does not support HTTP/2 multiplexing."));
}
};

Expand Down

0 comments on commit a8afcc7

Please sign in to comment.