aboutsummaryrefslogtreecommitdiffstats
path: root/dmaap-listener/src/main/java/org/openecomp/sdnc/dmaapclient/SdncDmaapConsumer.java
blob: 1472671d8ffcbbab5e7f9881b12c991a4c0f63ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : SDN-C
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 * 							reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdnc.dmaapclient;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.att.nsa.mr.client.MRClientFactory;
import com.att.nsa.mr.client.MRConsumer;
import com.att.nsa.mr.client.response.MRConsumerResponse;

public abstract class SdncDmaapConsumer implements Runnable {

	private static final Logger LOG = LoggerFactory
			.getLogger(SdncDmaapConsumer.class);

	private String propertiesPath = "";
	private Properties properties = null;
	MRConsumer consumer = null;
	MRConsumerResponse consumerResponse = null;
	boolean running = false;
	boolean ready = false;
	int fetchPause = 5000; // Default pause between fetchs - 5 seconds

	public boolean isReady() {
		return ready;
	}

	int timeout = 15000; // Default timeout - 15 seconds

	public boolean isRunning() {
		return running;
	}

	public SdncDmaapConsumer() {

	}

	public SdncDmaapConsumer(Properties properties, String propertiesPath) {
		init(properties, propertiesPath);
	}

	public String getProperty(String name) {
		return(properties.getProperty(name, ""));
	}

	public void init(Properties properties, String propertiesPath) {

		this.propertiesPath = propertiesPath;

		try {

			this.properties = (Properties) properties.clone();

			this.properties.load(new FileInputStream(new File(propertiesPath)));

			String timeoutStr = properties.getProperty("timeout");

			if ((timeoutStr != null) && (timeoutStr.length() > 0)) {
				try {
					timeout = Integer.parseInt(timeoutStr);
				} catch (NumberFormatException e) {
					LOG.error("Non-numeric value specified for timeout ("+timeoutStr+")");
				}
			}

			String fetchPauseStr = properties.getProperty("fetchPause");
			if ((fetchPauseStr != null) && (fetchPauseStr.length() > 0)) {
				try {
					fetchPause = Integer.parseInt(fetchPauseStr);
				} catch (NumberFormatException e) {
					LOG.error("Non-numeric valud specified for fetchPause ("+fetchPauseStr+")");
				}
			}

			this.consumer = MRClientFactory.createConsumer(propertiesPath);
			ready = true;
		} catch (Exception e) {
			LOG.error("Error initializing DMaaP consumer from file "+propertiesPath, e);
		}
	}


	@Override
	public void run() {
		if (ready) {

			running = true;

			while (running) {

				try {
					boolean noData = true;
					consumerResponse = consumer.fetchWithReturnConsumerResponse(timeout, -1);
					for (String msg : consumerResponse.getActualMessages()) {
						noData = false;
						LOG.info("Received message from DMaaP:\n"+msg);
						processMsg(msg);
					}

					if (noData) {
						if (fetchPause > 0) {

							LOG.info("No data received from fetch.  Pausing "+fetchPause+" ms before retry");
							Thread.sleep(fetchPause);
						} else {

							LOG.info("No data received from fetch.  No fetch pause specified - retrying immediately");
						}
					}
				} catch (Exception e) {
					LOG.error("Caught exception reading from DMaaP", e);
					running = false;
				}


			}
		}

	}

	abstract public void processMsg(String msg) throws InvalidMessageException;
}