Skip to content

Commit ff8469b

Browse files
authored
Hotfix für keepalive der Elasicsearch-Verbindung (#419)
add keepalive to elasticsearch connection
1 parent 9fa0ffa commit ff8469b

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/main/java/de/muenchen/dave/configuration/CustomElasticsearchConfiguration.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
import java.time.Duration;
44
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;
58
import org.springframework.beans.factory.annotation.Value;
69
import org.springframework.context.annotation.Configuration;
710
import org.springframework.data.elasticsearch.client.ClientConfiguration;
11+
import org.springframework.data.elasticsearch.client.elc.ElasticsearchClients;
812
import org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration;
913
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
1014

@@ -42,6 +46,36 @@ public ClientConfiguration clientConfiguration() {
4246
.withBasicAuth(this.user, this.password)
4347
.withConnectTimeout(Duration.ofSeconds(connectTimeout))
4448
.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+
}))
4579
.build();
4680
}
4781

0 commit comments

Comments
 (0)