1 /*******************************************************************************
2 * ============LICENSE_START==================================================
4 * * ===========================================================================
5 * * Copyright © 2017 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====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
25 package org.onap.dmaap.datarouter.node;
30 * Execute an operation no more frequently than a specified interval
33 public abstract class RateLimitedOperation implements Runnable {
34 private boolean marked; // a timer task exists
35 private boolean executing; // the operation is currently in progress
36 private boolean remark; // a request was made while the operation was in progress
38 private long last; // when the last operation started
39 private long mininterval;
42 * Create a rate limited operation
44 * @param mininterval The minimum number of milliseconds after the last execution starts before a new execution can begin
45 * @param timer The timer used to perform deferred executions
47 public RateLimitedOperation(long mininterval, Timer timer) {
49 this.mininterval = mininterval;
52 private class deferred extends TimerTask {
58 private synchronized void unmark() {
62 private void execute() {
68 * Request that the operation be performed by this thread or at a later time by the timer
70 public void request() {
79 private synchronized boolean premark() {
81 // currently executing - wait until it finishes
86 // timer currently running - will run when it expires
89 long now = System.currentTimeMillis();
90 if (last + mininterval > now) {
91 // too soon - schedule a timer
93 timer.schedule(new deferred(), last + mininterval - now);
102 private synchronized boolean demark() {