Class TikaHttpClient
- All Implemented Interfaces:
Closeable,AutoCloseable
Built on HttpClient with a daemon thread executor
so the JVM — including forked PipesServer processes — shuts down
cleanly without waiting for idle HTTP threads.
This class has no runtime dependencies beyond the JDK and tika-core.
Obtain an instance via build(int) and close it when done to release
the underlying executor.
A hosted inference engine caps the requests in flight per key and answers the excess with
429 at once, usually without a Retry-After; a saturated one answers 502, 503 or 504. Those
four statuses are retried up to maxRetries times with a short jittered backoff
(250 ms doubling to 4 s), or the Retry-After the server sends, never sleeping past the
budget the parse granted the call. Any other status fails at once. The retries cover
collisions between a few parse workers, not a sustained excess of workers over the key's
concurrency.
- Since:
- Apache Tika 4.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intRetries of a 429/502/503/504 answer unlessbuild(int, int)says otherwise. -
Method Summary
Modifier and TypeMethodDescriptionstatic TikaHttpClientbuild(int connectTimeoutSeconds) Create a newTikaHttpClientwith a daemon-thread executor.static TikaHttpClientbuild(int connectTimeoutSeconds, int maxRetries) Create a newTikaHttpClientwith a daemon-thread executor.voidclose()GETurland return the response body as a string.Same asget(String, Map, long), but bounds the wait tomin(requestedTimeoutMillis, ParseTimeout.remainingMillis())and checkpoints while waiting -- seepostJson(String, String, Map, long, ParseContext).intPOST a JSON body tourland return the response body as a string.postJson(String url, String jsonBody, Map<String, String> headers, long requestedTimeoutMillis, ParseContext context) Same aspostJson(String, String, Map, long), but bounds the wait tomin(requestedTimeoutMillis, ParseTimeout.remainingMillis())(seeParseTimeout.budgetFor(long)) so no single call can outlast the task's total timeout regardless of its own configuration.
-
Field Details
-
DEFAULT_MAX_RETRIES
public static final int DEFAULT_MAX_RETRIESRetries of a 429/502/503/504 answer unlessbuild(int, int)says otherwise.- See Also:
-
-
Method Details
-
build
Create a newTikaHttpClientwith a daemon-thread executor.- Parameters:
connectTimeoutSeconds- TCP connection timeout in seconds
-
build
Create a newTikaHttpClientwith a daemon-thread executor.- Parameters:
connectTimeoutSeconds- TCP connection timeout in secondsmaxRetries- how many times a 429, 502, 503 or 504 answer is retried; 0 fails on the first one
-
getMaxRetries
public int getMaxRetries() -
postJson
public String postJson(String url, String jsonBody, Map<String, String> headers, long requestedTimeoutMillis) throws IOException, TikaExceptionPOST a JSON body tourland return the response body as a string.Equivalent to
postJson(String, String, Map, long, ParseContext)with a null context:requestedTimeoutMillisis granted unclipped, no checkpointing.- Parameters:
url- target URLjsonBody- request body (UTF-8 JSON)headers- additional HTTP headers (e.g.Authorization)requestedTimeoutMillis- read timeout in millis;0uses the default timeout- Returns:
- response body string
- Throws:
IOException- on network errorTikaException- on non-2xx HTTP status
-
postJson
public String postJson(String url, String jsonBody, Map<String, String> headers, long requestedTimeoutMillis, ParseContext context) throws IOException, TikaExceptionSame aspostJson(String, String, Map, long), but bounds the wait tomin(requestedTimeoutMillis, ParseTimeout.remainingMillis())(seeParseTimeout.budgetFor(long)) so no single call can outlast the task's total timeout regardless of its own configuration. While waiting, checkpoints theParseTimeoutincontext(if any) every 1000L ms -- seeProcessUtils.execute(ProcessBuilder, ParseContext, long, int, int)for the same rationale applied to subprocess calls. A nullcontextmeans the budget is granted unclipped.- Parameters:
url- target URLjsonBody- request body (UTF-8 JSON)headers- additional HTTP headers (e.g.Authorization)requestedTimeoutMillis- the timeout the caller's own configuration asks for, in millis;0uses the default timeoutcontext- may be null- Returns:
- response body string
- Throws:
IOException- on network errorTikaException- on non-2xx HTTP status
-
get
public String get(String url, Map<String, String> headers, long requestedTimeoutMillis) throws IOException, TikaExceptionGETurland return the response body as a string. Useful for health-check probes at init time.Equivalent to
get(String, Map, long, ParseContext)with a null context.- Parameters:
url- target URLheaders- additional HTTP headersrequestedTimeoutMillis- read timeout in millis;0uses the default timeout- Returns:
- response body string
- Throws:
IOException- on network errorTikaException- on non-2xx HTTP status
-
get
public String get(String url, Map<String, String> headers, long requestedTimeoutMillis, ParseContext context) throws IOException, TikaExceptionSame asget(String, Map, long), but bounds the wait tomin(requestedTimeoutMillis, ParseTimeout.remainingMillis())and checkpoints while waiting -- seepostJson(String, String, Map, long, ParseContext).- Parameters:
url- target URLheaders- additional HTTP headersrequestedTimeoutMillis- the timeout the caller's own configuration asks for, in millis;0uses the default timeoutcontext- may be null- Returns:
- response body string
- Throws:
IOException- on network errorTikaException- on non-2xx HTTP status
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-