Initial add of APPC client libraries
[appc.git] / appc-client / client-lib / src / main / java / org / openecomp / appc / client / impl / core / TimerServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.client.impl.core;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29
30 import java.util.List;
31 import java.util.concurrent.*;
32
33 class TimerServiceImpl implements ITimerService {
34
35     private final EELFLogger LOG = EELFManager.getInstance().getLogger(TimerServiceImpl.class);
36     private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
37     private final ConcurrentHashMap<String, Future> timeOutEvents = new ConcurrentHashMap<>();
38     private final long responseTimeout;
39
40     TimerServiceImpl(long responseTimeout) {
41         this.responseTimeout = responseTimeout;
42     }
43
44     @Override
45     public synchronized void cancel(String correlationID) {
46         Future timeOutEvent = timeOutEvents.remove(correlationID);
47         if (timeOutEvent != null){
48             timeOutEvent.cancel(true);
49         }
50     }
51
52     @Override
53     public synchronized void add(String correlationID, ITimeoutHandler handler) {
54         Future timeOutEvent = scheduler.schedule(new HandleTimeout(correlationID, handler), responseTimeout, TimeUnit.MILLISECONDS);
55         timeOutEvents.put(correlationID, timeOutEvent);
56     }
57
58     @Override
59     public void shutdown() {
60         List<Runnable> listTask = scheduler.shutdownNow();
61         LOG.info("the amount of tasks that never commenced execution " + listTask.size());
62     }
63
64     private class HandleTimeout implements Runnable {
65
66         String correlationID;
67         ITimeoutHandler handler;
68
69         HandleTimeout(String correlationID, ITimeoutHandler handler) {
70             this.correlationID = correlationID;
71             this.handler = handler;
72         }
73
74         @Override
75         public void run(){
76             System.out.println("Timeout event of request " + correlationID);
77             handler.onTimeout();
78             timeOutEvents.remove(correlationID);
79         }
80     }
81
82 }