/*
- * Copyright 2017 ZTE Corporation.
+ * Copyright 2017 - 2022 ZTE Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*/
package org.onap.holmes.engine.dmaap;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import javax.annotation.PreDestroy;
-import javax.inject.Inject;
+import jakarta.annotation.PreDestroy;
import lombok.extern.slf4j.Slf4j;
-import org.jvnet.hk2.annotations.Service;
+import org.onap.holmes.common.database.DbDaoUtil;
import org.onap.holmes.dsa.dmaappolling.Subscriber;
+import org.onap.holmes.engine.db.AlarmInfoDaoService;
+import org.onap.holmes.engine.db.jdbi.AlarmInfoDao;
import org.onap.holmes.engine.manager.DroolsEngine;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
-@Service
+@Component
@Slf4j
public class SubscriberAction {
- @Inject
+ @Autowired
private DroolsEngine droolsEngine;
+ @Autowired
+ private DbDaoUtil daoUtil;
+ @Autowired
+ private AlarmInfoDaoService alarmInfoDaoService;
+
private HashMap<String, DMaaPAlarmPolling> pollingTasks = new HashMap<>();
public synchronized void addSubscriber(Subscriber subscriber) {
- if (!pollingTasks.containsKey(subscriber.getTopic())) {
- DMaaPAlarmPolling pollingTask = new DMaaPAlarmPolling(subscriber, droolsEngine);
+ String topic = subscriber.getTopic();
+ if (topic != null) {
+ if (pollingTasks.containsKey(topic)) {
+ removeSubscriber(subscriber);
+ }
+ AlarmInfoDao alarmInfoDao = daoUtil.getJdbiDaoByOnDemand(AlarmInfoDao.class);
+ DMaaPAlarmPolling pollingTask = new DMaaPAlarmPolling(subscriber, droolsEngine, alarmInfoDaoService);
Thread thread = new Thread(pollingTask);
thread.start();
- pollingTasks.put(subscriber.getTopic(), pollingTask);
- log.info("Subscribe to topic: " + subscriber.getUrl());
+ pollingTasks.put(topic, pollingTask);
+ log.info("Subscribed to topic: " + subscriber.getUrl());
+ } else {
+ log.info("The topic is null. Operation aborted.");
}
}
public synchronized void removeSubscriber(Subscriber subscriber) {
- if (pollingTasks.containsKey(subscriber.getTopic())) {
- pollingTasks.get(subscriber.getTopic()).stopTask();
- pollingTasks.remove(subscriber.getTopic());
+ String topic = subscriber.getTopic();
+ if (topic != null && pollingTasks.containsKey(topic)) {
+ pollingTasks.get(topic).stopTask();
+ pollingTasks.remove(topic);
}
log.info("Topic unsubscribed: " + subscriber.getUrl());
}
public void stopPollingTasks() {
Iterator iterator = pollingTasks.entrySet().iterator();
while (iterator.hasNext()) {
- Map.Entry entry = (Map.Entry)iterator.next();
+ Map.Entry entry = (Map.Entry) iterator.next();
String key = (String) entry.getKey();
pollingTasks.get(key).stopTask();
}