Replaced all tabs with spaces in java and pom.xml
[so.git] / common / src / main / java / org / onap / so / client / dmaap / DmaapConsumer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.client.dmaap;
22
23 import com.google.common.base.Stopwatch;
24 import java.io.IOException;
25 import java.util.concurrent.TimeUnit;
26 import org.onap.so.client.dmaap.exceptions.DMaaPConsumerFailure;
27 import org.onap.so.client.dmaap.exceptions.ExceededMaximumPollingTime;
28 import org.onap.so.client.dmaap.rest.RestConsumer;
29
30 public abstract class DmaapConsumer extends DmaapClient {
31
32     public DmaapConsumer() throws IOException {
33         super("dmaap/default-consumer.properties");
34     }
35
36     public Consumer getConsumer() {
37         return new RestConsumer(this.properties);
38     }
39
40     public boolean consume() throws Exception {
41         Consumer mrConsumer = this.getConsumer();
42         boolean accepted = false;
43         Stopwatch stopwatch = Stopwatch.createUnstarted();
44         try {
45             while (this.continuePolling()) {
46                 if (stopwatch.elapsed(TimeUnit.MILLISECONDS) >= this.getMaximumElapsedTime()) {
47                     final String message =
48                             "exceeded maximum retries on " + this.getRequestId() + " on " + this.getTopic();
49                     logger.error(message);
50                     throw new ExceededMaximumPollingTime(message);
51                 }
52                 stopwatch.start();
53                 Iterable<String> itr = mrConsumer.fetch();
54                 stopwatch.stop();
55                 for (String message : itr) {
56                     if (!accepted && this.isAccepted(message)) {
57                         logger.info("accepted message found for " + this.getRequestId() + " on " + this.getTopic());
58                         accepted = true;
59                     }
60                     if (accepted) {
61                         logger.info("received dmaap message: " + message);
62                         if (this.isFailure(message)) {
63                             this.stopProcessingMessages();
64                             final String errorMsg = "failure received from dmaap topic " + this.getTopic();
65                             logger.error(errorMsg);
66                             throw new DMaaPConsumerFailure(errorMsg);
67                         } else {
68                             this.processMessage(message);
69                         }
70                     }
71                 }
72             }
73             return true;
74         } finally {
75             if (stopwatch.isRunning()) {
76                 stopwatch.stop();
77             }
78         }
79     }
80
81     /**
82      * Should this consumer continue to consume messages from the topic?
83      * 
84      * @return
85      */
86     public abstract boolean continuePolling();
87
88     /**
89      * Process a message from a DMaaP topic
90      *
91      * @param message
92      * @throws Exception
93      */
94     public abstract void processMessage(String message) throws Exception;
95
96     /**
97      * Has the request been accepted by the receiving system? Should the consumer move to processing messages?
98      *
99      * @param message
100      * @return
101      */
102     public abstract boolean isAccepted(String message);
103
104     /**
105      * has the request failed?
106      *
107      * @param message
108      * @return
109      */
110     public abstract boolean isFailure(String message);
111
112     /**
113      * The request id to filter messages on
114      * 
115      * @return
116      */
117     public abstract String getRequestId();
118
119     /**
120      * Logic that defines when the consumer should stop processing messages
121      */
122     public abstract void stopProcessingMessages();
123
124     /**
125      * time in milliseconds
126      */
127     public int getMaximumElapsedTime() {
128         return 180000;
129     }
130
131
132
133 }