modify not publish messages to DMaaP
[holmes/engine-management.git] / engine-d / src / main / java / org / onap / holmes / engine / dmaappolling / SubscriberAction.java
1 /*
2  * Copyright 2017 ZTE Corporation.
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  */
16 package org.onap.holmes.engine.dmaappolling;
17
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.ScheduledExecutorService;
21 import java.util.concurrent.ScheduledFuture;
22 import java.util.concurrent.TimeUnit;
23 import javax.inject.Inject;
24 import org.jvnet.hk2.annotations.Service;
25 import org.onap.holmes.dsa.dmaappolling.Subscriber;
26 import org.onap.holmes.engine.manager.DroolsEngine;
27
28 @Service
29 public class SubscriberAction {
30
31     @Inject
32     private DroolsEngine droolsEngine;
33
34     private ConcurrentHashMap<String, ScheduledFuture> pollingRequests = new ConcurrentHashMap<String, ScheduledFuture>();
35     private ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
36
37     public void addSubscriber(Subscriber subscriber) {
38         if (!pollingRequests.containsKey(subscriber.getUrl())) {
39             DMaaPPollingRequest pollingTask = new DMaaPPollingRequest(subscriber, droolsEngine);
40             ScheduledFuture future = service
41                     .scheduleAtFixedRate(pollingTask, 0, subscriber.getPeriod(), TimeUnit.MILLISECONDS);
42             pollingRequests.put(subscriber.getUrl(), future);
43         }
44     }
45
46     public void removeSubscriber(Subscriber subscriber) {
47         ScheduledFuture future = pollingRequests.get(subscriber.getUrl());
48         if (future != null) {
49             future.cancel(true);
50         }
51         pollingRequests.remove(subscriber.getUrl());
52     }
53 }