X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=policy-agent%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fpolicyagent%2Fdmaap%2FDmaapMessageConsumerImpl.java;h=4a2605b6c55ba1f1b7519682d26a2b4fbd83e456;hb=1b8d76de3262675c5afe45351929b0d5d2c0bab1;hp=bd3ba69f9c43eee75cad948aaea5d26c5b044de4;hpb=3482ea81c8189d5cfacf4005e1c92673304e11b3;p=nonrtric.git diff --git a/policy-agent/src/main/java/org/oransc/policyagent/dmaap/DmaapMessageConsumerImpl.java b/policy-agent/src/main/java/org/oransc/policyagent/dmaap/DmaapMessageConsumerImpl.java index bd3ba69f..4a2605b6 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/dmaap/DmaapMessageConsumerImpl.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/dmaap/DmaapMessageConsumerImpl.java @@ -21,48 +21,56 @@ package org.oransc.policyagent.dmaap; import com.google.common.collect.Iterables; + +import java.io.FileNotFoundException; import java.io.IOException; +import java.time.Duration; import java.util.Properties; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; + +import org.onap.dmaap.mr.client.MRBatchingPublisher; import org.onap.dmaap.mr.client.MRClientFactory; import org.onap.dmaap.mr.client.MRConsumer; import org.onap.dmaap.mr.client.response.MRConsumerResponse; +import org.oransc.policyagent.clients.AsyncRestClient; import org.oransc.policyagent.configuration.ApplicationConfig; +import org.oransc.policyagent.exceptions.ServiceException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.scheduling.annotation.EnableScheduling; -import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component -@EnableScheduling -public class DmaapMessageConsumerImpl implements DmaapMessageConsumer { +public class DmaapMessageConsumerImpl implements Runnable { private static final Logger logger = LoggerFactory.getLogger(DmaapMessageConsumerImpl.class); - private boolean alive = false; + final Duration ERROR_TIMEOUT = Duration.ofSeconds(30); + final Duration TIME_BETWEEN_DMAAP_POLLS = Duration.ofSeconds(10); private final ApplicationConfig applicationConfig; - protected MRConsumer consumer; - @Autowired - private DmaapMessageHandler dmaapMessageHandler; - private final long FETCHTIMEOUT = 30000; - private CountDownLatch sleep = new CountDownLatch(1); + @Value("${server.port}") + private int localServerPort; @Autowired public DmaapMessageConsumerImpl(ApplicationConfig applicationConfig) { this.applicationConfig = applicationConfig; + + Thread thread = new Thread(this); + thread.start(); + } + + private boolean isDmaapConfigured() { + Properties consumerCfg = applicationConfig.getDmaapConsumerConfig(); + Properties producerCfg = applicationConfig.getDmaapPublisherConfig(); + return (consumerCfg != null && consumerCfg.size() > 0 && producerCfg != null && producerCfg.size() > 0); } - @Scheduled(fixedRate = 1000 * 10) @Override public void run() { - if (!alive) { - init(); - } - if (this.alive) { + while (sleep(TIME_BETWEEN_DMAAP_POLLS) && isDmaapConfigured()) { try { Iterable dmaapMsgs = fetchAllMessages(); if (dmaapMsgs != null && Iterables.size(dmaapMsgs) > 0) { @@ -73,65 +81,45 @@ public class DmaapMessageConsumerImpl implements DmaapMessageConsumer { } } catch (Exception e) { logger.error("{}: cannot fetch because of ", this, e.getMessage(), e); + sleep(ERROR_TIMEOUT); } } } - private Iterable fetchAllMessages() { - MRConsumerResponse response = null; - try { - response = consumer.fetchWithReturnConsumerResponse(); - } catch (Exception e) { - logger.error("Failed to get message from DMAAP", e); - } + private Iterable fetchAllMessages() throws ServiceException, FileNotFoundException, IOException { + Properties dmaapConsumerProperties = this.applicationConfig.getDmaapConsumerConfig(); + MRConsumer consumer = MRClientFactory.createConsumer(dmaapConsumerProperties); + MRConsumerResponse response = consumer.fetchWithReturnConsumerResponse(); if (response == null || !"200".equals(response.getResponseCode())) { - logger.warn("{}: DMaaP NULL response received", this); - sleepAfterFailure(); + throw new ServiceException("DMaaP NULL response received"); } else { logger.debug("DMaaP consumer received {} : {}", response.getResponseCode(), response.getResponseMessage()); - } - return response.getActualMessages(); - } - - @Override - public void init() { - Properties dmaapConsumerProperties = applicationConfig.getDmaapConsumerConfig(); - Properties dmaapPublisherProperties = applicationConfig.getDmaapPublisherConfig(); - // No need to start if there is no configuration. - if (dmaapConsumerProperties == null || dmaapPublisherProperties == null || dmaapConsumerProperties.size() == 0 - || dmaapPublisherProperties.size() == 0) { - logger.error("DMaaP properties Failed to Load"); - return; - } - try { - logger.debug("Creating DMAAP Client"); - logger.debug("dmaapConsumerProperties---> {}", dmaapConsumerProperties.getProperty("topic")); - logger.debug("dmaapPublisherProperties---> {}", dmaapPublisherProperties.getProperty("topic")); - consumer = MRClientFactory.createConsumer(dmaapConsumerProperties); - this.alive = true; - } catch (IOException e) { - logger.error("Exception occurred while creating Dmaap Consumer", e); + return response.getActualMessages(); } } - @Override - public void processMsg(String msg) throws Exception { + private void processMsg(String msg) throws Exception { logger.debug("Message Reveived from DMAAP : {}", msg); - // Call the concurrent Task executor to handle the incoming request - dmaapMessageHandler.handleDmaapMsg(msg); + createDmaapMessageHandler().handleDmaapMsg(msg); } - @Override - public boolean isAlive() { - return alive; + private DmaapMessageHandler createDmaapMessageHandler() throws FileNotFoundException, IOException { + String agentBaseUrl = "http://localhost:" + this.localServerPort; + AsyncRestClient agentClient = new AsyncRestClient(agentBaseUrl); + Properties dmaapPublisherProperties = applicationConfig.getDmaapPublisherConfig(); + MRBatchingPublisher producer = MRClientFactory.createBatchingPublisher(dmaapPublisherProperties); + + return new DmaapMessageHandler(producer, this.applicationConfig, agentClient); } - private void sleepAfterFailure() { - logger.warn("DMAAP message Consumer is put to Sleep for {} milliseconds", FETCHTIMEOUT); + private boolean sleep(Duration duration) { + CountDownLatch sleep = new CountDownLatch(1); try { - sleep.await(FETCHTIMEOUT, TimeUnit.MILLISECONDS); - } catch (InterruptedException e) { - logger.error("Failed to put the thread to sleep: {}", e); + sleep.await(duration.toMillis(), TimeUnit.MILLISECONDS); + return true; + } catch (Exception e) { + logger.error("msg", e); + return false; } } }