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

Alachisoft.NCache.Common.Logger.InMemoryLogger Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Alachisoft.NCache.Common.Logger;

/**
 * @author Basit Anwer
 */
//using System.Linq;

import Alachisoft.NCache.Common.DataStructures.BridgeOperation;

/**
 * This Class used for In Memory-Logging for Bridge Dev Tracing
 */
public class InMemoryLogger implements Runnable {

    private static java.util.HashMap _inMemoryLoggers = new java.util.HashMap();
    private java.util.HashMap hashMap;
    private int _addCount = 0;
    private int _updateCount = 0;
    private String _cacheName;
    private Thread _writerThread = null;
    private boolean _isThreadStopped = true;
    private String _loggerType;
    private int _threadWaitInterval = 120000; //2 min  || 240000; 4 min;
    private ILogger _nCachelogger;
    private Object mutex = new Object();

    public static InMemoryLogger GetLogger(String srcCache, String LoggerType, ILogger NCacheLog) {
        InMemoryLogger logger = null;
        synchronized (_inMemoryLoggers) {
            if (_inMemoryLoggers.containsKey(srcCache)) {
                logger = _inMemoryLoggers.get(srcCache);
            } else {
                logger = new InMemoryLogger();
                logger.Initialize(srcCache, LoggerType, NCacheLog);
                _inMemoryLoggers.put(srcCache, logger);
            }
        }
        return logger;
    }

    public final void Add(String key, BridgeOperation operation) {
        synchronized (mutex) {
            if (hashMap.containsKey(key)) {
                hashMap.put(key, operation);
                _updateCount++;
            } else {
                hashMap.put(key, operation);
                _addCount++;
            }
        }
    }

    public final void Initialize(String targetId, String LoggerType, ILogger ncachelogger) {
        hashMap = new java.util.HashMap();
        _cacheName = targetId;
        _loggerType = LoggerType;
        _nCachelogger = ncachelogger;
        Start();
    }

    public final void Start() {
        _writerThread = new Thread(new Thread(this));
        _writerThread.setDaemon(true);
        _writerThread.setName("InMemoryLogger." + _cacheName + "." + _loggerType);
        _writerThread.start();
    }

    @Override
    public void run() {
        try {
            while (_isThreadStopped) {
                Thread.sleep(_threadWaitInterval);
                String msg = " ::: Add = " + _addCount + "  ---- Update = " + _updateCount + " ::: ";
                _nCachelogger.DevTrace(msg);
            }
        } catch (InterruptedException ie) {
        }
    }

    public final void Stop() {
        _isThreadStopped = false;

        if (_writerThread.isAlive()) {
            _writerThread.stop();
            _writerThread = null;
        }

        _addCount = 0;
        _updateCount = 0;

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy