All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alachisoft.ncache.client.internal.communication.RequestModerator Maven / Gradle / Ivy

There is a newer version: 5.3.0
Show newest version
package com.alachisoft.ncache.client.internal.communication;

import java.util.Timer;
import java.util.TimerTask;

//  Copyright (c) 2020 Alachisoft
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//     http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License


class RequestModerator implements  AutoCloseable {
    private java.util.HashMap statusBook;

    private Timer cleanupTask;

    public RequestModerator() {
        statusBook = new java.util.HashMap();
        cleanupTask = new Timer("RequestModeratorCleanupTask");
        cleanupTask.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                synchronized (statusBook) {
                    for (NodeRequestStatus status : statusBook.values()) {
                        status.Clean();
                    }
                }
            }
        }, 30, 3000);


    }

    public final long registerRequest(String address, long currentId) {
        long returnValue = -1;
        if (address != null) {
            synchronized (statusBook) {
                NodeRequestStatus status = null;
                if (!((status = statusBook.get(address)) != null)) {
                    status = new NodeRequestStatus();
                    statusBook.put(address, status);
                }
                synchronized (status.getSyncRoot()) {
                    status.RegisterRequest(currentId);
                    returnValue = status.getLastAcknowledged();
                }
            }
        }
        return returnValue;
    }

    public final void unRegisterRequest(long Id) {
        synchronized (statusBook) {
            for (NodeRequestStatus status : statusBook.values()) {
                status.Acknowledge(Id);
            }
        }
    }


    @Override
    public void close() throws Exception {
        if(cleanupTask !=null)
            cleanupTask.cancel();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy