Alachisoft.NCache.Common.Logger.BridgeOperationMonitor 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.BridgeOpCodes;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author Basit Anwer
*/
public class BridgeOperationMonitor implements Runnable {
public ILogger _logger;
private java.util.HashMap _bridgeOperationsHistory = new java.util.HashMap();
private java.util.HashMap _failedbridgeOperationsHistory = new java.util.HashMap();
private Thread bridgeOperationThread;
public BridgeOperationMonitor() {
bridgeOperationThread = new Thread(new Thread(this));
bridgeOperationThread.setDaemon(true);
bridgeOperationThread.start();
}
public final void IncrementBridgeOperation(BridgeOpCodes opcode) {
synchronized (this) {
if (!_bridgeOperationsHistory.containsKey(opcode)) {
_bridgeOperationsHistory.put(opcode, new AtomicInteger(0));
}
_bridgeOperationsHistory.get(opcode).set(_bridgeOperationsHistory.get(opcode).get() + 1);
}
}
public final void IncremenFailedtBridgeOperation(BridgeOpCodes opcode) {
synchronized (this) {
if (!_failedbridgeOperationsHistory.containsKey(opcode)) {
_failedbridgeOperationsHistory.put(opcode, new AtomicInteger(0));
}
_failedbridgeOperationsHistory.get(opcode).set(_failedbridgeOperationsHistory.get(opcode).get() + 1);
}
}
@Override
public void run() {
long sleepTime = 50000;
while (true) {
if (_logger != null) {
StringBuilder sb = new StringBuilder();
sb.append("Operations :[");
synchronized (this) {
java.util.Iterator> ide = _bridgeOperationsHistory.entrySet().iterator();
while (ide.hasNext()) {
sb.append(" ").append(ide.next().getKey()).append(" = ").append(ide.next().getValue()).append(" ,");
}
sb.append("]").append(System.getProperty("line.separator"));
sb.append("Failed [");
ide = _failedbridgeOperationsHistory.entrySet().iterator();
while (ide.hasNext()) {
sb.append(" ").append(ide.next().getKey()).append(" = ").append(ide.next().getValue()).append(" ,");
}
sb.append("]").append(System.getProperty("line.separator"));
}
}
try {
Thread.sleep(sleepTime);
} catch (InterruptedException ie) {
}
}
}
}