2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
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.openecomp.mso.client.dmaap;
23 import com.google.common.base.Stopwatch;
24 import java.io.IOException;
25 import java.util.concurrent.TimeUnit;
26 import org.openecomp.mso.client.dmaap.exceptions.DMaaPConsumerFailure;
27 import org.openecomp.mso.client.dmaap.exceptions.ExceededMaximumPollingTime;
28 import org.openecomp.mso.client.dmaap.rest.RestConsumer;
30 public abstract class DmaapConsumer extends DmaapClient {
32 public DmaapConsumer() throws IOException {
33 super("dmaap/default-consumer.properties");
36 public Consumer getConsumer() {
37 return new RestConsumer(this.properties);
40 public boolean consume() throws Exception {
41 Consumer mrConsumer = this.getConsumer();
42 boolean accepted = false;
43 Stopwatch stopwatch = Stopwatch.createUnstarted();
45 while (this.continuePolling()) {
46 if (stopwatch.elapsed(TimeUnit.MILLISECONDS) >= this.getMaximumElapsedTime()) {
47 final String message = "exceeded maximum retries on " + this.getRequestId() + " on " + this.getTopic();
48 auditLogger.error(message);
49 throw new ExceededMaximumPollingTime(message);
52 Iterable<String> itr = mrConsumer.fetch();
54 for (String message : itr) {
55 if (!accepted && this.isAccepted(message)) {
56 auditLogger.info("accepted message found for " + this.getRequestId() + " on " + this.getTopic());
60 auditLogger.info("received dmaap message: " + message);
61 if (this.isFailure(message)) {
62 this.stopProcessingMessages();
63 final String errorMsg = "failure received from dmaap topic " + this.getTopic();
64 auditLogger.error(errorMsg);
65 throw new DMaaPConsumerFailure(errorMsg);
67 this.processMessage(message);
74 if (stopwatch.isRunning()) {
81 * Should this consumer continue to consume messages from the topic?
84 public abstract boolean continuePolling();
86 * Process a message from a DMaaP topic
91 public abstract void processMessage(String message) throws Exception;
93 * Has the request been accepted by the receiving system?
94 * Should the consumer move to processing messages?
99 public abstract boolean isAccepted(String message);
101 * has the request failed?
106 public abstract boolean isFailure(String message);
108 * The request id to filter messages on
111 public abstract String getRequestId();
113 * Logic that defines when the consumer should stop processing messages
115 public abstract void stopProcessingMessages();
118 * time in milliseconds
120 public int getMaximumElapsedTime() {