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

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

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Alachisoft.NCache.Common.Logger;

import Alachisoft.NCache.Common.DataStructures.BridgeOperation;

import java.util.Map;

/**
 * @author Basit Anwer
 */
public class BridgeActivityMonitorHandler implements Runnable {

    private String _monitorName;
    private java.util.HashMap _bridgeActivityHistory = new java.util.HashMap();
    private ILogger _monitorLogger = null;
    private Thread _writerThread;
    private boolean _isThreadStopped = true;

    public final String getMonitorName() {
        return _monitorName;
    }

    public final void setMonitorName(String value) {
        _monitorName = value;
    }

    public final void NodeActivity(String key, BridgeOperation operation) {
        Activity activity = PopulateKeyActivity(operation.getOpCode().toString(), operation.getBridgeOpTimeStamp().toString(), operation.getIsStateTransferOperation()).clone();
        Add(key, activity.clone());
    }

    public final void StartActivity() {

        _monitorLogger = new JLogger();
        try {
            _monitorLogger.Initialize(LoggerNames.BridgeActivityLog, _monitorName);
        } catch (Exception e) {
        }
        _writerThread = new Thread(new Thread(this));
        _writerThread.setDaemon(true);
        _writerThread.setName("BridgeActivityMonitor." + _monitorName);
        _writerThread.start();
    }

    public final void StopActivity() {
        _isThreadStopped = false;

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

    private Activity PopulateKeyActivity(String operation, String timeStamp, boolean isStateTransfer) {
        Activity activity = new Activity();

        activity.operation = operation;
        activity.timestamp = timeStamp;
        activity.isStateTransfer = isStateTransfer;

        return activity;
    }

    private void Add(String key, Activity activity) {
        java.util.ArrayList operationList = null;

        synchronized (this) {
            if (!_bridgeActivityHistory.containsKey(key)) {
                operationList = new java.util.ArrayList();
                operationList.add(activity.clone());
                _bridgeActivityHistory.put(key, operationList);
            } else {
                operationList = (java.util.ArrayList) ((_bridgeActivityHistory.get(key) instanceof java.util.ArrayList) ? _bridgeActivityHistory.get(key) : null);
                if (operationList != null) {
                    operationList.add(activity.clone());
                }
            }
        }
    }

    @Override
    public void run() {
        long sleepTime = 30000;
        while (_isThreadStopped) {
            if (_monitorLogger != null) {
                StringBuilder sb = new StringBuilder();
                java.util.HashMap oldTable = _bridgeActivityHistory;

                synchronized (this) {
                    _bridgeActivityHistory = new java.util.HashMap();
                }
                java.util.Iterator ide = oldTable.entrySet().iterator();
                _monitorLogger.Info("Start Logging");

                while (ide.hasNext()) {
                    Map.Entry pairs = (Map.Entry) ide.next();
                    java.util.ArrayList operationArray = (java.util.ArrayList) ((pairs.getValue() instanceof java.util.ArrayList) ? pairs.getValue() : null);
                    sb.append(" " + pairs.getKey() + " ");
                    for (int i = 0; i < operationArray.size(); i++) {
                        sb.append(operationArray.get(i).toString() + "--- ");
                    }
                    sb.append("\n");
                }
                if (!tangible.DotNetToJavaStringHelper.isNullOrEmpty(sb.toString())) {
                    _monitorLogger.Info(sb.toString());
                    oldTable.clear();
                    sb.delete(0, sb.length());
                }
                _monitorLogger.Info("End Logging");
            }
            try {
                Thread.sleep(sleepTime);
            } catch (InterruptedException ie) {
            }
        }
    }

    //C# TO JAVA CONVERTER WARNING: Java does not allow user-defined value types. The behavior of this class will differ from the original:
//ORIGINAL LINE: struct Activity
    private final static class Activity {

        public String operation;
        public String timestamp;
        public boolean isStateTransfer;

        @Override
        public String toString() {
            return "(" + operation + " ; " + timestamp + " ; " + isStateTransfer + ")";
        }

        public Activity clone() {
            Activity varCopy = new Activity();

            varCopy.operation = this.operation;
            varCopy.timestamp = this.timestamp;
            varCopy.isStateTransfer = this.isStateTransfer;

            return varCopy;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy