Merge pull request #22998 from richard-imaoka/doc-22803-blocking-imaoka

More description about blocking operations
This commit is contained in:
Patrik Nordwall 2017-06-16 15:49:02 +02:00 committed by GitHub
commit 4d4fe1471b
13 changed files with 551 additions and 197 deletions

View file

@ -0,0 +1,22 @@
/**
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
*/
package jdocs.actor;
import akka.actor.AbstractActor;
// #blocking-in-actor
class BlockingActor extends AbstractActor {
@Override
public Receive createReceive() {
return receiveBuilder()
.match(Integer.class, i -> {
Thread.sleep(5000); //block for 5 seconds, representing blocking I/O, etc
System.out.println("Blocking operation finished: " + i);
})
.build();
}
}
// #blocking-in-actor

View file

@ -0,0 +1,32 @@
/**
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
*/
package jdocs.actor;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
public class BlockingDispatcherTest {
public static void main(String args[]) {
ActorSystem system = ActorSystem.create("BlockingDispatcherTest");
try {
// #blocking-main
ActorRef actor1 = system.actorOf(Props.create(BlockingFutureActor.class));
ActorRef actor2 = system.actorOf(Props.create(PrintActor.class));
for (int i = 0; i < 100; i++) {
actor1.tell(i, ActorRef.noSender());
actor2.tell(i, ActorRef.noSender());
}
// #blocking-main
Thread.sleep(5000 * 6);
} catch (InterruptedException e) {
//swallow the exception
} finally {
system.terminate();
}
}
}

View file

@ -0,0 +1,30 @@
/**
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
*/
package jdocs.actor;
import akka.actor.AbstractActor;
import akka.dispatch.Futures;
import scala.concurrent.ExecutionContext;
import scala.concurrent.Future;
// #blocking-in-future
class BlockingFutureActor extends AbstractActor {
ExecutionContext ec = getContext().dispatcher();
@Override
public Receive createReceive() {
return receiveBuilder()
.match(Integer.class, i -> {
System.out.println("Calling blocking Future: " + i);
Future<Integer> f = Futures.future(() -> {
Thread.sleep(5000);
System.out.println("Blocking future finished: " + i);
return i;
}, ec);
})
.build();
}
}
// #blocking-in-future

View file

@ -0,0 +1,20 @@
/**
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
*/
package jdocs.actor;
import akka.actor.AbstractActor;
// #print-actor
class PrintActor extends AbstractActor {
@Override
public Receive createReceive() {
return receiveBuilder()
.match(Integer.class, i -> {
System.out.println("PrintActor: " + i);
})
.build();
}
}
// #print-actor

View file

@ -0,0 +1,30 @@
/**
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
*/
package jdocs.actor;
import akka.actor.AbstractActor;
import akka.dispatch.Futures;
import scala.concurrent.ExecutionContext;
import scala.concurrent.Future;
// #separate-dispatcher
class SeparateDispatcherFutureActor extends AbstractActor {
ExecutionContext ec = getContext().getSystem().dispatchers().lookup("my-blocking-dispatcher");
@Override
public Receive createReceive() {
return receiveBuilder()
.match(Integer.class, i -> {
System.out.println("Calling blocking Future on separate dispatcher: " + i);
Future<Integer> f = Futures.future(() -> {
Thread.sleep(5000);
System.out.println("Blocking future finished: " + i);
return i;
}, ec);
})
.build();
}
}
// #separate-dispatcher

View file

@ -0,0 +1,46 @@
/**
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
*/
package jdocs.actor;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
class SeparateDispatcherTest {
public static void main(String args[]) {
Config config = ConfigFactory.parseString(
"my-blocking-dispatcher {\n" +
" type = Dispatcher\n" +
" executor = \"thread-pool-executor\"\n" +
" thread-pool-executor {\n" +
" fixed-pool-size = 16\n" +
" }\n" +
" throughput = 1\n" +
"}\n"
);
ActorSystem system = ActorSystem.create("BlockingDispatcherTest", config);
try {
// #separate-dispatcher-main
ActorRef actor1 = system.actorOf(Props.create(SeparateDispatcherFutureActor.class));
ActorRef actor2 = system.actorOf(Props.create(PrintActor.class));
for (int i = 0; i < 100; i++) {
actor1.tell(i, ActorRef.noSender());
actor2.tell(i, ActorRef.noSender());
}
// #separate-dispatcher-main
Thread.sleep(5000 * 6);
} catch (InterruptedException e) {
//swallow the exception
} finally {
system.terminate();
}
}
}