
org.interledger.spsp.server.gcp.BufferedConsoleAppender Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spsp-server Show documentation
Show all versions of spsp-server Show documentation
An Interledger SPSP server with a stateless STREAM receiver that publishes events.
The newest version!
package org.interledger.spsp.server.gcp;
import ch.qos.logback.core.OutputStreamAppender;
import ch.qos.logback.core.joran.spi.ConsoleTarget;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* Console appender that buffers output and periodically flushes. By default, output will be flushed every 100KB or
* 1000ms (whichever comes first). These can be overridden via {@link #setBufferSize(int)} and {@link
* #setFlushRateMillis(int)}
*
* @param
*
* @deprecated This will be removed once a version of this class is extracted to a common library.
*/
@Deprecated
public class BufferedConsoleAppender extends OutputStreamAppender {
private final ScheduledExecutorService scheduledFlusher;
private OutputStream consoleStream = ConsoleTarget.SystemOut.getStream();
private int bufferSize = 100 * 1024;
private int flushRateMillis = 1000;
private BufferedOutputStream targetStream;
public BufferedConsoleAppender() {
scheduledFlusher = Executors.newScheduledThreadPool(1);
}
/**
* Set the number of bytes that are buffered before writing to the underlying console stream
*
* @param bufferSize buffer size in bytes
*/
public void setBufferSize(int bufferSize) {
this.bufferSize = bufferSize;
}
/**
* Sets how frequently the underlying console stream is flushed.
*
* @param flushRateMillis flush rate in millseconds
*/
public void setFlushRateMillis(int flushRateMillis) {
this.flushRateMillis = flushRateMillis;
}
protected void setConsoleStream(OutputStream consoleStream) {
this.consoleStream = consoleStream;
}
@Override
public void start() {
super.setImmediateFlush(false);
targetStream = new BufferedOutputStream(consoleStream, bufferSize);
setOutputStream(targetStream);
super.start();
startFlushing();
}
@Override
public void stop() {
scheduledFlusher.shutdown();
super.stop();
}
private void startFlushing() {
scheduledFlusher.scheduleAtFixedRate(this::flushStream,
flushRateMillis,
flushRateMillis,
TimeUnit.MILLISECONDS);
Runtime.getRuntime().addShutdownHook(new Thread(() -> flushStream()));
}
private void flushStream() {
try {
targetStream.flush();
} catch (IOException e) {
e.printStackTrace(); // this should never happen with System.out
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy