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

io.infinicast.client.impl.responder.RequestResponseManager Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
package io.infinicast.client.impl.responder;

import io.infinicast.ConcurrentHashmapExtensions;
import io.infinicast.IntervalChecker;
import io.infinicast.Logger;
import io.infinicast.LoggerFactory;
import io.infinicast.client.impl.pathAccess.RequestResponder;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
public class RequestResponseManager {
    static Logger _logger = LoggerFactory.getLogger(RequestResponseManager.class);
    public static int timeBetweenChecks = 5000;
    ArrayList _toBeRemoved;
    ConcurrentHashMap _awaitingResponders = new ConcurrentHashMap();
    public RequestResponseManager() {
        this._toBeRemoved = new ArrayList();
    }
    public void initChecker(IntervalChecker requestResponseChecker) {
        requestResponseChecker.StartChecker(() -> {
            this._check();
        }
        , RequestResponseManager.timeBetweenChecks);
    }
    void _check() {
        try {
            this.doCheck();
        }
        catch (Exception ex) {
            RequestResponseManager._logger.error("Exception in requestResponseManager _check", ex);
        }
    }
    void doCheck() {
        for (RequestResponder responder : this._awaitingResponders.keySet()) {
            if (responder.alreadyResponded()) {
                this._toBeRemoved.add(responder);
            }
            else if (responder.answerTakesLong()) {
                RequestResponseManager._logger.info("responder takes long " + responder.toString());
                responder.sendHandlingStarted();
            }
        }
        for (RequestResponder requestResponder : this._toBeRemoved) {
            this._awaitingResponders.remove(requestResponder);
        }
        if (this._toBeRemoved.size() > 0) {
            this._toBeRemoved.clear();
        }
    }
    public void addResponder(RequestResponder responder) {
        ConcurrentHashmapExtensions.getOrAdd(this._awaitingResponders, responder, responder);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy