Update Source.actorRef sample to not use deprecated method #28679
This commit is contained in:
parent
7d790ef328
commit
6f23b1fb4b
3 changed files with 27 additions and 10 deletions
|
|
@ -4,11 +4,9 @@ Materialize an `ActorRef`; sending messages to it will emit them on the stream.
|
||||||
|
|
||||||
@ref[Source operators](../index.md#source-operators)
|
@ref[Source operators](../index.md#source-operators)
|
||||||
|
|
||||||
@@@ div { .group-scala }
|
|
||||||
## Signature
|
## Signature
|
||||||
|
|
||||||
@@signature [Source.scala](/akka-stream/src/main/scala/akka/stream/scaladsl/Source.scala) { #actorRef }
|
@apidoc[Source.actorRef](Source$) { scala="#actorRef[T](completionMatcher:PartialFunction[Any,akka.stream.CompletionStrategy],failureMatcher:PartialFunction[Any,Throwable],bufferSize:Int,overflowStrategy:akka.stream.OverflowStrategy):akka.stream.scaladsl.Source[T,akka.actor.ActorRef]" java="#actorRef(akka.japi.function.Function,akka.japi.function.Function,int,akka.stream.OverflowStrategy)" }
|
||||||
@@@
|
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ package jdocs.stream.operators;
|
||||||
|
|
||||||
// #imports
|
// #imports
|
||||||
// #range-imports
|
// #range-imports
|
||||||
|
import akka.Done;
|
||||||
import akka.NotUsed;
|
import akka.NotUsed;
|
||||||
import akka.actor.ActorSystem;
|
import akka.actor.ActorSystem;
|
||||||
import akka.actor.testkit.typed.javadsl.ManualTime;
|
import akka.actor.testkit.typed.javadsl.ManualTime;
|
||||||
|
|
@ -81,7 +82,17 @@ public class SourceDocExamples {
|
||||||
// #actor-ref
|
// #actor-ref
|
||||||
|
|
||||||
int bufferSize = 100;
|
int bufferSize = 100;
|
||||||
Source<Object, ActorRef> source = Source.actorRef(bufferSize, OverflowStrategy.dropHead());
|
Source<Object, ActorRef> source =
|
||||||
|
Source.actorRef(
|
||||||
|
elem -> {
|
||||||
|
// complete stream immediately if we send it Done
|
||||||
|
if (elem == Done.done()) return Optional.of(CompletionStrategy.immediately());
|
||||||
|
else return Optional.empty();
|
||||||
|
},
|
||||||
|
// never fail the stream because of a message
|
||||||
|
elem -> Optional.empty(),
|
||||||
|
bufferSize,
|
||||||
|
OverflowStrategy.dropHead());
|
||||||
|
|
||||||
ActorRef actorRef = source.to(Sink.foreach(System.out::println)).run(system);
|
ActorRef actorRef = source.to(Sink.foreach(System.out::println)).run(system);
|
||||||
actorRef.tell("hello", ActorRef.noSender());
|
actorRef.tell("hello", ActorRef.noSender());
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
package docs.stream.operators
|
package docs.stream.operators
|
||||||
|
|
||||||
|
import akka.Done
|
||||||
import akka.actor.ActorSystem
|
import akka.actor.ActorSystem
|
||||||
import akka.testkit.TestProbe
|
import akka.testkit.TestProbe
|
||||||
|
|
||||||
|
|
@ -29,23 +30,30 @@ object SourceOperators {
|
||||||
|
|
||||||
def actorRef(): Unit = {
|
def actorRef(): Unit = {
|
||||||
//#actorRef
|
//#actorRef
|
||||||
|
import akka.Done
|
||||||
import akka.actor.Status.Success
|
|
||||||
import akka.actor.ActorRef
|
import akka.actor.ActorRef
|
||||||
import akka.stream.OverflowStrategy
|
import akka.stream.OverflowStrategy
|
||||||
import akka.stream.CompletionStrategy
|
import akka.stream.CompletionStrategy
|
||||||
import akka.stream.scaladsl._
|
import akka.stream.scaladsl._
|
||||||
|
import scala.util.Failure
|
||||||
|
|
||||||
val bufferSize = 100
|
val source: Source[Any, ActorRef] = Source.actorRef(
|
||||||
|
completionMatcher = {
|
||||||
val source: Source[Any, ActorRef] = Source.actorRef[Any](bufferSize, OverflowStrategy.dropHead)
|
case Done =>
|
||||||
|
// complete stream immediately if we send it Done
|
||||||
|
CompletionStrategy.immediately
|
||||||
|
},
|
||||||
|
// never fail the stream because of a message
|
||||||
|
failureMatcher = PartialFunction.empty,
|
||||||
|
bufferSize = 100,
|
||||||
|
overflowStrategy = OverflowStrategy.dropHead)
|
||||||
val actorRef: ActorRef = source.to(Sink.foreach(println)).run()
|
val actorRef: ActorRef = source.to(Sink.foreach(println)).run()
|
||||||
|
|
||||||
actorRef ! "hello"
|
actorRef ! "hello"
|
||||||
actorRef ! "hello"
|
actorRef ! "hello"
|
||||||
|
|
||||||
// The stream completes successfully with the following message
|
// The stream completes successfully with the following message
|
||||||
actorRef ! Success(CompletionStrategy.immediately)
|
actorRef ! Done
|
||||||
//#actorRef
|
//#actorRef
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue