Skip to content

Instantly share code, notes, and snippets.

@guidomedina
Last active April 2, 2016 11:04
Show Gist options
  • Save guidomedina/b71b81bedd69c64a7b65 to your computer and use it in GitHub Desktop.
Save guidomedina/b71b81bedd69c64a7b65 to your computer and use it in GitHub Desktop.
Non-blocking LoggerMailbox
import akka.actor.ActorRef;
import akka.dispatch.*;
import akka.event.*;
import org.jctools.queues.MpscArrayQueue;
/**
* A non-blocking, multiple producer, single consumer high performance logger mailbox.
*/
public final class LoggerMailbox implements MessageQueue, LoggerMessageQueueSemantics {
private final MpscArrayQueue<Envelope> queue = new MpscArrayQueue<>(4096);
@Override
public void enqueue(ActorRef receiver, Envelope handle) {
queue.offer(handle);
}
@Override
public Envelope dequeue() {
return queue.poll();
}
@Override
public int numberOfMessages() {
return queue.size();
}
@Override
public boolean hasMessages() {
return !queue.isEmpty();
}
@Override
public void cleanUp(ActorRef owner, MessageQueue deadLetters) {
if (hasMessages()) {
Envelope envelope;
final Logging.StandardOutLogger standardOutLogger = Logging.StandardOutLogger();
while ((envelope = queue.poll()) != null) {
standardOutLogger.tell(envelope.message(), envelope.sender());
}
}
}
}
import akka.actor.*;
import akka.dispatch.*;
import com.typesafe.config.Config;
import scala.Option;
/**
* A non-blocking, multiple producer, single consumer high performance logger mailbox type.
*/
public final class LoggerMailboxType implements MailboxType, ProducesMessageQueue<LoggerMailbox> {
@SuppressWarnings("UnusedParameters")
public LoggerMailboxType(ActorSystem.Settings settings, Config config) {
}
@Override
public MessageQueue create(Option<ActorRef> owner, Option<ActorSystem> system) {
return new LoggerMailbox();
}
}
akka.actor.mailbox.logger-queue.mailbox-type = "com.xyz.mailbox.LoggerMailboxType"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment