cancel requests if interrupted

This commit is contained in:
Athou
2026-10-13 11:12:46 +02:00
parent 26cce1faac
commit f72bd4cc5c
2 changed files with 10 additions and 9 deletions

View File

@@ -237,6 +237,7 @@ public class HttpGetter {
contentType, contentType,
urlAfterRedirect); urlAfterRedirect);
} catch (InterruptedException e) { } catch (InterruptedException e) {
future.cancel(true);
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
throw new RuntimeException("interrupted while fetching " + request.getUrl(), e); throw new RuntimeException("interrupted while fetching " + request.getUrl(), e);
} catch (ExecutionException e) { } catch (ExecutionException e) {

View File

@@ -17,13 +17,13 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder; import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
import org.apache.hc.client5.http.async.methods.SimpleRequestProducer; import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpResponse; import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.Message;
import org.apache.hc.core5.http.NameValuePair; import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.message.BasicNameValuePair; import org.apache.hc.core5.http.message.BasicNameValuePair;
import org.apache.hc.core5.http.nio.entity.DiscardingEntityConsumer; import org.apache.hc.core5.http.nio.entity.DiscardingEntityConsumer;
@@ -36,6 +36,7 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
@Singleton @Singleton
@Slf4j @Slf4j
@@ -198,20 +199,19 @@ public class PushNotificationService {
} }
private void send(String name, SimpleHttpRequest request) throws IOException { private void send(String name, SimpleHttpRequest request) throws IOException {
Future<Message<HttpResponse, Object>> future =
httpClient.execute(
SimpleRequestProducer.create(request),
new BasicResponseConsumer<>(new DiscardingEntityConsumer<>()),
null);
try { try {
HttpResponse response = HttpResponse response = future.get().getHead();
httpClient
.execute(
SimpleRequestProducer.create(request),
new BasicResponseConsumer<>(new DiscardingEntityConsumer<>()),
null)
.get()
.getHead();
if (response.getCode() >= 400) { if (response.getCode() >= 400) {
throw new PushNotificationException( throw new PushNotificationException(
name + " notification failed with status " + response.getCode()); name + " notification failed with status " + response.getCode());
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
future.cancel(true);
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
throw new IOException("interrupted while sending " + name + " notification", e); throw new IOException("interrupted while sending " + name + " notification", e);
} catch (ExecutionException e) { } catch (ExecutionException e) {