diff options
author | PatrikBuhr <patrik.buhr@est.tech> | 2019-04-04 14:41:34 +0000 |
---|---|---|
committer | PatrikBuhr <patrik.buhr@est.tech> | 2019-04-04 14:41:34 +0000 |
commit | 814ddd12e695433b8c6a760cc9424dc1c0bae4d1 (patch) | |
tree | e0d5d295e8419311f7f53b2baa0501e395150163 /datafile-dmaap-client/src/main/java | |
parent | d90535ce664cfde22e0670c950498a114388df99 (diff) |
Improved logging
Fixed problem with startup which lead
to that the REST API was not working running
locally.
Fixed problem with DmaapProducerHttpClient which would use
no timeout, which can lead to infinitly haninging threads.
A long timeout is used instead.
Change-Id: I28469b1b3aaad0dab4cf247bb8af968e71a60133
Issue-ID: DCAEGEN2-1305
Signed-off-by: PatrikBuhr <patrik.buhr@est.tech>
Diffstat (limited to 'datafile-dmaap-client/src/main/java')
-rw-r--r-- | datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/service/DmaapReactiveWebClient.java | 15 | ||||
-rw-r--r-- | datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerHttpClient.java (renamed from datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerReactiveHttpClient.java) | 30 |
2 files changed, 26 insertions, 19 deletions
diff --git a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/service/DmaapReactiveWebClient.java b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/service/DmaapReactiveWebClient.java index 21266fbc..23fd0bc7 100644 --- a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/service/DmaapReactiveWebClient.java +++ b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/service/DmaapReactiveWebClient.java @@ -16,9 +16,10 @@ package org.onap.dcaegen2.collectors.datafile.service; -import static org.onap.dcaegen2.collectors.datafile.model.logging.MdcVariables.RESPONSE_CODE; -import static org.onap.dcaegen2.collectors.datafile.model.logging.MdcVariables.SERVICE_NAME; +import static org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext.RESPONSE_CODE; +import static org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext.SERVICE_NAME; import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication; + import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapCustomConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,6 +28,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.web.reactive.function.client.ExchangeFilterFunction; import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient.Builder; + import reactor.core.publisher.Mono; /** @@ -57,12 +59,13 @@ public class DmaapReactiveWebClient { * @return WebClient */ public WebClient build() { - Builder webClientBuilder = WebClient.builder().defaultHeader(HttpHeaders.CONTENT_TYPE, dmaaPContentType) - .filter(logRequest()).filter(logResponse()); + Builder webClientBuilder = WebClient.builder() + .defaultHeader(HttpHeaders.CONTENT_TYPE, dmaaPContentType) // + .filter(logRequest()) // + .filter(logResponse()); if (dmaaPUserName != null && !dmaaPUserName.isEmpty() && dmaaPUserPassword != null && !dmaaPUserPassword.isEmpty()) { webClientBuilder.filter(basicAuthentication(dmaaPUserName, dmaaPUserPassword)); - } return webClientBuilder.build(); } @@ -81,7 +84,7 @@ public class DmaapReactiveWebClient { MDC.put(SERVICE_NAME, String.valueOf(clientRequest.url())); logger.trace("Request: {} {}", clientRequest.method(), clientRequest.url()); clientRequest.headers() - .forEach((name, values) -> values.forEach(value -> logger.info("{}={}", name, value))); + .forEach((name, values) -> values.forEach(value -> logger.trace("{}={}", name, value))); logger.trace("HTTP request headers: {}", clientRequest.headers()); MDC.remove(SERVICE_NAME); return Mono.just(clientRequest); diff --git a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerReactiveHttpClient.java b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerHttpClient.java index 944d3b34..b0904b29 100644 --- a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerReactiveHttpClient.java +++ b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerHttpClient.java @@ -20,6 +20,7 @@ import java.nio.charset.StandardCharsets; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; +import java.time.Duration; import java.util.Map; import java.util.concurrent.Future; @@ -35,11 +36,11 @@ import org.apache.http.ssl.SSLContextBuilder; import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException; import org.onap.dcaegen2.collectors.datafile.http.HttpAsyncClientBuilderWrapper; import org.onap.dcaegen2.collectors.datafile.http.IHttpAsyncClientBuilder; -import org.onap.dcaegen2.collectors.datafile.model.logging.MdcVariables; import org.onap.dcaegen2.collectors.datafile.web.PublishRedirectStrategy; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.MDC; import org.slf4j.Marker; import org.slf4j.MarkerFactory; import org.springframework.web.util.DefaultUriBuilderFactory; @@ -49,9 +50,9 @@ import org.springframework.web.util.UriBuilder; * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a> */ -public class DmaapProducerReactiveHttpClient { +public class DmaapProducerHttpClient { - private static final int NO_REQUEST_TIMEOUT = -1; + private static final Duration DEFAULT_REQUEST_TIMEOUT = Duration.ofMinutes(2); private static final Marker INVOKE = MarkerFactory.getMarker("INVOKE"); private static final Marker INVOKE_RETURN = MarkerFactory.getMarker("INVOKE_RETURN"); @@ -64,14 +65,14 @@ public class DmaapProducerReactiveHttpClient { * * @param dmaapPublisherConfiguration - DMaaP producer configuration object */ - public DmaapProducerReactiveHttpClient(DmaapPublisherConfiguration dmaapPublisherConfiguration) { + public DmaapProducerHttpClient(DmaapPublisherConfiguration dmaapPublisherConfiguration) { this.configuration = dmaapPublisherConfiguration; } public HttpResponse getDmaapProducerResponseWithRedirect(HttpUriRequest request, Map<String, String> contextMap) throws DatafileTaskException { - try (CloseableHttpAsyncClient webClient = createWebClient(true, NO_REQUEST_TIMEOUT)) { - MdcVariables.setMdcContextMap(contextMap); + MDC.setContextMap(contextMap); + try (CloseableHttpAsyncClient webClient = createWebClient(true, DEFAULT_REQUEST_TIMEOUT)) { webClient.start(); logger.trace(INVOKE, "Starting to produce to DR {}", request); @@ -84,10 +85,10 @@ public class DmaapProducerReactiveHttpClient { } } - public HttpResponse getDmaapProducerResponseWithCustomTimeout(HttpUriRequest request, int requestTimeout, + public HttpResponse getDmaapProducerResponseWithCustomTimeout(HttpUriRequest request, Duration requestTimeout, Map<String, String> contextMap) throws DatafileTaskException { + MDC.setContextMap(contextMap); try (CloseableHttpAsyncClient webClient = createWebClient(false, requestTimeout)) { - MdcVariables.setMdcContextMap(contextMap); webClient.start(); logger.trace(INVOKE, "Starting to produce to DR {}", request); @@ -116,7 +117,7 @@ public class DmaapProducerReactiveHttpClient { .port(configuration.dmaapPortNumber()); } - private CloseableHttpAsyncClient createWebClient(boolean expectRedirect, int requestTimeout) + private CloseableHttpAsyncClient createWebClient(boolean expectRedirect, Duration requestTimeout) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException { SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (certificate, authType) -> true).build(); @@ -129,14 +130,17 @@ public class DmaapProducerReactiveHttpClient { clientBuilder.setRedirectStrategy(PublishRedirectStrategy.INSTANCE); } - if (requestTimeout > NO_REQUEST_TIMEOUT) { + if (requestTimeout.toMillis() > 0) { + int millis = (int)requestTimeout.toMillis(); RequestConfig requestConfig = RequestConfig.custom() // - .setSocketTimeout(requestTimeout) // - .setConnectTimeout(requestTimeout) // - .setConnectionRequestTimeout(requestTimeout) // + .setSocketTimeout(millis) // + .setConnectTimeout(millis) // + .setConnectionRequestTimeout(millis) // .build(); clientBuilder.setDefaultRequestConfig(requestConfig); + } else { + logger.error("WEB client without timeout created {}", requestTimeout); } return clientBuilder.build(); |