2016-01-13 16:25:24 +01:00
|
|
|
/*
|
2017-01-04 17:37:10 +01:00
|
|
|
* Copyright (C) 2015-2017 Lightbend Inc. <http://www.lightbend.com>
|
2016-01-13 16:25:24 +01:00
|
|
|
*/
|
2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.stream;
|
2016-01-13 16:25:24 +01:00
|
|
|
|
|
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.actor.ActorSystem;
|
|
|
|
|
import akka.actor.Props;
|
|
|
|
|
import akka.stream.ActorMaterializer;
|
|
|
|
|
import akka.stream.Materializer;
|
|
|
|
|
import akka.stream.actor.AbstractActorPublisher;
|
|
|
|
|
import akka.stream.actor.ActorPublisherMessage;
|
|
|
|
|
import akka.stream.javadsl.Sink;
|
|
|
|
|
import akka.stream.javadsl.Source;
|
2017-03-16 09:30:00 +01:00
|
|
|
import jdocs.AbstractJavaTest;
|
2017-03-17 03:02:47 +08:00
|
|
|
import akka.testkit.javadsl.TestKit;
|
2016-01-13 16:25:24 +01:00
|
|
|
import org.junit.AfterClass;
|
|
|
|
|
import org.junit.BeforeClass;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
2016-02-11 16:39:25 +01:00
|
|
|
public class ActorPublisherDocTest extends AbstractJavaTest {
|
2016-01-13 16:25:24 +01:00
|
|
|
|
|
|
|
|
static ActorSystem system;
|
2016-02-11 16:39:25 +01:00
|
|
|
static Materializer mat;
|
2016-01-13 16:25:24 +01:00
|
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
|
public static void setup() {
|
|
|
|
|
system = ActorSystem.create("ActorPublisherDocTest");
|
2016-02-11 16:39:25 +01:00
|
|
|
mat = ActorMaterializer.create(system);
|
2016-01-13 16:25:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@AfterClass
|
|
|
|
|
public static void tearDown() {
|
2017-03-17 03:02:47 +08:00
|
|
|
TestKit.shutdownActorSystem(system);
|
2016-01-13 16:25:24 +01:00
|
|
|
system = null;
|
2016-02-11 16:39:25 +01:00
|
|
|
mat = null;
|
2016-01-13 16:25:24 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-11 16:39:25 +01:00
|
|
|
//#job-manager
|
|
|
|
|
public static class JobManagerProtocol {
|
|
|
|
|
final public static class Job {
|
|
|
|
|
public final String payload;
|
2016-01-13 16:25:24 +01:00
|
|
|
|
2016-02-11 16:39:25 +01:00
|
|
|
public Job(String payload) {
|
|
|
|
|
this.payload = payload;
|
2016-01-13 16:25:24 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-11 16:39:25 +01:00
|
|
|
}
|
2016-01-13 16:25:24 +01:00
|
|
|
|
2016-02-11 16:39:25 +01:00
|
|
|
public static class JobAcceptedMessage {
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
return "JobAccepted";
|
2016-01-13 16:25:24 +01:00
|
|
|
}
|
|
|
|
|
}
|
2016-02-11 16:39:25 +01:00
|
|
|
public static final JobAcceptedMessage JobAccepted = new JobAcceptedMessage();
|
2016-01-13 16:25:24 +01:00
|
|
|
|
2016-02-11 16:39:25 +01:00
|
|
|
public static class JobDeniedMessage {
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
return "JobDenied";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public static final JobDeniedMessage JobDenied = new JobDeniedMessage();
|
|
|
|
|
}
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
|
2016-02-11 16:39:25 +01:00
|
|
|
public static class JobManager extends AbstractActorPublisher<JobManagerProtocol.Job> {
|
|
|
|
|
|
|
|
|
|
public static Props props() { return Props.create(JobManager.class); }
|
|
|
|
|
|
|
|
|
|
private final int MAX_BUFFER_SIZE = 100;
|
|
|
|
|
private final List<JobManagerProtocol.Job> buf = new ArrayList<>();
|
|
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
.match(JobManagerProtocol.Job.class, job -> buf.size() == MAX_BUFFER_SIZE, job -> {
|
2017-03-16 09:30:00 +01:00
|
|
|
getSender().tell(JobManagerProtocol.JobDenied, getSelf());
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
})
|
|
|
|
|
.match(JobManagerProtocol.Job.class, job -> {
|
2017-03-16 09:30:00 +01:00
|
|
|
getSender().tell(JobManagerProtocol.JobAccepted, getSelf());
|
2016-02-11 16:39:25 +01:00
|
|
|
|
|
|
|
|
if (buf.isEmpty() && totalDemand() > 0)
|
|
|
|
|
onNext(job);
|
|
|
|
|
else {
|
|
|
|
|
buf.add(job);
|
|
|
|
|
deliverBuf();
|
2016-01-13 16:25:24 +01:00
|
|
|
}
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
})
|
|
|
|
|
.match(ActorPublisherMessage.Request.class, request -> deliverBuf())
|
|
|
|
|
.match(ActorPublisherMessage.Cancel.class, cancel -> getContext().stop(self()))
|
|
|
|
|
.build();
|
2016-02-11 16:39:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void deliverBuf() {
|
|
|
|
|
while (totalDemand() > 0) {
|
|
|
|
|
/*
|
|
|
|
|
* totalDemand is a Long and could be larger than
|
|
|
|
|
* what buf.splitAt can accept
|
|
|
|
|
*/
|
|
|
|
|
if (totalDemand() <= Integer.MAX_VALUE) {
|
|
|
|
|
final List<JobManagerProtocol.Job> took =
|
|
|
|
|
buf.subList(0, Math.min(buf.size(), (int) totalDemand()));
|
|
|
|
|
took.forEach(this::onNext);
|
|
|
|
|
buf.removeAll(took);
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
final List<JobManagerProtocol.Job> took =
|
|
|
|
|
buf.subList(0, Math.min(buf.size(), Integer.MAX_VALUE));
|
|
|
|
|
took.forEach(this::onNext);
|
|
|
|
|
buf.removeAll(took);
|
2016-01-13 16:25:24 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-11 16:39:25 +01:00
|
|
|
}
|
|
|
|
|
//#job-manager
|
2016-01-13 16:25:24 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void demonstrateActorPublisherUsage() {
|
2017-03-17 03:02:47 +08:00
|
|
|
new TestKit(system) {
|
2016-01-13 16:25:24 +01:00
|
|
|
private final SilenceSystemOut.System System = SilenceSystemOut.get(getTestActor());
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
//#actor-publisher-usage
|
|
|
|
|
final Source<JobManagerProtocol.Job, ActorRef> jobManagerSource =
|
|
|
|
|
Source.actorPublisher(JobManager.props());
|
|
|
|
|
|
|
|
|
|
final ActorRef ref = jobManagerSource
|
|
|
|
|
.map(job -> job.payload.toUpperCase())
|
|
|
|
|
.map(elem -> {
|
|
|
|
|
System.out.println(elem);
|
|
|
|
|
return elem;
|
|
|
|
|
})
|
|
|
|
|
.to(Sink.ignore())
|
|
|
|
|
.run(mat);
|
|
|
|
|
|
|
|
|
|
ref.tell(new JobManagerProtocol.Job("a"), ActorRef.noSender());
|
|
|
|
|
ref.tell(new JobManagerProtocol.Job("b"), ActorRef.noSender());
|
|
|
|
|
ref.tell(new JobManagerProtocol.Job("c"), ActorRef.noSender());
|
|
|
|
|
//#actor-publisher-usage
|
|
|
|
|
|
|
|
|
|
expectMsgEquals("A");
|
|
|
|
|
expectMsgEquals("B");
|
|
|
|
|
expectMsgEquals("C");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|