Skip to content

Instantly share code, notes, and snippets.

@guidomedina
Created August 4, 2016 11:01
Show Gist options
  • Save guidomedina/0f6f6dee3aab9ec6a387200bc07d01e2 to your computer and use it in GitHub Desktop.
Save guidomedina/0f6f6dee3aab9ec6a387200bc07d01e2 to your computer and use it in GitHub Desktop.
import akka.actor.ActorRef;
import akka.dispatch.Envelope;
import akka.dispatch.MessageQueue;
import akka.dispatch.UnboundedMessageQueueSemantics;
import org.jctools.queues.MpscChunkedArrayQueue;
import org.jctools.util.Pow2;
public final class MpscChunkedMailbox implements MessageQueue, UnboundedMessageQueueSemantics {
private final MpscChunkedArrayQueue<Envelope> queue;
public MpscChunkedMailbox(int chunkSize) {
queue = new MpscChunkedArrayQueue<>(chunkSize, Pow2.MAX_POW2, true);
}
@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;
while ((envelope = queue.poll()) != null) {
deadLetters.enqueue(owner, envelope);
}
}
}
}
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.dispatch.MailboxType;
import akka.dispatch.MessageQueue;
import akka.dispatch.ProducesMessageQueue;
import com.typesafe.config.Config;
import scala.Option;
public final class MpscChunkedMailboxType implements MailboxType, ProducesMessageQueue<MpscChunkedMailbox> {
private final int chunkSize;
@SuppressWarnings("UnusedParameters")
public MpscChunkedMailboxType(ActorSystem.Settings settings, Config config) {
chunkSize = config.getInt("chunk-size");
if (chunkSize < 1) {
throw new IllegalArgumentException("Mailbox chunk size must not be less than 1");
}
}
@Override
public MessageQueue create(Option<ActorRef> owner, Option<ActorSystem> system) {
return new MpscChunkedMailbox(chunkSize);
}
}
chunked-mailbox-8 {
mailbox-type = "mailbox.MpscChunkedMailboxType"
chunk-size = 8
}
chunked-mailbox-16 {
mailbox-type = "mailbox.MpscChunkedMailboxType"
chunk-size = 16
}
chunked-mailbox-128 {
mailbox-type = "mailbox.MpscChunkedMailboxType"
chunk-size = 128
}
chunked-mailbox-1024 {
mailbox-type = "mailbox.MpscChunkedMailboxType"
chunk-size = 1024
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment