2 * Copyright 2017 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.holmes.engine.dmaap;
18 import java.util.HashMap;
19 import java.util.Iterator;
21 import java.util.concurrent.ConcurrentHashMap;
22 import javax.annotation.PreDestroy;
23 import javax.inject.Inject;
24 import lombok.extern.slf4j.Slf4j;
25 import org.jvnet.hk2.annotations.Service;
26 import org.onap.holmes.common.utils.DbDaoUtil;
27 import org.onap.holmes.dsa.dmaappolling.Subscriber;
28 import org.onap.holmes.engine.db.AlarmInfoDao;
29 import org.onap.holmes.engine.manager.DroolsEngine;
33 public class SubscriberAction {
36 private DroolsEngine droolsEngine;
38 private DbDaoUtil daoUtil;
39 private HashMap<String, DMaaPAlarmPolling> pollingTasks = new HashMap<>();
41 public synchronized void addSubscriber(Subscriber subscriber) {
42 String topic = subscriber.getTopic();
43 if (topic != null && !pollingTasks.containsKey(topic)) {
44 AlarmInfoDao alarmInfoDao = daoUtil.getJdbiDaoByOnDemand(AlarmInfoDao.class);
45 DMaaPAlarmPolling pollingTask = new DMaaPAlarmPolling(subscriber, droolsEngine, alarmInfoDao);
46 Thread thread = new Thread(pollingTask);
48 pollingTasks.put(topic, pollingTask);
49 log.info("Subscribe to topic: " + subscriber.getUrl());
53 public synchronized void removeSubscriber(Subscriber subscriber) {
54 String topic = subscriber.getTopic();
55 if (topic != null && pollingTasks.containsKey(topic)) {
56 pollingTasks.get(topic).stopTask();
57 pollingTasks.remove(topic);
59 log.info("Topic unsubscribed: " + subscriber.getUrl());
63 public void stopPollingTasks() {
64 Iterator iterator = pollingTasks.entrySet().iterator();
65 while (iterator.hasNext()) {
66 Map.Entry entry = (Map.Entry)iterator.next();
67 String key = (String) entry.getKey();
68 pollingTasks.get(key).stopTask();