X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=policy-agent%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fpolicyagent%2Fdmaap%2FDmaapMessageConsumerImpl.java;h=4a2605b6c55ba1f1b7519682d26a2b4fbd83e456;hb=1b8d76de3262675c5afe45351929b0d5d2c0bab1;hp=c7dab584b9fb13ae1724e9e6127319e2d95e7a26;hpb=296116b25bb7ef0971dc7234554436b2e2b090e6;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 c7dab584..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 @@ -20,105 +20,106 @@ 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; - private MRConsumerResponse response = null; - @Autowired - private DmaapMessageHandler dmaapMessageHandler; + + @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) // , initialDelay=60000) @Override public void run() { - if (!alive) { - init(); - } - if (this.alive) { + while (sleep(TIME_BETWEEN_DMAAP_POLLS) && isDmaapConfigured()) { try { Iterable dmaapMsgs = fetchAllMessages(); - logger.debug("Fetched all the messages from DMAAP and will start to process the messages"); - for (String msg : dmaapMsgs) { - processMsg(msg); + if (dmaapMsgs != null && Iterables.size(dmaapMsgs) > 0) { + logger.debug("Fetched all the messages from DMAAP and will start to process the messages"); + for (String msg : dmaapMsgs) { + processMsg(msg); + } } } catch (Exception e) { logger.error("{}: cannot fetch because of ", this, e.getMessage(), e); + sleep(ERROR_TIMEOUT); } } } - private Iterable fetchAllMessages() { - response = consumer.fetchWithReturnConsumerResponse(); - if (response == null) { - logger.warn("{}: DMaaP NULL response received", this); + 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())) { + throw new ServiceException("DMaaP NULL response received"); } else { logger.debug("DMaaP consumer received {} : {}", response.getResponseCode(), response.getResponseMessage()); - if (!"200".equals(response.getResponseCode())) { - logger.error("DMaaP consumer received: {} : {}", response.getResponseCode(), - response.getResponseMessage()); - } + return response.getActualMessages(); } - 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); - } - } - - @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); } - @Override - public void stopConsumer() { - alive = false; + private boolean sleep(Duration duration) { + CountDownLatch sleep = new CountDownLatch(1); + try { + sleep.await(duration.toMillis(), TimeUnit.MILLISECONDS); + return true; + } catch (Exception e) { + logger.error("msg", e); + return false; + } } }