Running Dmaap consumer in a seprate thread
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / dmaap / DmaapMessageConsumerImpl.java
index 2ae5e5e..4a2605b 100644 (file)
 
 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 javax.annotation.PostConstruct;
+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<String> 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<String> fetchAllMessages() {
-        response = consumer.fetchWithReturnConsumerResponse();
-        if (response == null) {
-            logger.warn("{}: DMaaP NULL response received", this);
+    private Iterable<String> 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();
-    }
-
-    @PostConstruct
-    @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");
-            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);
     }
 
-    @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;
+        }
     }
 }