![JAR search and dependency download from the Maven repository](/logo.png)
com.van.logging.CapacityBasedBufferMonitor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of appender-core Show documentation
Show all versions of appender-core Show documentation
Core functionality to send content to various channels
package com.van.logging;
/**
* Implementation of {@link IBufferMonitor} that flushes the cache when its capacity
* reaches a limit.
*/
public class CapacityBasedBufferMonitor implements IBufferMonitor {
private final int cacheLimit;
private final Object countGuard = new Object();
private int count = 0;
/**
* Creates an instance with the cache limit as provided. When hooked up to a
* LoggingEventCache, this object will flush the cache when the cache's event
* count reaches the limit.
*
* @param cacheLimit the limit in number of event messages. When the associated
* {@link LoggingEventCache}'s event cache size reaches this
* limit, the event cache will be flushed and published.
*/
public CapacityBasedBufferMonitor(int cacheLimit) {
this.cacheLimit = cacheLimit;
}
@Override
public void eventAdded(final T event, final IFlushAndPublish cache) {
boolean flush = false;
synchronized (countGuard) {
if (++count >= cacheLimit) {
flush = true;
count = 0;
}
}
if (flush) {
try {
cache.flushAndPublish();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
@Override
public void shutDown() {
System.out.println("CapacityBasedBufferMonitor: shutting down.");
}
@Override
public String toString() {
return String.format("CapacityBasedBufferMonitor(cacheLimit: %d)", cacheLimit);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy