2023-01-08 17:13:31 +08:00
|
|
|
/*
|
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
* license agreements; and to You under the Apache License, version 2.0:
|
|
|
|
|
*
|
|
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
2023-06-22 14:19:26 +01:00
|
|
|
* This file is part of the Apache Pekko project, which was derived from Akka.
|
2023-01-08 17:13:31 +08:00
|
|
|
*/
|
|
|
|
|
|
2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2022-02-04 12:36:44 +01:00
|
|
|
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
|
2013-10-14 13:12:17 +02:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.dispatcher;
|
2013-10-14 13:12:17 +02:00
|
|
|
|
|
|
|
|
// #mailbox-implementation-example
|
2022-11-12 10:21:24 +01:00
|
|
|
import org.apache.pekko.actor.ActorRef;
|
|
|
|
|
import org.apache.pekko.actor.ActorSystem;
|
|
|
|
|
import org.apache.pekko.dispatch.Envelope;
|
|
|
|
|
import org.apache.pekko.dispatch.MailboxType;
|
|
|
|
|
import org.apache.pekko.dispatch.MessageQueue;
|
|
|
|
|
import org.apache.pekko.dispatch.ProducesMessageQueue;
|
2013-10-14 13:12:17 +02:00
|
|
|
import com.typesafe.config.Config;
|
|
|
|
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
|
|
|
|
import java.util.Queue;
|
|
|
|
|
import scala.Option;
|
|
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
public class MyUnboundedMailbox
|
|
|
|
|
implements MailboxType, ProducesMessageQueue<MyUnboundedMailbox.MyMessageQueue> {
|
2013-10-14 13:12:17 +02:00
|
|
|
|
|
|
|
|
// This is the MessageQueue implementation
|
|
|
|
|
public static class MyMessageQueue implements MessageQueue, MyUnboundedMessageQueueSemantics {
|
|
|
|
|
private final Queue<Envelope> queue = new ConcurrentLinkedQueue<Envelope>();
|
|
|
|
|
|
|
|
|
|
// these must be implemented; queue used as example
|
|
|
|
|
public void enqueue(ActorRef receiver, Envelope handle) {
|
|
|
|
|
queue.offer(handle);
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2013-10-14 13:12:17 +02:00
|
|
|
public Envelope dequeue() {
|
|
|
|
|
return queue.poll();
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2013-10-14 13:12:17 +02:00
|
|
|
public int numberOfMessages() {
|
|
|
|
|
return queue.size();
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2013-10-14 13:12:17 +02:00
|
|
|
public boolean hasMessages() {
|
|
|
|
|
return !queue.isEmpty();
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2013-10-14 13:12:17 +02:00
|
|
|
public void cleanUp(ActorRef owner, MessageQueue deadLetters) {
|
2022-02-16 09:31:21 +01:00
|
|
|
while (!queue.isEmpty()) {
|
2022-01-06 19:36:55 +05:30
|
|
|
deadLetters.enqueue(owner, dequeue());
|
2013-10-14 13:12:17 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-18 08:13:01 +01:00
|
|
|
// This constructor signature must exist, it will be called by Pekko
|
2017-03-16 09:30:00 +01:00
|
|
|
public MyUnboundedMailbox(ActorSystem.Settings settings, Config config) {
|
2013-10-14 13:12:17 +02:00
|
|
|
// put your initialization code here
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The create method is called to create the MessageQueue
|
|
|
|
|
public MessageQueue create(Option<ActorRef> owner, Option<ActorSystem> system) {
|
|
|
|
|
return new MyMessageQueue();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// #mailbox-implementation-example
|