blob: 7986869b9e5a785da085099c402834069f7488d2 (
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
|
/*-
* ============LICENSE_START=======================================================
* SDC
* ================================================================================
* Copyright (C) 2022 Nordix Foundation. 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.onap.dmaap.kafka;
import java.util.ArrayList;
import java.util.List;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.apache.kafka.common.KafkaException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Utility class that provides a handler for Kafka interactions
*/
public class OnapKafkaClient {
private final Logger log = LoggerFactory.getLogger(OnapKafkaClient.class.getName());
private OnapKafkaConsumer onapKafkaConsumer = null;
private final OnapKafkaProducer onapKafkaProducer;
public OnapKafkaClient(IKafkaConfig configuration) {
if (!configuration.getConsumerTopics().isEmpty()) {
onapKafkaConsumer = new OnapKafkaConsumer(configuration);
onapKafkaConsumer.subscribeConsumerToTopics();
}
onapKafkaProducer = new OnapKafkaProducer(configuration);
}
/**
* @param topicName The topic from which messages will be fetched
* @return A list of messages from a specific topic
*/
public List<String> fetchFromTopic(String topicName) {
List<String> messages = new ArrayList<>();
if (onapKafkaConsumer != null) {
try {
log.debug("Polling for messages from topic: {}", topicName);
messages = onapKafkaConsumer.poll(topicName);
log.debug("Returning messages from topic {}", topicName);
return messages;
} catch (KafkaException e) {
log.error("Failed to fetch from kafka for topic: {}", topicName, e);
}
} else {
log.error("Consumer has not been initialised with the required topic list");
}
return messages;
}
/**
* Publish data to a given topic
* @param topicName The topic to which the message should be published
* @param data The data to publish to the topic specified
* @return
*/
public RecordMetadata publishToTopic(String topicName, String data) {
// Should we check the data size and chunk it if necessary? Do we need to?
return onapKafkaProducer.sendDataSynch(topicName, data);
}
}
|