2 * ========================LICENSE_START=================================
5 * Copyright (C) 2021 Nordix Foundation
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.oran.dmaapadapter.tasks;
23 import java.util.HashMap;
28 import org.oran.dmaapadapter.configuration.ApplicationConfig;
29 import org.oran.dmaapadapter.repository.InfoType;
30 import org.oran.dmaapadapter.repository.InfoTypes;
31 import org.oran.dmaapadapter.repository.Job;
32 import org.oran.dmaapadapter.repository.Jobs;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.scheduling.annotation.EnableScheduling;
37 import org.springframework.scheduling.annotation.Scheduled;
38 import org.springframework.stereotype.Component;
40 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
43 public class KafkaTopicConsumers {
44 private static final Logger logger = LoggerFactory.getLogger(KafkaTopicConsumers.class);
46 private final Map<String, KafkaTopicListener> topicListeners = new HashMap<>(); // Key is typeId
49 private final Map<String, KafkaJobDataConsumer> consumers = new HashMap<>(); // Key is jobId
51 private static final int CONSUMER_SUPERVISION_INTERVAL_MS = 1000 * 60 * 3;
53 public KafkaTopicConsumers(@Autowired ApplicationConfig appConfig, @Autowired InfoTypes types,
54 @Autowired Jobs jobs) {
56 for (InfoType type : types.getAll()) {
57 if (type.isKafkaTopicDefined()) {
58 KafkaTopicListener topicConsumer = new KafkaTopicListener(appConfig, type);
59 topicListeners.put(type.getId(), topicConsumer);
63 jobs.addObserver(new Jobs.Observer() {
65 public void onJobbAdded(Job job) {
70 public void onJobRemoved(Job job) {
77 public synchronized void addJob(Job job) {
78 if (this.consumers.get(job.getId()) == null && job.getType().isKafkaTopicDefined()) {
79 logger.debug("Kafka job added {}", job.getId());
80 KafkaTopicListener topicConsumer = topicListeners.get(job.getType().getId());
81 KafkaJobDataConsumer subscription = new KafkaJobDataConsumer(job);
82 subscription.start(topicConsumer.getOutput());
83 consumers.put(job.getId(), subscription);
87 public synchronized void removeJob(Job job) {
88 KafkaJobDataConsumer d = consumers.remove(job.getId());
90 logger.debug("Kafka job removed {}", job.getId());
95 @Scheduled(fixedRate = CONSUMER_SUPERVISION_INTERVAL_MS)
96 public synchronized void restartNonRunningTasks() {
98 for (KafkaJobDataConsumer consumer : consumers.values()) {
99 if (!consumer.isRunning()) {
100 restartTopic(consumer);
105 private void restartTopic(KafkaJobDataConsumer consumer) {
106 InfoType type = consumer.getJob().getType();
107 KafkaTopicListener topic = this.topicListeners.get(type.getId());
109 restartConsumersOfType(topic, type);
112 private void restartConsumersOfType(KafkaTopicListener topic, InfoType type) {
113 this.consumers.forEach((jobId, consumer) -> {
114 if (consumer.getJob().getType().getId().equals(type.getId())) {
115 consumer.start(topic.getOutput());