William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 1 | /* |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame^] | 2 | * Copyright 2018- Cisco |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 16 | package kafka; |
| 17 | import org.apache.kafka.clients.consumer.*; |
| 18 | import org.apache.kafka.clients.consumer.Consumer; |
| 19 | import org.apache.kafka.common.serialization.LongDeserializer; |
| 20 | import org.apache.kafka.common.serialization.StringDeserializer; |
| 21 | import org.apache.kafka.common.KafkaException; |
| 22 | import java.util.Collections; |
| 23 | import java.util.Properties; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | import org.slf4j.Marker; |
| 27 | import org.slf4j.MarkerFactory; |
| 28 | |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame^] | 29 | import java.time.Instant; |
| 30 | import java.time.Duration; |
| 31 | |
| 32 | import com.google.gson.JsonSyntaxException; |
| 33 | |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 34 | import javax.xml.ws.http.HTTPException; |
| 35 | |
| 36 | import java.util.concurrent.TimeUnit; |
| 37 | |
| 38 | import ves.*; |
| 39 | import config.Config; |
| 40 | |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame^] | 41 | |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 42 | public class VolthaKafkaConsumer { |
| 43 | |
| 44 | private final Logger logger = LoggerFactory.getLogger("VolthaKafkaConsumer"); |
| 45 | private final String dataMarkerText = "DATA"; |
| 46 | private final Marker dataMarker = MarkerFactory.getMarker(dataMarkerText); |
| 47 | |
| 48 | private KafkaConsumer<Long, String> consumer; |
| 49 | |
| 50 | public VolthaKafkaConsumer() { |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame^] | 51 | logger.debug("VolthaKafkaConsumer constructor called"); |
| 52 | initVesAgent(); |
| 53 | try { |
| 54 | consumer = createConsumer(); |
| 55 | } catch (KafkaException e) { |
| 56 | logger.error("Error with Kafka connection. Retrying in 15 seconds."); |
| 57 | //Don't try to resolve it here. Try again in the thread loo, in case this is a temporal issue |
| 58 | } |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | private void initVesAgent() { |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame^] | 62 | VesAgent.initVes(); |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | private KafkaConsumer<Long, String> createConsumer() { |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame^] | 66 | logger.debug("Creating Kafka Consumer"); |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 67 | |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame^] | 68 | String kafkaAddress = Config.getKafkaAddress() + ":" + Config.getKafkaPort(); |
| 69 | final Properties props = new Properties(); |
| 70 | props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, |
| 71 | kafkaAddress); |
| 72 | props.put(ConsumerConfig.GROUP_ID_CONFIG, |
| 73 | "KafkaExampleConsumer"); |
| 74 | props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, |
| 75 | LongDeserializer.class.getName()); |
| 76 | props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, |
| 77 | StringDeserializer.class.getName()); |
| 78 | props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, |
| 79 | false); |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 80 | |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame^] | 81 | // Create the consumer using props. |
| 82 | final KafkaConsumer<Long, String> consumer = |
| 83 | new KafkaConsumer<>(props); |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 84 | |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame^] | 85 | // Subscribe to the topic. |
| 86 | consumer.subscribe(Collections.singletonList(Config.getKafkaTopic())); |
| 87 | return consumer; |
| 88 | } |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 89 | |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame^] | 90 | public void runConsumer() throws InterruptedException { |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 91 | |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame^] | 92 | logger.debug("Starting Consumer"); |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 93 | |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame^] | 94 | while (true) { |
| 95 | ConsumerRecords<Long, String> consumerRecords; |
| 96 | try { |
| 97 | if (consumer == null) { |
| 98 | this.consumer = createConsumer(); |
| 99 | } |
| 100 | consumerRecords = consumer.poll(20000); |
| 101 | } catch (KafkaException e) { |
| 102 | logger.error("Error with Kafka connection. Retrying in 15 seconds."); |
| 103 | consumer = null; |
| 104 | TimeUnit.SECONDS.sleep(15); |
| 105 | continue; |
| 106 | } |
| 107 | logger.info("{} Records retrieved from poll.", consumerRecords.count()); |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 108 | |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame^] | 109 | boolean commit = true; |
| 110 | try { |
| 111 | consumerRecords.forEach(record -> { |
| 112 | Instant start = Instant.now(); |
| 113 | logger.info(dataMarker, "Consumer Record:({}, {}, {}, {})\n", |
| 114 | record.key(), record.value(), |
| 115 | record.partition(), record.offset()); |
| 116 | logger.info("Attempting to send data to VES"); |
| 117 | boolean success = VesAgent.sendToVES(record.value()); |
| 118 | if (!success) { |
| 119 | throw new HTTPException(0); |
| 120 | } else { |
| 121 | Instant finish = Instant.now(); |
| 122 | logger.info("Sent Ves Message. Took " + Duration.between(start, finish).toMillis() + " Milliseconds."); |
| 123 | } |
| 124 | }); |
| 125 | } catch (HTTPException e) { |
| 126 | logger.info("Ves message failed. Going back to polling."); |
| 127 | commit = false; |
| 128 | } catch (JsonSyntaxException e) { |
| 129 | logger.error("Json Syntax Exception: ", e); |
| 130 | } |
| 131 | if (commit) { |
| 132 | consumer.commitAsync(); |
| 133 | } |
| 134 | } |
| 135 | //consumer.close(); |
| 136 | //logger.debug("DONE"); |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | } |