Skip to content

Instantly share code, notes, and snippets.

@dln
Forked from ajsutton/BackgroundLogger.java
Created February 23, 2012 15:25
Show Gist options
  • Save dln/1893288 to your computer and use it in GitHub Desktop.
Save dln/1893288 to your computer and use it in GitHub Desktop.
Java vs Scala -- Disruptor Example of Background Logging
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class BackgroundLogger
{
private static final int ENTRIES = 64;
private final ExecutorService executorService;
private final Disruptor<LogEntry> disruptor;
private final RingBuffer<LogEntry> ringBuffer;
BackgroundLogger()
{
executorService = Executors.newCachedThreadPool();
disruptor = new Disruptor<LogEntry>(LogEntry.FACTORY, ENTRIES, executorService);
disruptor.handleEventsWith(new LogEntryHandler());
disruptor.start();
ringBuffer = disruptor.getRingBuffer();
}
public void log(String text)
{
final long sequence = ringBuffer.next();
final LogEntry logEntry = ringBuffer.get(sequence);
logEntry.time = System.currentTimeMillis();
logEntry.level = level;
logEntry.text = text;
ringBuffer.publish(sequence);
}
public void stop()
{
disruptor.shutdown();
executorService.shutdownNow();
}
}
import com.lmax.disruptor.RingBuffer
import com.lmax.disruptor.dsl.Disruptor
import java.util.concurrent.{ExecutorService, Executors}
class BackgroundLogger {
val int ENTRIES = 64
private val executorService = Executors.newCachedThreadPool
private val disruptor = new Disruptor[LogEntry](LogEntry.FACTORY, ENTRIES, executorService)
disruptor.handleEventsWith(new LogEntryHandler)
disruptor.start()
private val ringBuffer = disruptor.getRingBuffer
def log(text: String) {
val sequence = ringBuffer.next
val logEntry = ringBuffer.get(sequence)
logEntry.time = System.currentTimeMillis
logEntry.level = level
logEntry.text = text
ringBuffer.publish(sequence)
}
def stop() {
disruptor.shutdown()
executorService.shutdownNow()
}
}
import com.lmax.disruptor.EventFactory;
class LogEntry
{
public static final EventFactory<LogEntry> FACTORY = new EventFactory<LogEntry>()
{
public LogEntry newInstance()
{
return new LogEntry();
}
};
long time;
int level;
String text;
}
import com.lmax.disruptor.EventFactory
class LogEntry {
var time: Long = _
var level: Int = _
var text: String = _
}
object LogEntry {
val FACTORY = new EventFactory[LogEntry] {
def LogEntry newInstance = new LogEntry
}
}
import com.lmax.disruptor.EventHandler;
public class LogEntryHandler implements EventHandler<LogEntry>
{
public LogEntryHandler()
{
}
public void onEvent(final LogEntry logEntry, final long sequence, final boolean endOfBatch) throws Exception
{
// Write
}
}
import com.lmax.disruptor.EventHandler
class LogEntryHandler extends EventHandler[LogEntry] {
def onEvent(logEntry: LogEntry, sequence: Long, endOfBatch: Boolean) {
// Write
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment