2 * ========================LICENSE_START=================================
5 * Copyright (C) 2021 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 package org.oran.dmaapadapter.tasks;
23 import java.time.Duration;
25 import org.oran.dmaapadapter.clients.AsyncRestClient;
26 import org.oran.dmaapadapter.clients.AsyncRestClientFactory;
27 import org.oran.dmaapadapter.configuration.ApplicationConfig;
28 import org.oran.dmaapadapter.repository.InfoType;
29 import org.oran.dmaapadapter.repository.Jobs;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import reactor.core.publisher.Flux;
34 import reactor.core.publisher.FluxSink;
35 import reactor.core.publisher.Mono;
38 * The class fetches incoming requests from DMAAP and sends them further to the
39 * consumers that has a job for this InformationType.
42 public class DmaapMessageConsumer {
43 private static final Duration TIME_BETWEEN_DMAAP_RETRIES = Duration.ofSeconds(10);
44 private static final Logger logger = LoggerFactory.getLogger(DmaapMessageConsumer.class);
45 private final ApplicationConfig applicationConfig;
46 private final AsyncRestClient restClient;
47 private final InfoType type;
48 private final Jobs jobs;
49 private final InfiniteFlux infiniteSubmitter = new InfiniteFlux();
51 /** Submits new elements until stopped */
52 private static class InfiniteFlux {
53 private FluxSink<Integer> sink;
54 private int counter = 0;
56 public synchronized Flux<Integer> start() {
58 return Flux.create(this::next).doOnRequest(this::onRequest);
61 public synchronized void stop() {
62 if (this.sink != null) {
68 void onRequest(long no) {
69 logger.debug("InfiniteFlux.onRequest {}", no);
70 for (long i = 0; i < no; ++i) {
75 void next(FluxSink<Integer> sink) {
76 logger.debug("InfiniteFlux.next");
82 public DmaapMessageConsumer(ApplicationConfig applicationConfig, InfoType type, Jobs jobs) {
83 this.applicationConfig = applicationConfig;
84 AsyncRestClientFactory restclientFactory = new AsyncRestClientFactory(applicationConfig.getWebClientConfig());
85 this.restClient = restclientFactory.createRestClientNoHttpProxy("");
91 infiniteSubmitter.stop();
93 createTask().subscribe(//
94 value -> logger.debug("DmaapMessageConsumer next: {}", value), //
95 throwable -> logger.error("DmaapMessageConsumer error: {}", throwable.getMessage()), //
96 () -> logger.warn("DmaapMessageConsumer stopped") //
100 protected Flux<String> createTask() {
101 final int CONCURRENCY = 5;
102 return infiniteSubmitter.start() //
103 .flatMap(notUsed -> getFromMessageRouter(getDmaapUrl()), 1) //
104 .doOnNext(message -> logger.debug("Message Reveived from DMAAP : {}", message)) //
105 .flatMap(this::handleReceivedMessage, CONCURRENCY);
108 private String getDmaapUrl() {
109 return this.applicationConfig.getDmaapBaseUrl() + type.getDmaapTopicUrl();
112 private Mono<String> handleErrorResponse(Throwable t) {
113 logger.debug("error from DMAAP {}", t.getMessage());
114 return Mono.delay(TIME_BETWEEN_DMAAP_RETRIES) //
115 .flatMap(notUsed -> Mono.empty());
118 protected Mono<String> getFromMessageRouter(String topicUrl) {
119 logger.trace("getFromMessageRouter {}", topicUrl);
120 return restClient.get(topicUrl) //
121 .onErrorResume(this::handleErrorResponse);
124 protected Flux<String> handleReceivedMessage(String body) {
125 logger.debug("Received from DMAAP {}", body);
126 final int CONCURRENCY = 5;
128 // Distibute the body to all jobs for this type
129 return Flux.fromIterable(this.jobs.getJobsForType(this.type)) //
130 .doOnNext(job -> logger.debug("Sending to consumer {}", job.getCallbackUrl()))
131 .flatMap(job -> restClient.post(job.getCallbackUrl(), body), CONCURRENCY) //
132 .onErrorResume(this::handleErrorResponse);