2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 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.oransc.policyagent.dmaap;
23 import java.io.IOException;
24 import java.util.Properties;
25 import org.onap.dmaap.mr.client.MRClientFactory;
26 import org.onap.dmaap.mr.client.MRConsumer;
27 import org.onap.dmaap.mr.client.response.MRConsumerResponse;
28 import org.oransc.policyagent.configuration.ApplicationConfig;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.scheduling.annotation.EnableScheduling;
33 import org.springframework.scheduling.annotation.Scheduled;
34 import org.springframework.stereotype.Component;
38 public class DmaapMessageConsumerImpl implements DmaapMessageConsumer {
40 private static final Logger logger = LoggerFactory.getLogger(DmaapMessageConsumerImpl.class);
42 private boolean alive = false;
43 private final ApplicationConfig applicationConfig;
44 protected MRConsumer consumer;
45 private MRConsumerResponse response = null;
47 private DmaapMessageHandler dmaapMessageHandler;
50 public DmaapMessageConsumerImpl(ApplicationConfig applicationConfig) {
51 this.applicationConfig = applicationConfig;
54 @Scheduled(fixedRate = 1000 * 10) // , initialDelay=60000)
62 Iterable<String> dmaapMsgs = fetchAllMessages();
63 logger.debug("Fetched all the messages from DMAAP and will start to process the messages");
64 for (String msg : dmaapMsgs) {
67 } catch (Exception e) {
68 logger.error("{}: cannot fetch because of ", this, e.getMessage(), e);
73 private Iterable<String> fetchAllMessages() {
74 response = consumer.fetchWithReturnConsumerResponse();
75 if (response == null) {
76 logger.warn("{}: DMaaP NULL response received", this);
78 logger.debug("DMaaP consumer received {} : {}", response.getResponseCode(), response.getResponseMessage());
79 if (!"200".equals(response.getResponseCode())) {
80 logger.error("DMaaP consumer received: {} : {}", response.getResponseCode(),
81 response.getResponseMessage());
84 return response.getActualMessages();
87 // Properties are not loaded in first atempt. Need to fix this and then uncomment the post construct annotation
91 Properties dmaapConsumerProperties = applicationConfig.getDmaapConsumerConfig();
92 Properties dmaapPublisherProperties = applicationConfig.getDmaapPublisherConfig();
93 // No need to start if there is no configuration.
94 if (dmaapConsumerProperties == null || dmaapPublisherProperties == null || dmaapConsumerProperties.size() == 0
95 || dmaapPublisherProperties.size() == 0) {
96 logger.error("DMaaP properties Failed to Load");
100 logger.debug("Creating DMAAP Client");
101 System.out.println("dmaapConsumerProperties--->"+dmaapConsumerProperties.getProperty("topic"));
102 System.out.println("dmaapPublisherProperties--->"+dmaapPublisherProperties.getProperty("topic"));
103 consumer = MRClientFactory.createConsumer(dmaapConsumerProperties);
105 } catch (IOException e) {
106 logger.error("Exception occurred while creating Dmaap Consumer", e);
111 public void processMsg(String msg) throws Exception {
112 logger.debug("Message Reveived from DMAAP : {}", msg);
113 // Call the concurrent Task executor to handle the incoming request
114 dmaapMessageHandler.handleDmaapMsg(msg);
118 public boolean isAlive() {
123 public void stopConsumer() {