+ private boolean alive = false;
+ private final ApplicationConfig applicationConfig;
+ protected MRConsumer consumer;
+ private MRBatchingPublisher producer;
+
+ @Value("${server.port}")
+ private int localServerPort;
+
+ @Autowired
+ public DmaapMessageConsumerImpl(ApplicationConfig applicationConfig) {
+ this.applicationConfig = applicationConfig;
+ }
+
+ @Scheduled(fixedRate = 1000 * 40)
+ @Override
+ public void run() {
+ if (!alive) {
+ init();
+ }
+ if (this.alive) {
+ try {
+ Iterable<String> dmaapMsgs = fetchAllMessages();
+ 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);
+ }
+ }
+ }
+
+ private Iterable<String> fetchAllMessages() {
+ MRConsumerResponse response = null;
+ try {
+ response = consumer.fetchWithReturnConsumerResponse();
+ } catch (Exception e) {
+ logger.error("Failed to get message from DMAAP", e);
+ }
+ if (response == null || !"200".equals(response.getResponseCode())) {
+ logger.warn("{}: DMaaP NULL response received", this);
+ } 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);
+ producer = MRClientFactory.createBatchingPublisher(dmaapPublisherProperties);
+ this.alive = true;
+ } catch (IOException e) {
+ logger.error("Exception occurred while creating Dmaap Consumer", e);
+ }
+ }
+
+ @Override
+ public void processMsg(String msg) throws Exception {
+ logger.debug("Message Reveived from DMAAP : {}", msg);
+ createDmaapMessageHandler().handleDmaapMsg(msg);
+ }
+
+ protected DmaapMessageHandler createDmaapMessageHandler() {
+ String agentBaseUrl = "http://localhost:" + this.localServerPort;
+ AsyncRestClient agentClient = new AsyncRestClient(agentBaseUrl);
+ return new DmaapMessageHandler(this.producer, this.applicationConfig, agentClient);
+ }
+
+ @Override
+ public boolean isAlive() {
+ return alive;
+ }