2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.cadi.register;
24 import java.util.Deque;
25 import java.util.Iterator;
26 import java.util.Timer;
27 import java.util.TimerTask;
28 import java.util.concurrent.ConcurrentLinkedDeque;
30 import org.onap.aaf.cadi.client.Result;
31 import org.onap.aaf.misc.env.impl.BasicEnv;
33 public class Registrar<ENV extends BasicEnv> {
34 private static final String REGISTRAR = "Registrar";
35 private static final long INTERVAL = 15*60*1000L; // 15 mins
36 private static final long START = 3000; // Start in 3 seconds
37 private static final Object LOCK = new Object();
38 private Deque<Registrant<ENV>> registrants;
39 private Timer timer, erroringTimer;
41 public Registrar(final ENV env, boolean shutdownHook) {
42 registrants = new ConcurrentLinkedDeque<Registrant<ENV>>();
45 timer = new Timer(REGISTRAR,true);
46 timer.schedule(new RegistrationTimerTask(env), START, INTERVAL);
49 Runtime.getRuntime().addShutdownHook(new Thread() {
57 private class RegistrationTimerTask extends TimerTask {
58 private final ENV env;
59 public RegistrationTimerTask(ENV env) {
64 for (Iterator<Registrant<ENV>> iter = registrants.iterator(); iter.hasNext();) {
65 Registrant<ENV> reg = iter.next();
66 Result<Void> rv = reg.update(env);
69 if (erroringTimer!=null) {
70 erroringTimer.cancel();
74 env.error().log(rv.toString());
75 // Account for different Registrations not being to same place
76 if (erroringTimer==null) {
77 erroringTimer = new Timer(REGISTRAR + " error re-check ",true);
78 erroringTimer.schedule(new RegistrationTimerTask(env),20000,20000);
86 public void register(Registrant<ENV> r) {
87 registrants.addLast(r);
90 public void deregister(Registrant<ENV> r) {
91 registrants.remove(r);
94 public void close(ENV env) {
98 while (registrants.peek()!=null) {
99 r = registrants.pop();