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

org.nustaq.kontraktor.RateLoggingRemoteCallMonitor Maven / Gradle / Ivy

The newest version!
package org.nustaq.kontraktor;

import org.nustaq.kontraktor.remoting.base.ObjectSocket;
import org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry;
import org.nustaq.kontraktor.util.RateMeasure;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;

public class RateLoggingRemoteCallMonitor implements RemoteCallMonitor {

    ConcurrentMap rateMeasureMap = new ConcurrentHashMap<>();

    long intervalMillis;

    public RateLoggingRemoteCallMonitor(long intervalMillis) {
        this.intervalMillis = intervalMillis;
    }

    @Override
    public  void remoteCallObserved(Actor selfActor, ObjectSocket objSocket, RemoteCallEntry rce, Object authContext) {
        RateMeasure rateMeasure = rateMeasureMap.get(selfActor.getClass());
        if ( rateMeasure == null ) {
            rateMeasure = new RateMeasure(selfActor.getClass().getSimpleName(), intervalMillis).print(true);
            rateMeasureMap.put(selfActor.getClass(),rateMeasure);
        }
        rateMeasure.count();
    }

    public static void main(String[] args) throws InterruptedException {
        Actor actor = Actors.AsActor(Actor.class);
        RateLoggingRemoteCallMonitor mon = new RateLoggingRemoteCallMonitor(TimeUnit.SECONDS.toMillis(1));
        for (int i = 0; i < 1_000_000_000; i++) {
            mon.remoteCallObserved( actor, null, null, null );
            Thread.sleep(1);
        }
        System.exit(1);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy