|
2 | 2 |
|
3 | 3 | import java.time.Duration; |
4 | 4 | import lombok.extern.slf4j.Slf4j; |
| 5 | +import org.apache.commons.lang3.StringUtils; |
| 6 | +import org.apache.http.message.BasicHeaderElementIterator; |
| 7 | +import org.apache.http.protocol.HTTP; |
5 | 8 | import org.springframework.beans.factory.annotation.Value; |
6 | 9 | import org.springframework.context.annotation.Configuration; |
7 | 10 | import org.springframework.data.elasticsearch.client.ClientConfiguration; |
| 11 | +import org.springframework.data.elasticsearch.client.elc.ElasticsearchClients; |
8 | 12 | import org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration; |
9 | 13 | import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; |
10 | 14 |
|
@@ -42,6 +46,36 @@ public ClientConfiguration clientConfiguration() { |
42 | 46 | .withBasicAuth(this.user, this.password) |
43 | 47 | .withConnectTimeout(Duration.ofSeconds(connectTimeout)) |
44 | 48 | .withSocketTimeout(Duration.ofSeconds(socketTimeout)) |
| 49 | + .withClientConfigurer(ElasticsearchClients.ElasticsearchHttpClientConfigurationCallback |
| 50 | + .from(clientBuilder -> { |
| 51 | + /** |
| 52 | + * Setzen der {@link org.apache.http.conn.ConnectionKeepAliveStrategy} in Millisekunden. |
| 53 | + */ |
| 54 | + clientBuilder.setKeepAliveStrategy((httpResponse, httpContext) -> { |
| 55 | + final var headerIterator = new BasicHeaderElementIterator(httpResponse.headerIterator(HTTP.CONN_KEEP_ALIVE)); |
| 56 | + |
| 57 | + while (headerIterator.hasNext()) { |
| 58 | + final var header = headerIterator.nextElement(); |
| 59 | + final var headerName = header.getName(); |
| 60 | + final var headerValue = header.getValue(); |
| 61 | + |
| 62 | + if (StringUtils.isNotEmpty(headerValue) && headerName.equalsIgnoreCase("timeout")) { |
| 63 | + try { |
| 64 | + final var timeoutSeconds = Long.parseLong(headerValue); |
| 65 | + // to millis |
| 66 | + return timeoutSeconds * 1000; |
| 67 | + } catch (NumberFormatException ignore) { |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Connections nicht unendlich lange offen halten, |
| 73 | + // da Netzwerk-Firewall sie sonst evtl. mit deny auslaufen lässt. |
| 74 | + // 30000 Millisekunden |
| 75 | + return 30 * 1000; |
| 76 | + }); |
| 77 | + return clientBuilder; |
| 78 | + })) |
45 | 79 | .build(); |
46 | 80 | } |
47 | 81 |
|
|
0 commit comments