-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
90 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>de.malkusch.km200</groupId> | ||
<artifactId>km200</artifactId> | ||
|
@@ -13,7 +15,8 @@ | |
<url>https://github.com/malkusch/${project.artifactId}</url> | ||
<scm> | ||
<connection>scm:git:git://github.com/malkusch/${project.artifactId}.git</connection> | ||
<developerConnection>scm:git:[email protected]:malkusch/${project.artifactId}.git</developerConnection> | ||
<developerConnection> | ||
scm:git:[email protected]:malkusch/${project.artifactId}.git</developerConnection> | ||
</scm> | ||
<properties> | ||
<github.repository>malkusch/km200</github.repository> | ||
|
77 changes: 77 additions & 0 deletions
77
src/main/java/de/malkusch/km200/HttpTimeoutWorkaround.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package de.malkusch.km200; | ||
|
||
import static java.net.http.HttpClient.newBuilder; | ||
import static java.net.http.HttpClient.Redirect.ALWAYS; | ||
import static java.util.Optional.ofNullable; | ||
import static java.util.concurrent.TimeUnit.MILLISECONDS; | ||
|
||
import java.io.IOException; | ||
import java.net.CookieManager; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.net.http.HttpResponse.BodyHandler; | ||
import java.net.http.HttpTimeoutException; | ||
import java.time.Duration; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
final class HttpTimeoutWorkaround implements AutoCloseable { | ||
|
||
private final Duration timeout; | ||
private final HttpClient client; | ||
private final ScheduledExecutorService executor; | ||
|
||
HttpTimeoutWorkaround(Duration timeout) { | ||
this.client = newBuilder() // | ||
.connectTimeout(timeout) // | ||
.cookieHandler(new CookieManager()) // | ||
.followRedirects(ALWAYS) // | ||
.build(); | ||
|
||
this.timeout = timeout.plusSeconds(1); | ||
|
||
executor = Executors.newSingleThreadScheduledExecutor(r -> { | ||
var thread = new Thread(r, "KM200"); | ||
thread.setUncaughtExceptionHandler((t, e) -> { | ||
e.printStackTrace(); | ||
}); | ||
thread.setDaemon(true); | ||
return thread; | ||
}); | ||
} | ||
|
||
public <T> HttpResponse<T> send(HttpRequest request, BodyHandler<T> bodyHandler) | ||
throws IOException, InterruptedException { | ||
|
||
try { | ||
return executor // | ||
.submit(() -> client.send(request, bodyHandler)) // | ||
.get(timeout.toMillis(), MILLISECONDS); | ||
|
||
} catch (TimeoutException e) { | ||
throw new HttpTimeoutException("KM200 timed out (HttpClient timed out)"); | ||
|
||
} catch (ExecutionException e) { | ||
if (e.getCause() instanceof HttpTimeoutException timeoutException) { | ||
throw timeoutException; | ||
} | ||
throw new IOException("KM200 failed", ofNullable(e.getCause()).orElse(e)); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
executor.shutdown(); | ||
if (executor.awaitTermination(timeout.toMillis(), TimeUnit.MILLISECONDS)) { | ||
return; | ||
} | ||
executor.shutdownNow(); | ||
if (!executor.awaitTermination(timeout.toMillis(), TimeUnit.MILLISECONDS)) { | ||
throw new IOException("Closing KM200 failed"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters