2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.client.impl.core;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
30 import java.util.List;
31 import java.util.concurrent.*;
33 class TimerServiceImpl implements ITimerService {
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;
40 TimerServiceImpl(long responseTimeout) {
41 this.responseTimeout = responseTimeout;
45 public synchronized void cancel(String correlationID) {
46 Future timeOutEvent = timeOutEvents.remove(correlationID);
47 if (timeOutEvent != null){
48 timeOutEvent.cancel(true);
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);
59 public void shutdown() {
60 List<Runnable> listTask = scheduler.shutdownNow();
61 LOG.info("the amount of tasks that never commenced execution " + listTask.size());
64 private class HandleTimeout implements Runnable {
67 ITimeoutHandler handler;
69 HandleTimeout(String correlationID, ITimeoutHandler handler) {
70 this.correlationID = correlationID;
71 this.handler = handler;
76 System.out.println("Timeout event of request " + correlationID);
78 timeOutEvents.remove(correlationID);