Reformating configuration and examples for PDF (Java). See #2413

This commit is contained in:
Björn Antonsson 2012-09-26 10:56:25 +02:00
parent 5490bcf66d
commit 309bb53d98
42 changed files with 902 additions and 615 deletions

View file

@ -38,10 +38,11 @@ akka {
# See the Akka Documentation for more info about Extensions # See the Akka Documentation for more info about Extensions
extensions = [] extensions = []
# Toggles whether the threads created by this ActorSystem should be daemons or not # Toggles whether threads created by this ActorSystem should be daemons or not
daemonic = off daemonic = off
# JVM shutdown, System.exit(-1), in case of a fatal error, such as OutOfMemoryError # JVM shutdown, System.exit(-1), in case of a fatal error, such as
# OutOfMemoryError
jvm-exit-on-fatal-error = on jvm-exit-on-fatal-error = on
actor { actor {
@ -50,15 +51,15 @@ akka {
# another one is akka.remote.RemoteActorRefProvider in the akka-remote bundle. # another one is akka.remote.RemoteActorRefProvider in the akka-remote bundle.
provider = "akka.actor.LocalActorRefProvider" provider = "akka.actor.LocalActorRefProvider"
# The guardian "/user" will use this subclass of akka.actor.SupervisorStrategyConfigurator # The guardian "/user" will use this class to obtain its supervisorStrategy.
# to obtain its supervisorStrategy. Besides the default there is # It needs to be a subclass of akka.actor.SupervisorStrategyConfigurator.
# akka.actor.StoppingSupervisorStrategy # In addition to the default there is akka.actor.StoppingSupervisorStrategy.
guardian-supervisor-strategy = "akka.actor.DefaultSupervisorStrategy" guardian-supervisor-strategy = "akka.actor.DefaultSupervisorStrategy"
# Timeout for ActorSystem.actorOf # Timeout for ActorSystem.actorOf
creation-timeout = 20s creation-timeout = 20s
# frequency with which stopping actors are prodded in case they had to be # Frequency with which stopping actors are prodded in case they had to be
# removed from their parents # removed from their parents
reaper-interval = 5s reaper-interval = 5s
@ -66,12 +67,13 @@ akka {
# this is only intended for testing. # this is only intended for testing.
serialize-messages = off serialize-messages = off
# Serializes and deserializes creators (in Props) to ensure that they can be sent over the network, # Serializes and deserializes creators (in Props) to ensure that they can be
# this is only intended for testing. # sent over the network, this is only intended for testing.
serialize-creators = off serialize-creators = off
# Timeout for send operations to top-level actors which are in the process of being started. # Timeout for send operations to top-level actors which are in the process
# This is only relevant if using a bounded mailbox or the CallingThreadDispatcher for a top-level actor. # of being started. This is only relevant if using a bounded mailbox or the
# CallingThreadDispatcher for a top-level actor.
unstarted-push-timeout = 10s unstarted-push-timeout = 10s
typed { typed {
@ -85,24 +87,28 @@ akka {
default { default {
# routing (load-balance) scheme to use # routing (load-balance) scheme to use
# available: "from-code", "round-robin", "random", "smallest-mailbox", "scatter-gather", "broadcast" # - available: "from-code", "round-robin", "random", "smallest-mailbox",
# or: Fully qualified class name of the router class. # "scatter-gather", "broadcast"
# The router class must extend akka.routing.CustomRouterConfig and and have constructor # - or: Fully qualified class name of the router class.
# with com.typesafe.config.Config parameter. # The class must extend akka.routing.CustomRouterConfig and
# default is "from-code"; # have a constructor with com.typesafe.config.Config
# Whether or not an actor is transformed to a Router is decided in code only (Props.withRouter). # parameter.
# The type of router can be overridden in the configuration; specifying "from-code" means # - default is "from-code";
# that the values specified in the code shall be used. # Whether or not an actor is transformed to a Router is decided in code
# only (Props.withRouter). The type of router can be overridden in the
# configuration; specifying "from-code" means that the values specified
# in the code shall be used.
# In case of routing, the actors to be routed to can be specified # In case of routing, the actors to be routed to can be specified
# in several ways: # in several ways:
# - nr-of-instances: will create that many children # - nr-of-instances: will create that many children
# - routees.paths: will look the paths up using actorFor and route to # - routees.paths: will look the paths up using actorFor and route to
# them, i.e. will not create children # them, i.e. will not create children
# - resizer: dynamically resizable number of routees as specified in resizer below # - resizer: dynamically resizable number of routees as specified in
# resizer below
router = "from-code" router = "from-code"
# number of children to create in case of a non-direct router; this setting # number of children to create in case of a non-direct router; this
# is ignored if routees.paths is given # setting is ignored if routees.paths is given
nr-of-instances = 1 nr-of-instances = 1
# within is the timeout used for routers containing future calls # within is the timeout used for routers containing future calls
@ -118,8 +124,8 @@ akka {
paths = [] paths = []
} }
# Routers with dynamically resizable number of routees; this feature is enabled # Routers with dynamically resizable number of routees; this feature is
# by including (parts of) this section in the deployment # enabled by including (parts of) this section in the deployment
resizer { resizer {
# The fewest number of routees the router should ever have. # The fewest number of routees the router should ever have.
@ -129,8 +135,8 @@ akka {
# Must be greater than or equal to lower-bound. # Must be greater than or equal to lower-bound.
upper-bound = 10 upper-bound = 10
# Threshold to evaluate if routee is considered to be busy (under pressure). # Threshold used to evaluate if a routee is considered to be busy
# Implementation depends on this value (default is 1). # (under pressure). Implementation depends on this value (default is 1).
# 0: number of routees currently processing a message. # 0: number of routees currently processing a message.
# 1: number of routees currently processing a message has # 1: number of routees currently processing a message has
# some messages in mailbox. # some messages in mailbox.
@ -158,9 +164,10 @@ akka {
# capacity is 9 it will request an decrease of 1 routee. # capacity is 9 it will request an decrease of 1 routee.
backoff-rate = 0.1 backoff-rate = 0.1
# When the resizer reduce the capacity the abandoned routee actors are stopped # When the resizer reduce the capacity the abandoned routee actors are
# with PoisonPill after this delay. The reason for the delay is to give concurrent # stopped with PoisonPill after this delay. The reason for the delay is
# messages a chance to be placed in mailbox before sending PoisonPill. # to give concurrent messages a chance to be placed in mailbox before
# sending PoisonPill.
# Use 0s to skip delay. # Use 0s to skip delay.
stop-delay = 1s stop-delay = 1s
@ -178,20 +185,19 @@ akka {
default-dispatcher { default-dispatcher {
# Must be one of the following # Must be one of the following
# Dispatcher, (BalancingDispatcher, only valid when all actors using it are of # Dispatcher, (BalancingDispatcher, only valid when all actors using it are
# the same type), PinnedDispatcher, or a FQCN to a class inheriting # of the same type), PinnedDispatcher, or a FQCN to a class inheriting
# MessageDispatcherConfigurator with a constructor with # MessageDispatcherConfigurator with a constructor with
# com.typesafe.config.Config parameter and akka.dispatch.DispatcherPrerequisites # both com.typesafe.config.Config parameter and
# parameters. # akka.dispatch.DispatcherPrerequisites parameters.
# PinnedDispatcher must be used toghether with executor=thread-pool-executor. # PinnedDispatcher must be used toghether with executor=thread-pool-executor.
type = "Dispatcher" type = "Dispatcher"
# Which kind of ExecutorService to use for this dispatcher # Which kind of ExecutorService to use for this dispatcher
# Valid options: # Valid options:
# "fork-join-executor" requires a "fork-join-executor" section # - "fork-join-executor" requires a "fork-join-executor" section
# "thread-pool-executor" requires a "thread-pool-executor" section # - "thread-pool-executor" requires a "thread-pool-executor" section
# or # - A FQCN of a class extending ExecutorServiceConfigurator
# A FQCN of a class extending ExecutorServiceConfigurator
executor = "fork-join-executor" executor = "fork-join-executor"
# This will be used if you have set "executor = "fork-join-executor"" # This will be used if you have set "executor = "fork-join-executor""
@ -256,12 +262,12 @@ akka {
throughput-deadline-time = 0ms throughput-deadline-time = 0ms
# If negative (or zero) then an unbounded mailbox is used (default) # If negative (or zero) then an unbounded mailbox is used (default)
# If positive then a bounded mailbox is used and the capacity is set using the # If positive then a bounded mailbox is used and the capacity is set using
# property # the property
# NOTE: setting a mailbox to 'blocking' can be a bit dangerous, could lead to # NOTE: setting a mailbox to 'blocking' can be a bit dangerous, could lead
# deadlock, use with care # to deadlock, use with care
# The following mailbox-push-timeout-time is only used for type=Dispatcher and # The following mailbox-push-timeout-time is only used for type=Dispatcher
# only if mailbox-capacity > 0 # and only if mailbox-capacity > 0
mailbox-capacity = -1 mailbox-capacity = -1
# Specifies the timeout to add a new message to a mailbox that is full - # Specifies the timeout to add a new message to a mailbox that is full -
@ -281,18 +287,18 @@ akka {
# For Actor with Stash: The default capacity of the stash. # For Actor with Stash: The default capacity of the stash.
# If negative (or zero) then an unbounded stash is used (default) # If negative (or zero) then an unbounded stash is used (default)
# If positive then a bounded stash is used and the capacity is set using the # If positive then a bounded stash is used and the capacity is set using
# property # the property
stash-capacity = -1 stash-capacity = -1
} }
debug { debug {
# enable function of Actor.loggable(), which is to log any received message at # enable function of Actor.loggable(), which is to log any received message
# DEBUG level, see the “Testing Actor Systems” section of the Akka Documentation # at DEBUG level, see the “Testing Actor Systems” section of the Akka
# at http://akka.io/docs # Documentation at http://akka.io/docs
receive = off receive = off
# enable DEBUG logging of all AutoReceiveMessages (Kill, PoisonPill and the like) # enable DEBUG logging of all AutoReceiveMessages (Kill, PoisonPill et.c.)
autoreceive = off autoreceive = off
# enable DEBUG logging of actor lifecycle changes # enable DEBUG logging of actor lifecycle changes
@ -318,8 +324,9 @@ akka {
} }
# Class to Serializer binding. You only need to specify the name of an interface # Class to Serializer binding. You only need to specify the name of an interface
# or abstract base class of the messages. In case of ambiguity it is using the # or abstract base class of the messages. In case of ambiguity it is using
# most specific configured class, or giving a warning and choosing the “first” one. # the most specific configured class, or giving a warning and choosing the
# “first” one.
# #
# To disable one of the default serializers, assign its class to "none", like # To disable one of the default serializers, assign its class to "none", like
# "java.io.Serializable" = none # "java.io.Serializable" = none
@ -330,8 +337,8 @@ akka {
# Configuration items which are used by the akka.actor.ActorDSL._ methods # Configuration items which are used by the akka.actor.ActorDSL._ methods
dsl { dsl {
# Maximum queue size of the actor created by newInbox(); this protects against # Maximum queue size of the actor created by newInbox(); this protects
# faulty programs which use select() and consistently miss messages # against faulty programs which use select() and consistently miss messages
inbox-size = 1000 inbox-size = 1000
# Default timeout to assume for operations like Inbox.receive et al # Default timeout to assume for operations like Inbox.receive et al
@ -340,16 +347,18 @@ akka {
} }
# Used to set the behavior of the scheduler. # Used to set the behavior of the scheduler.
# Changing the default values may change the system behavior drastically so make sure # Changing the default values may change the system behavior drastically so make
# you know what you're doing! See the Scheduler section of the Akka documentation for more details. # sure you know what you're doing! See the Scheduler section of the Akka
# Documentation for more details.
scheduler { scheduler {
# The HashedWheelTimer (HWT) implementation from Netty is used as the default scheduler # The HashedWheelTimer (HWT) implementation from Netty is used as the default
# in the system. # scheduler in the system.
# HWT does not execute the scheduled tasks on exact time. # HWT does not execute the scheduled tasks on exact time.
# It will, on every tick, check if there are any tasks behind the schedule and execute them. # It will, on every tick, check if there are any tasks behind the schedule
# You can increase or decrease the accuracy of the execution timing by specifying smaller # and execute them. You can increase or decrease the accuracy of the execution
# or larger tick duration. # timing by specifying smaller or larger tick duration.
# If you are scheduling a lot of tasks you should consider increasing the ticks per wheel. # If you are scheduling a lot of tasks you should consider increasing the
# ticks per wheel.
# For more information see: http://www.jboss.org/netty/ # For more information see: http://www.jboss.org/netty/
tick-duration = 100ms tick-duration = 100ms
ticks-per-wheel = 512 ticks-per-wheel = 512

View file

@ -21,63 +21,63 @@ import java.util.concurrent.Callable;
//#circuit-breaker-initialization //#circuit-breaker-initialization
public class DangerousJavaActor extends UntypedActor { public class DangerousJavaActor extends UntypedActor {
private final CircuitBreaker breaker; private final CircuitBreaker breaker;
private final LoggingAdapter log = Logging.getLogger(getContext().system(), this); private final LoggingAdapter log = Logging.getLogger(getContext().system(), this);
public DangerousJavaActor() { public DangerousJavaActor() {
this.breaker = new CircuitBreaker( this.breaker = new CircuitBreaker(
getContext().dispatcher(), getContext().system().scheduler(), getContext().dispatcher(), getContext().system().scheduler(),
5, Duration.create(10, "s"), Duration.create(1, "m")) 5, Duration.create(10, "s"), Duration.create(1, "m"))
.onOpen(new Callable<Object>() { .onOpen(new Callable<Object>() {
public Object call() throws Exception { public Object call() throws Exception {
notifyMeOnOpen(); notifyMeOnOpen();
return null; return null;
} }
}); });
} }
public void notifyMeOnOpen() { public void notifyMeOnOpen() {
log.warning("My CircuitBreaker is now open, and will not close for one minute"); log.warning("My CircuitBreaker is now open, and will not close for one minute");
} }
//#circuit-breaker-initialization //#circuit-breaker-initialization
//#circuit-breaker-usage //#circuit-breaker-usage
public String dangerousCall() { public String dangerousCall() {
return "This really isn't that dangerous of a call after all"; return "This really isn't that dangerous of a call after all";
} }
@Override @Override
public void onReceive(Object message) { public void onReceive(Object message) {
if (message instanceof String) { if (message instanceof String) {
String m = (String) message; String m = (String) message;
if ("is my middle name".equals(m)) { if ("is my middle name".equals(m)) {
final Future<String> f = future( final Future<String> f = future(
new Callable<String>() { new Callable<String>() {
public String call() { public String call() {
return dangerousCall(); return dangerousCall();
} }
}, getContext().dispatcher()); }, getContext().dispatcher());
getSender().tell(breaker getSender().tell(breaker
.callWithCircuitBreaker( .callWithCircuitBreaker(
new Callable<Future<String>>() { new Callable<Future<String>>() {
public Future<String> call() throws Exception { public Future<String> call() throws Exception {
return f; return f;
} }
}), getSelf()); }), getSelf());
} }
if ("block for me".equals(m)) { if ("block for me".equals(m)) {
getSender().tell(breaker getSender().tell(breaker
.callWithSyncCircuitBreaker( .callWithSyncCircuitBreaker(
new Callable<String>() { new Callable<String>() {
@Override @Override
public String call() throws Exception { public String call() throws Exception {
return dangerousCall(); return dangerousCall();
} }
}), getSelf()); }), getSelf());
} }
}
} }
}
//#circuit-breaker-usage //#circuit-breaker-usage
} }

View file

@ -12,7 +12,7 @@ object Scala {
val threemillis = 3.millis val threemillis = 3.millis
val diff = fivesec - threemillis val diff = fivesec - threemillis
assert(diff < fivesec) assert(diff < fivesec)
val fourmillis = threemillis * 4 / 3 // though you cannot write it the other way around val fourmillis = threemillis * 4 / 3 // you cannot write it the other way around
val n = threemillis / (1 millisecond) val n = threemillis / (1 millisecond)
//#dsl //#dsl

View file

@ -98,9 +98,9 @@ Each actor path has an address component, describing the protocol and location
by which the corresponding actor is reachable, followed by the names of the by which the corresponding actor is reachable, followed by the names of the
actors in the hierarchy from the root up. Examples are:: actors in the hierarchy from the root up. Examples are::
"akka://my-system/user/service-a/worker1" // purely local "akka://my-sys/user/service-a/worker1" // purely local
"akka://my-system@serv.example.com:5678/user/service-b" // local or remote "akka://my-sys@host.example.com:5678/user/service-b" // local or remote
"cluster://my-cluster/service-c" // clustered (Future Extension) "cluster://my-cluster/service-c" // clustered (Future Extension)
Here, ``akka`` is the default remote protocol for the 2.0 release, and others Here, ``akka`` is the default remote protocol for the 2.0 release, and others
are pluggable. The interpretation of the host & port part (i.e. are pluggable. The interpretation of the host & port part (i.e.

View file

@ -224,7 +224,8 @@ differentiate actor systems within the hierarchy of the configuration::
val config = ConfigFactory.load() val config = ConfigFactory.load()
val app1 = ActorSystem("MyApp1", config.getConfig("myapp1").withFallback(config)) val app1 = ActorSystem("MyApp1", config.getConfig("myapp1").withFallback(config))
val app2 = ActorSystem("MyApp2", config.getConfig("myapp2").withOnlyPath("akka").withFallback(config)) val app2 = ActorSystem("MyApp2",
config.getConfig("myapp2").withOnlyPath("akka").withFallback(config))
These two samples demonstrate different variations of the “lift-a-subtree” These two samples demonstrate different variations of the “lift-a-subtree”
trick: in the first case, the configuration accessible from within the actor trick: in the first case, the configuration accessible from within the actor

View file

@ -22,8 +22,9 @@ import akka.testkit.AkkaSpec;
public class FSMDocTestBase { public class FSMDocTestBase {
static
//#data //#data
public static final class SetTarget { public final class SetTarget {
final ActorRef ref; final ActorRef ref;
public SetTarget(ActorRef ref) { public SetTarget(ActorRef ref) {
@ -31,7 +32,10 @@ public class FSMDocTestBase {
} }
} }
public static final class Queue { //#data
static
//#data
public final class Queue {
final Object o; final Object o;
public Queue(Object o) { public Queue(Object o) {
@ -39,9 +43,15 @@ public class FSMDocTestBase {
} }
} }
public static final Object flush = new Object(); //#data
static
//#data
public final Object flush = new Object();
public static final class Batch { //#data
static
//#data
public final class Batch {
final List<Object> objects; final List<Object> objects;
public Batch(List<Object> objects) { public Batch(List<Object> objects) {
@ -51,8 +61,9 @@ public class FSMDocTestBase {
//#data //#data
static
//#base //#base
static abstract class MyFSMBase extends UntypedActor { public abstract class MyFSMBase extends UntypedActor {
/* /*
* This is the mutable state of this state machine. * This is the mutable state of this state machine.
@ -118,10 +129,12 @@ public class FSMDocTestBase {
//#base //#base
static
//#actor //#actor
static public class MyFSM extends MyFSMBase { public class MyFSM extends MyFSMBase {
private final LoggingAdapter log = Logging.getLogger(getContext().system(), this); private final LoggingAdapter log =
Logging.getLogger(getContext().system(), this);
@Override @Override
public void onReceive(Object o) { public void onReceive(Object o) {

View file

@ -35,11 +35,13 @@ import org.junit.AfterClass;
//#testkit //#testkit
public class FaultHandlingTestBase { public class FaultHandlingTestBase {
//#testkit //#testkit
static
//#supervisor //#supervisor
static public class Supervisor extends UntypedActor { public class Supervisor extends UntypedActor {
//#strategy //#strategy
private static SupervisorStrategy strategy = new OneForOneStrategy(10, Duration.parse("1 minute"), private static SupervisorStrategy strategy =
new OneForOneStrategy(10, Duration.parse("1 minute"),
new Function<Throwable, Directive>() { new Function<Throwable, Directive>() {
@Override @Override
public Directive apply(Throwable t) { public Directive apply(Throwable t) {
@ -73,11 +75,13 @@ public class FaultHandlingTestBase {
//#supervisor //#supervisor
static
//#supervisor2 //#supervisor2
static public class Supervisor2 extends UntypedActor { public class Supervisor2 extends UntypedActor {
//#strategy2 //#strategy2
private static SupervisorStrategy strategy = new OneForOneStrategy(10, Duration.parse("1 minute"), private static SupervisorStrategy strategy = new OneForOneStrategy(10,
Duration.parse("1 minute"),
new Function<Throwable, Directive>() { new Function<Throwable, Directive>() {
@Override @Override
public Directive apply(Throwable t) { public Directive apply(Throwable t) {
@ -116,8 +120,9 @@ public class FaultHandlingTestBase {
//#supervisor2 //#supervisor2
static
//#child //#child
static public class Child extends UntypedActor { public class Child extends UntypedActor {
int state = 0; int state = 0;
public void onReceive(Object o) throws Exception { public void onReceive(Object o) throws Exception {
@ -163,7 +168,8 @@ public class FaultHandlingTestBase {
//#create //#create
Props superprops = new Props(Supervisor.class); Props superprops = new Props(Supervisor.class);
ActorRef supervisor = system.actorOf(superprops, "supervisor"); ActorRef supervisor = system.actorOf(superprops, "supervisor");
ActorRef child = (ActorRef) Await.result(ask(supervisor, new Props(Child.class), 5000), timeout); ActorRef child = (ActorRef) Await.result(ask(supervisor,
new Props(Child.class), 5000), timeout);
//#create //#create
//#resume //#resume
@ -186,7 +192,8 @@ public class FaultHandlingTestBase {
//#stop //#stop
//#escalate-kill //#escalate-kill
child = (ActorRef) Await.result(ask(supervisor, new Props(Child.class), 5000), timeout); child = (ActorRef) Await.result(ask(supervisor,
new Props(Child.class), 5000), timeout);
probe.watch(child); probe.watch(child);
assert Await.result(ask(child, "get", 5000), timeout).equals(0); assert Await.result(ask(child, "get", 5000), timeout).equals(0);
child.tell(new Exception(), null); child.tell(new Exception(), null);
@ -196,7 +203,8 @@ public class FaultHandlingTestBase {
//#escalate-restart //#escalate-restart
superprops = new Props(Supervisor2.class); superprops = new Props(Supervisor2.class);
supervisor = system.actorOf(superprops); supervisor = system.actorOf(superprops);
child = (ActorRef) Await.result(ask(supervisor, new Props(Child.class), 5000), timeout); child = (ActorRef) Await.result(ask(supervisor,
new Props(Child.class), 5000), timeout);
child.tell(23, null); child.tell(23, null);
assert Await.result(ask(child, "get", 5000), timeout).equals(23); assert Await.result(ask(child, "get", 5000), timeout).equals(23);
child.tell(new Exception(), null); child.tell(new Exception(), null);
@ -207,7 +215,8 @@ public class FaultHandlingTestBase {
//#testkit //#testkit
public <A> Seq<A> seq(A... args) { public <A> Seq<A> seq(A... args) {
return JavaConverters.collectionAsScalaIterableConverter(java.util.Arrays.asList(args)).asScala().toSeq(); return JavaConverters.collectionAsScalaIterableConverter(
java.util.Arrays.asList(args)).asScala().toSeq();
} }
//#testkit //#testkit
} }

View file

@ -7,14 +7,12 @@ package docs.actor;
import akka.actor.Props; import akka.actor.Props;
import scala.concurrent.util.Duration; import scala.concurrent.util.Duration;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
//#imports1 //#imports1
//#imports2 //#imports2
import akka.actor.UntypedActor; import akka.actor.UntypedActor;
import akka.actor.UntypedActorFactory; import akka.actor.UntypedActorFactory;
import akka.actor.Cancellable; import akka.actor.Cancellable;
//#imports2 //#imports2
import akka.actor.ActorRef; import akka.actor.ActorRef;
@ -44,17 +42,17 @@ public class SchedulerDocTestBase {
@Test @Test
public void scheduleOneOffTask() { public void scheduleOneOffTask() {
//#schedule-one-off-message //#schedule-one-off-message
//Schedules to send the "foo"-message to the testActor after 50ms system.scheduler().scheduleOnce(Duration.create(50, TimeUnit.MILLISECONDS),
system.scheduler().scheduleOnce(Duration.create(50, TimeUnit.MILLISECONDS), testActor, "foo", system.dispatcher()); testActor, "foo", system.dispatcher());
//#schedule-one-off-message //#schedule-one-off-message
//#schedule-one-off-thunk //#schedule-one-off-thunk
//Schedules a Runnable to be executed (send the current time) to the testActor after 50ms system.scheduler().scheduleOnce(Duration.create(50, TimeUnit.MILLISECONDS),
system.scheduler().scheduleOnce(Duration.create(50, TimeUnit.MILLISECONDS), new Runnable() { new Runnable() {
@Override @Override
public void run() { public void run() {
testActor.tell(System.currentTimeMillis(), null); testActor.tell(System.currentTimeMillis(), null);
} }
}, system.dispatcher()); }, system.dispatcher());
//#schedule-one-off-thunk //#schedule-one-off-thunk
} }
@ -62,24 +60,26 @@ public class SchedulerDocTestBase {
@Test @Test
public void scheduleRecurringTask() { public void scheduleRecurringTask() {
//#schedule-recurring //#schedule-recurring
ActorRef tickActor = system.actorOf(new Props().withCreator(new UntypedActorFactory() { ActorRef tickActor = system.actorOf(new Props().withCreator(
public UntypedActor create() { new UntypedActorFactory() {
return new UntypedActor() { public UntypedActor create() {
public void onReceive(Object message) { return new UntypedActor() {
if (message.equals("Tick")) { public void onReceive(Object message) {
// Do someting if (message.equals("Tick")) {
} else { // Do someting
unhandled(message); } else {
unhandled(message);
}
} }
} };
}; }
} }));
}));
//This will schedule to send the Tick-message //This will schedule to send the Tick-message
//to the tickActor after 0ms repeating every 50ms //to the tickActor after 0ms repeating every 50ms
Cancellable cancellable = system.scheduler().schedule(Duration.Zero(), Duration.create(50, TimeUnit.MILLISECONDS), Cancellable cancellable = system.scheduler().schedule(Duration.Zero(),
tickActor, "Tick", system.dispatcher()); Duration.create(50, TimeUnit.MILLISECONDS), tickActor, "Tick",
system.dispatcher());
//This cancels further Ticks to be sent //This cancels further Ticks to be sent
cancellable.cancel(); cancellable.cancel();

View file

@ -23,8 +23,9 @@ public class TypedActorDocTestBase {
Object someReference = null; Object someReference = null;
ActorSystem system = null; ActorSystem system = null;
static
//#typed-actor-iface //#typed-actor-iface
public static interface Squarer { public interface Squarer {
//#typed-actor-iface-methods //#typed-actor-iface-methods
void squareDontCare(int i); //fire-forget void squareDontCare(int i); //fire-forget
@ -37,8 +38,9 @@ public class TypedActorDocTestBase {
} }
//#typed-actor-iface //#typed-actor-iface
static
//#typed-actor-impl //#typed-actor-impl
static class SquarerImpl implements Squarer { class SquarerImpl implements Squarer {
private String name; private String name;
public SquarerImpl() { public SquarerImpl() {
@ -107,14 +109,16 @@ public class TypedActorDocTestBase {
try { try {
//#typed-actor-create1 //#typed-actor-create1
Squarer mySquarer = Squarer mySquarer =
TypedActor.get(system).typedActorOf(new TypedProps<SquarerImpl>(Squarer.class, SquarerImpl.class)); TypedActor.get(system).typedActorOf(
new TypedProps<SquarerImpl>(Squarer.class, SquarerImpl.class));
//#typed-actor-create1 //#typed-actor-create1
//#typed-actor-create2 //#typed-actor-create2
Squarer otherSquarer = Squarer otherSquarer =
TypedActor.get(system).typedActorOf(new TypedProps<SquarerImpl>(Squarer.class, TypedActor.get(system).typedActorOf(
new Creator<SquarerImpl>() { new TypedProps<SquarerImpl>(Squarer.class,
public SquarerImpl create() { return new SquarerImpl("foo"); } new Creator<SquarerImpl>() {
}), public SquarerImpl create() { return new SquarerImpl("foo"); }
}),
"name"); "name");
//#typed-actor-create2 //#typed-actor-create2
@ -136,7 +140,8 @@ public class TypedActorDocTestBase {
//#typed-actor-call-strict //#typed-actor-call-strict
//#typed-actor-calls //#typed-actor-calls
assertEquals(100, Await.result(fSquare, Duration.create(3, TimeUnit.SECONDS)).intValue()); assertEquals(100, Await.result(fSquare,
Duration.create(3, TimeUnit.SECONDS)).intValue());
assertEquals(100, oSquare.get().intValue()); assertEquals(100, oSquare.get().intValue());
@ -150,26 +155,26 @@ public class TypedActorDocTestBase {
TypedActor.get(system).poisonPill(otherSquarer); TypedActor.get(system).poisonPill(otherSquarer);
//#typed-actor-poisonpill //#typed-actor-poisonpill
} catch(Exception e) { } catch(Exception e) {
//Ignore //Ignore
} }
} }
@Test public void createHierarchies() { @Test public void createHierarchies() {
try { try {
//#typed-actor-hierarchy //#typed-actor-hierarchy
Squarer childSquarer = Squarer childSquarer =
TypedActor.get(TypedActor.context()). TypedActor.get(TypedActor.context()).
typedActorOf( typedActorOf(
new TypedProps<SquarerImpl>(Squarer.class, SquarerImpl.class) new TypedProps<SquarerImpl>(Squarer.class, SquarerImpl.class)
); );
//Use "childSquarer" as a Squarer //Use "childSquarer" as a Squarer
//#typed-actor-hierarchy //#typed-actor-hierarchy
} catch (Exception e) { } catch (Exception e) {
//dun care //dun care
}
} }
}
@Test public void proxyAnyActorRef() { @Test public void proxyAnyActorRef() {
try { try {
//#typed-actor-remote //#typed-actor-remote
Squarer typedActor = Squarer typedActor =

View file

@ -122,7 +122,8 @@ public class UntypedActorDocTestBase {
public void propsActorOf() { public void propsActorOf() {
ActorSystem system = ActorSystem.create("MySystem"); ActorSystem system = ActorSystem.create("MySystem");
//#creating-props //#creating-props
ActorRef myActor = system.actorOf(new Props(MyUntypedActor.class).withDispatcher("my-dispatcher"), "myactor"); ActorRef myActor = system.actorOf(
new Props(MyUntypedActor.class).withDispatcher("my-dispatcher"), "myactor");
//#creating-props //#creating-props
myActor.tell("test", null); myActor.tell("test", null);
system.shutdown(); system.shutdown();
@ -201,7 +202,8 @@ public class UntypedActorDocTestBase {
ActorRef actorRef = system.actorOf(new Props(MyUntypedActor.class)); ActorRef actorRef = system.actorOf(new Props(MyUntypedActor.class));
//#gracefulStop //#gracefulStop
try { try {
Future<Boolean> stopped = gracefulStop(actorRef, Duration.create(5, TimeUnit.SECONDS), system); Future<Boolean> stopped = gracefulStop(actorRef,
Duration.create(5, TimeUnit.SECONDS), system);
Await.result(stopped, Duration.create(6, TimeUnit.SECONDS)); Await.result(stopped, Duration.create(6, TimeUnit.SECONDS));
// the actor has been stopped // the actor has been stopped
} catch (AskTimeoutException e) { } catch (AskTimeoutException e) {
@ -234,16 +236,18 @@ public class UntypedActorDocTestBase {
futures.add(ask(actorA, "request", 1000)); // using 1000ms timeout futures.add(ask(actorA, "request", 1000)); // using 1000ms timeout
futures.add(ask(actorB, "another request", t)); // using timeout from above futures.add(ask(actorB, "another request", t)); // using timeout from above
final Future<Iterable<Object>> aggregate = Futures.sequence(futures, system.dispatcher()); final Future<Iterable<Object>> aggregate =
Futures.sequence(futures, system.dispatcher());
final Future<Result> transformed = aggregate.map(new Mapper<Iterable<Object>, Result>() { final Future<Result> transformed = aggregate.map(
public Result apply(Iterable<Object> coll) { new Mapper<Iterable<Object>, Result>() {
final Iterator<Object> it = coll.iterator(); public Result apply(Iterable<Object> coll) {
final String s = (String) it.next(); final Iterator<Object> it = coll.iterator();
final int x = (Integer) it.next(); final String s = (String) it.next();
return new Result(x, s); final int x = (Integer) it.next();
} return new Result(x, s);
}, system.dispatcher()); }
}, system.dispatcher());
pipe(transformed, system.dispatcher()).to(actorC); pipe(transformed, system.dispatcher()).to(actorC);
//#ask-pipe //#ask-pipe
@ -305,8 +309,9 @@ public class UntypedActorDocTestBase {
} }
} }
static
//#hot-swap-actor //#hot-swap-actor
public static class HotSwapActor extends UntypedActor { public class HotSwapActor extends UntypedActor {
Procedure<Object> angry = new Procedure<Object>() { Procedure<Object> angry = new Procedure<Object>() {
@Override @Override
@ -343,8 +348,9 @@ public class UntypedActorDocTestBase {
//#hot-swap-actor //#hot-swap-actor
static
//#stash //#stash
public static class ActorWithProtocol extends UntypedActorWithStash { public class ActorWithProtocol extends UntypedActorWithStash {
private Boolean isOpen = false; private Boolean isOpen = false;
public void onReceive(Object msg) { public void onReceive(Object msg) {
if (isOpen) { if (isOpen) {
@ -368,11 +374,12 @@ public class UntypedActorDocTestBase {
} }
//#stash //#stash
static
//#watch //#watch
public static class WatchActor extends UntypedActor { public class WatchActor extends UntypedActor {
final ActorRef child = this.getContext().actorOf(Props.empty(), "child"); final ActorRef child = this.getContext().actorOf(Props.empty(), "child");
{ {
this.getContext().watch(child); // <-- this is the only call needed for registration this.getContext().watch(child); // <-- the only call needed for registration
} }
ActorRef lastSender = getContext().system().deadLetters(); ActorRef lastSender = getContext().system().deadLetters();

View file

@ -39,7 +39,8 @@ public class FaultHandlingDocSample {
* Runs the sample * Runs the sample
*/ */
public static void main(String[] args) { public static void main(String[] args) {
Config config = ConfigFactory.parseString("akka.loglevel = DEBUG \n" + "akka.actor.debug.lifecycle = on"); Config config = ConfigFactory.parseString("akka.loglevel = DEBUG \n" +
"akka.actor.debug.lifecycle = on");
ActorSystem system = ActorSystem.create("FaultToleranceSample", config); ActorSystem system = ActorSystem.create("FaultToleranceSample", config);
ActorRef worker = system.actorOf(new Props(Worker.class), "worker"); ActorRef worker = system.actorOf(new Props(Worker.class), "worker");
@ -59,7 +60,8 @@ public class FaultHandlingDocSample {
@Override @Override
public void preStart() { public void preStart() {
// If we don't get any progress within 15 seconds then the service is unavailable // If we don't get any progress within 15 seconds then the service
// is unavailable
getContext().setReceiveTimeout(Duration.parse("15 seconds")); getContext().setReceiveTimeout(Duration.parse("15 seconds"));
} }
@ -111,23 +113,25 @@ public class FaultHandlingDocSample {
final LoggingAdapter log = Logging.getLogger(getContext().system(), this); final LoggingAdapter log = Logging.getLogger(getContext().system(), this);
final Timeout askTimeout = new Timeout(Duration.create(5, "seconds")); final Timeout askTimeout = new Timeout(Duration.create(5, "seconds"));
// The sender of the initial Start message will continuously be notified about progress // The sender of the initial Start message will continuously be notified
// about progress
ActorRef progressListener; ActorRef progressListener;
final ActorRef counterService = getContext().actorOf(new Props(CounterService.class), "counter"); final ActorRef counterService = getContext().actorOf(
new Props(CounterService.class), "counter");
final int totalCount = 51; final int totalCount = 51;
// Stop the CounterService child if it throws ServiceUnavailable // Stop the CounterService child if it throws ServiceUnavailable
private static SupervisorStrategy strategy = new OneForOneStrategy(-1, Duration.Inf(), private static SupervisorStrategy strategy = new OneForOneStrategy(-1,
new Function<Throwable, Directive>() { Duration.Inf(), new Function<Throwable, Directive>() {
@Override @Override
public Directive apply(Throwable t) { public Directive apply(Throwable t) {
if (t instanceof ServiceUnavailable) { if (t instanceof ServiceUnavailable) {
return stop(); return stop();
} else { } else {
return escalate(); return escalate();
} }
} }
}); });
@Override @Override
public SupervisorStrategy supervisorStrategy() { public SupervisorStrategy supervisorStrategy() {
@ -139,7 +143,8 @@ public class FaultHandlingDocSample {
if (msg.equals(Start) && progressListener == null) { if (msg.equals(Start) && progressListener == null) {
progressListener = getSender(); progressListener = getSender();
getContext().system().scheduler().schedule( getContext().system().scheduler().schedule(
Duration.Zero(), Duration.create(1, "second"), getSelf(), Do, getContext().dispatcher() Duration.Zero(), Duration.create(1, "second"), getSelf(), Do,
getContext().dispatcher()
); );
} else if (msg.equals(Do)) { } else if (msg.equals(Do)) {
counterService.tell(new Increment(1), getSelf()); counterService.tell(new Increment(1), getSelf());
@ -231,17 +236,17 @@ public class FaultHandlingDocSample {
// Restart the storage child when StorageException is thrown. // Restart the storage child when StorageException is thrown.
// After 3 restarts within 5 seconds it will be stopped. // After 3 restarts within 5 seconds it will be stopped.
private static SupervisorStrategy strategy = new OneForOneStrategy(3, Duration.parse("5 seconds"), private static SupervisorStrategy strategy = new OneForOneStrategy(3,
new Function<Throwable, Directive>() { Duration.parse("5 seconds"), new Function<Throwable, Directive>() {
@Override @Override
public Directive apply(Throwable t) { public Directive apply(Throwable t) {
if (t instanceof StorageException) { if (t instanceof StorageException) {
return restart(); return restart();
} else { } else {
return escalate(); return escalate();
} }
} }
}); });
@Override @Override
public SupervisorStrategy supervisorStrategy() { public SupervisorStrategy supervisorStrategy() {
@ -261,7 +266,8 @@ public class FaultHandlingDocSample {
* when it has been terminated. * when it has been terminated.
*/ */
void initStorage() { void initStorage() {
storage = getContext().watch(getContext().actorOf(new Props(Storage.class), "storage")); storage = getContext().watch(getContext().actorOf(
new Props(Storage.class), "storage"));
// Tell the counter, if any, to use the new storage // Tell the counter, if any, to use the new storage
if (counter != null) if (counter != null)
counter.tell(new UseStorage(storage), getSelf()); counter.tell(new UseStorage(storage), getSelf());
@ -272,14 +278,16 @@ public class FaultHandlingDocSample {
@Override @Override
public void onReceive(Object msg) { public void onReceive(Object msg) {
log.debug("received message {}", msg); log.debug("received message {}", msg);
if (msg instanceof Entry && ((Entry) msg).key.equals(key) && counter == null) { if (msg instanceof Entry && ((Entry) msg).key.equals(key) &&
counter == null) {
// Reply from Storage of the initial value, now we can create the Counter // Reply from Storage of the initial value, now we can create the Counter
final long value = ((Entry) msg).value; final long value = ((Entry) msg).value;
counter = getContext().actorOf(new Props().withCreator(new UntypedActorFactory() { counter = getContext().actorOf(new Props().withCreator(
public Actor create() { new UntypedActorFactory() {
return new Counter(key, value); public Actor create() {
} return new Counter(key, value);
})); }
}));
// Tell the counter to use current storage // Tell the counter to use current storage
counter.tell(new UseStorage(storage), getSelf()); counter.tell(new UseStorage(storage), getSelf());
// and send the buffered backlog to the counter // and send the buffered backlog to the counter
@ -299,7 +307,8 @@ public class FaultHandlingDocSample {
counter.tell(new UseStorage(null), getSelf()); counter.tell(new UseStorage(null), getSelf());
// Try to re-establish storage after while // Try to re-establish storage after while
getContext().system().scheduler().scheduleOnce( getContext().system().scheduler().scheduleOnce(
Duration.create(10, "seconds"), getSelf(), Reconnect, getContext().dispatcher() Duration.create(10, "seconds"), getSelf(), Reconnect,
getContext().dispatcher()
); );
} else if (msg.equals(Reconnect)) { } else if (msg.equals(Reconnect)) {
// Re-establish storage after the scheduled delay // Re-establish storage after the scheduled delay
@ -310,12 +319,13 @@ public class FaultHandlingDocSample {
} }
void forwardOrPlaceInBacklog(Object msg) { void forwardOrPlaceInBacklog(Object msg) {
// We need the initial value from storage before we can start delegate to the counter. // We need the initial value from storage before we can start delegate to
// Before that we place the messages in a backlog, to be sent to the counter when // the counter. Before that we place the messages in a backlog, to be sent
// it is initialized. // to the counter when it is initialized.
if (counter == null) { if (counter == null) {
if (backlog.size() >= MAX_BACKLOG) if (backlog.size() >= MAX_BACKLOG)
throw new ServiceUnavailable("CounterService not available, lack of initial value"); throw new ServiceUnavailable("CounterService not available," +
" lack of initial value");
backlog.add(new SenderMsgPair(getSender(), msg)); backlog.add(new SenderMsgPair(getSender(), msg));
} else { } else {
counter.forward(msg, getContext()); counter.forward(msg, getContext());
@ -449,7 +459,8 @@ public class FaultHandlingDocSample {
} else if (msg instanceof Get) { } else if (msg instanceof Get) {
Get get = (Get) msg; Get get = (Get) msg;
Long value = db.load(get.key); Long value = db.load(get.key);
getSender().tell(new Entry(get.key, value == null ? Long.valueOf(0L) : value), getSelf()); getSender().tell(new Entry(get.key, value == null ?
Long.valueOf(0L) : value), getSelf());
} else { } else {
unhandled(msg); unhandled(msg);
} }

View file

@ -50,7 +50,8 @@ public class DispatcherDocTestBase {
@Before @Before
public void setUp() { public void setUp() {
system = ActorSystem.create("MySystem", system = ActorSystem.create("MySystem",
ConfigFactory.parseString(DispatcherDocSpec.config()).withFallback(AkkaSpec.testConf())); ConfigFactory.parseString(
DispatcherDocSpec.config()).withFallback(AkkaSpec.testConf()));
} }
@After @After
@ -79,7 +80,7 @@ public class DispatcherDocTestBase {
public void priorityDispatcher() throws Exception { public void priorityDispatcher() throws Exception {
//#prio-dispatcher //#prio-dispatcher
// We create a new Actor that just prints out what it processes // We create a new Actor that just prints out what it processes
ActorRef myActor = system.actorOf( ActorRef myActor = system.actorOf(
new Props().withCreator(new UntypedActorFactory() { new Props().withCreator(new UntypedActorFactory() {
public UntypedActor create() { public UntypedActor create() {
@ -123,9 +124,11 @@ public class DispatcherDocTestBase {
} }
} }
static
//#prio-mailbox //#prio-mailbox
public static class MyPrioMailbox extends UnboundedPriorityMailbox { public class MyPrioMailbox extends UnboundedPriorityMailbox {
public MyPrioMailbox(ActorSystem.Settings settings, Config config) { // needed for reflective instantiation // needed for reflective instantiation
public MyPrioMailbox(ActorSystem.Settings settings, Config config) {
// Create a new PriorityGenerator, lower prio means more important // Create a new PriorityGenerator, lower prio means more important
super(new PriorityGenerator() { super(new PriorityGenerator() {
@Override @Override
@ -144,8 +147,9 @@ public class DispatcherDocTestBase {
} }
//#prio-mailbox //#prio-mailbox
static
//#mailbox-implementation-example //#mailbox-implementation-example
class MyUnboundedMailbox implements MailboxType { public class MyUnboundedMailbox implements MailboxType {
// This constructor signature must exist, it will be called by Akka // This constructor signature must exist, it will be called by Akka
public MyUnboundedMailbox(ActorSystem.Settings settings, Config config) { public MyUnboundedMailbox(ActorSystem.Settings settings, Config config) {
@ -158,7 +162,9 @@ public class DispatcherDocTestBase {
private final Queue<Envelope> queue = new ConcurrentLinkedQueue<Envelope>(); private final Queue<Envelope> queue = new ConcurrentLinkedQueue<Envelope>();
// these must be implemented; queue used as example // these must be implemented; queue used as example
public void enqueue(ActorRef receiver, Envelope handle) { queue.offer(handle); } public void enqueue(ActorRef receiver, Envelope handle) {
queue.offer(handle);
}
public Envelope dequeue() { return queue.poll(); } public Envelope dequeue() { return queue.poll(); }
public int numberOfMessages() { return queue.size(); } public int numberOfMessages() { return queue.size(); }
public boolean hasMessages() { return !queue.isEmpty(); } public boolean hasMessages() { return !queue.isEmpty(); }

View file

@ -76,8 +76,8 @@ public class LoggingDocTestBase {
@Override @Override
public void preRestart(Throwable reason, Option<Object> message) { public void preRestart(Throwable reason, Option<Object> message) {
log.error(reason, "Restarting due to [{}] when processing [{}]", reason.getMessage(), log.error(reason, "Restarting due to [{}] when processing [{}]",
message.isDefined() ? message.get() : ""); reason.getMessage(), message.isDefined() ? message.get() : "");
} }
public void onReceive(Object message) { public void onReceive(Object message) {
@ -109,8 +109,9 @@ public class LoggingDocTestBase {
} }
//#my-event-listener //#my-event-listener
static
//#deadletter-actor //#deadletter-actor
public static class DeadLetterActor extends UntypedActor { public class DeadLetterActor extends UntypedActor {
public void onReceive(Object message) { public void onReceive(Object message) {
if (message instanceof DeadLetter) { if (message instanceof DeadLetter) {
System.out.println(message); System.out.println(message);

View file

@ -13,8 +13,9 @@ import org.junit.Test;
public class ExtensionDocTestBase { public class ExtensionDocTestBase {
static
//#extension //#extension
public static class CountExtensionImpl implements Extension { public class CountExtensionImpl implements Extension {
//Since this Extension is a shared instance //Since this Extension is a shared instance
// per ActorSystem we need to be threadsafe // per ActorSystem we need to be threadsafe
private final AtomicLong counter = new AtomicLong(0); private final AtomicLong counter = new AtomicLong(0);
@ -27,8 +28,10 @@ public class ExtensionDocTestBase {
//#extension //#extension
static
//#extensionid //#extensionid
public static class CountExtension extends AbstractExtensionId<CountExtensionImpl> implements ExtensionIdProvider { public class CountExtension extends AbstractExtensionId<CountExtensionImpl>
implements ExtensionIdProvider {
//This will be the identifier of our CountExtension //This will be the identifier of our CountExtension
public final static CountExtension CountExtensionProvider = new CountExtension(); public final static CountExtension CountExtensionProvider = new CountExtension();
@ -49,10 +52,12 @@ public class ExtensionDocTestBase {
//#extensionid //#extensionid
static
//#extension-usage-actor //#extension-usage-actor
public static class MyActor extends UntypedActor { public class MyActor extends UntypedActor {
public void onReceive(Object msg) { public void onReceive(Object msg) {
// typically you would use static import of CountExtension.CountExtensionProvider field // typically you would use static import of the
// CountExtension.CountExtensionProvider field
CountExtension.CountExtensionProvider.get(getContext().system()).increment(); CountExtension.CountExtensionProvider.get(getContext().system()).increment();
} }
} }
@ -64,7 +69,8 @@ public class ExtensionDocTestBase {
final ActorSystem system = null; final ActorSystem system = null;
try { try {
//#extension-usage //#extension-usage
// typically you would use static import of CountExtension.CountExtensionProvider field // typically you would use static import of the
// CountExtension.CountExtensionProvider field
CountExtension.CountExtensionProvider.get(system).increment(); CountExtension.CountExtensionProvider.get(system).increment();
//#extension-usage //#extension-usage
} catch (Exception e) { } catch (Exception e) {

View file

@ -20,15 +20,17 @@ import org.junit.Test;
public class SettingsExtensionDocTestBase { public class SettingsExtensionDocTestBase {
static
//#extension //#extension
public static class SettingsImpl implements Extension { public class SettingsImpl implements Extension {
public final String DB_URI; public final String DB_URI;
public final Duration CIRCUIT_BREAKER_TIMEOUT; public final Duration CIRCUIT_BREAKER_TIMEOUT;
public SettingsImpl(Config config) { public SettingsImpl(Config config) {
DB_URI = config.getString("myapp.db.uri"); DB_URI = config.getString("myapp.db.uri");
CIRCUIT_BREAKER_TIMEOUT = Duration.create(config.getMilliseconds("myapp.circuit-breaker.timeout"), CIRCUIT_BREAKER_TIMEOUT =
Duration.create(config.getMilliseconds("myapp.circuit-breaker.timeout"),
TimeUnit.MILLISECONDS); TimeUnit.MILLISECONDS);
} }
@ -36,8 +38,10 @@ public class SettingsExtensionDocTestBase {
//#extension //#extension
static
//#extensionid //#extensionid
public static class Settings extends AbstractExtensionId<SettingsImpl> implements ExtensionIdProvider { public class Settings extends AbstractExtensionId<SettingsImpl>
implements ExtensionIdProvider {
public final static Settings SettingsProvider = new Settings(); public final static Settings SettingsProvider = new Settings();
public Settings lookup() { public Settings lookup() {
@ -51,13 +55,16 @@ public class SettingsExtensionDocTestBase {
//#extensionid //#extensionid
static
//#extension-usage-actor //#extension-usage-actor
public static class MyActor extends UntypedActor { public class MyActor extends UntypedActor {
// typically you would use static import of CountExtension.CountExtensionProvider field // typically you would use static import of the Settings.SettingsProvider field
final SettingsImpl settings = Settings.SettingsProvider.get(getContext().system()); final SettingsImpl settings =
Connection connection = connect(settings.DB_URI, settings.CIRCUIT_BREAKER_TIMEOUT); Settings.SettingsProvider.get(getContext().system());
Connection connection =
connect(settings.DB_URI, settings.CIRCUIT_BREAKER_TIMEOUT);
//#extension-usage-actor //#extension-usage-actor
public Connection connect(String dbUri, Duration circuitBreakerTimeout) { public Connection connect(String dbUri, Duration circuitBreakerTimeout) {
return new Connection(); return new Connection();
@ -65,8 +72,9 @@ public class SettingsExtensionDocTestBase {
public void onReceive(Object msg) { public void onReceive(Object msg) {
} }
//#extension-usage-actor
} }
//#extension-usage-actor
public static class Connection { public static class Connection {
} }
@ -76,7 +84,7 @@ public class SettingsExtensionDocTestBase {
final ActorSystem system = null; final ActorSystem system = null;
try { try {
//#extension-usage //#extension-usage
// typically you would use static import of CountExtension.CountExtensionProvider field // typically you would use static import of the Settings.SettingsProvider field
String dbUri = Settings.SettingsProvider.get(system).DB_URI; String dbUri = Settings.SettingsProvider.get(system).DB_URI;
//#extension-usage //#extension-usage
} catch (Exception e) { } catch (Exception e) {

View file

@ -9,7 +9,6 @@ import scala.concurrent.ExecutionContext;
import scala.concurrent.Future; import scala.concurrent.Future;
import scala.concurrent.Await; import scala.concurrent.Await;
import akka.util.Timeout; import akka.util.Timeout;
//#imports1 //#imports1
//#imports2 //#imports2
@ -18,39 +17,32 @@ import akka.japi.Function;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import static akka.dispatch.Futures.future; import static akka.dispatch.Futures.future;
import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.concurrent.TimeUnit.SECONDS;
//#imports2 //#imports2
//#imports3 //#imports3
import static akka.dispatch.Futures.sequence; import static akka.dispatch.Futures.sequence;
//#imports3 //#imports3
//#imports4 //#imports4
import static akka.dispatch.Futures.traverse; import static akka.dispatch.Futures.traverse;
//#imports4 //#imports4
//#imports5 //#imports5
import akka.japi.Function2; import akka.japi.Function2;
import static akka.dispatch.Futures.fold; import static akka.dispatch.Futures.fold;
//#imports5 //#imports5
//#imports6 //#imports6
import static akka.dispatch.Futures.reduce; import static akka.dispatch.Futures.reduce;
//#imports6 //#imports6
//#imports7 //#imports7
import scala.concurrent.ExecutionContext; import scala.concurrent.ExecutionContext;
import scala.concurrent.ExecutionContext$; import scala.concurrent.ExecutionContext$;
//#imports7 //#imports7
//#imports8 //#imports8
import static akka.pattern.Patterns.after; import static akka.pattern.Patterns.after;
//#imports8 //#imports8
import java.util.ArrayList; import java.util.ArrayList;
@ -96,7 +88,7 @@ public class FutureDocTestBase {
//Use ec with your Futures //Use ec with your Futures
Future<String> f1 = Futures.successful("foo"); Future<String> f1 = Futures.successful("foo");
// Then you shut the ExecutorService down somewhere at the end of your program/application. // Then you shut down the ExecutorService at the end of your application.
yourExecutorServiceGoesHere.shutdown(); yourExecutorServiceGoesHere.shutdown();
//#diy-execution-context //#diy-execution-context
} }
@ -236,14 +228,15 @@ public class FutureDocTestBase {
Future<Iterable<Integer>> futureListOfInts = sequence(listOfFutureInts, ec); Future<Iterable<Integer>> futureListOfInts = sequence(listOfFutureInts, ec);
// Find the sum of the odd numbers // Find the sum of the odd numbers
Future<Long> futureSum = futureListOfInts.map(new Mapper<Iterable<Integer>, Long>() { Future<Long> futureSum = futureListOfInts.map(
public Long apply(Iterable<Integer> ints) { new Mapper<Iterable<Integer>, Long>() {
long sum = 0; public Long apply(Iterable<Integer> ints) {
for (Integer i : ints) long sum = 0;
sum += i; for (Integer i : ints)
return sum; sum += i;
} return sum;
}, ec); }
}, ec);
long result = Await.result(futureSum, Duration.create(1, SECONDS)); long result = Await.result(futureSum, Duration.create(1, SECONDS));
//#sequence //#sequence
@ -257,15 +250,16 @@ public class FutureDocTestBase {
//Just a sequence of Strings //Just a sequence of Strings
Iterable<String> listStrings = Arrays.asList("a", "b", "c"); Iterable<String> listStrings = Arrays.asList("a", "b", "c");
Future<Iterable<String>> futureResult = traverse(listStrings, new Function<String, Future<String>>() { Future<Iterable<String>> futureResult = traverse(listStrings,
public Future<String> apply(final String r) { new Function<String, Future<String>>() {
return future(new Callable<String>() { public Future<String> apply(final String r) {
public String call() { return future(new Callable<String>() {
return r.toUpperCase(); public String call() {
} return r.toUpperCase();
}, ec); }
} }, ec);
}, ec); }
}, ec);
//Returns the sequence of strings as upper case //Returns the sequence of strings as upper case
Iterable<String> result = Await.result(futureResult, Duration.create(1, SECONDS)); Iterable<String> result = Await.result(futureResult, Duration.create(1, SECONDS));
@ -286,11 +280,12 @@ public class FutureDocTestBase {
Iterable<Future<String>> futures = source; Iterable<Future<String>> futures = source;
//Start value is the empty string //Start value is the empty string
Future<String> resultFuture = fold("", futures, new Function2<String, String, String>() { Future<String> resultFuture = fold("", futures,
public String apply(String r, String t) { new Function2<String, String, String>() {
return r + t; //Just concatenate public String apply(String r, String t) {
} return r + t; //Just concatenate
}, ec); }
}, ec);
String result = Await.result(resultFuture, Duration.create(1, SECONDS)); String result = Await.result(resultFuture, Duration.create(1, SECONDS));
//#fold //#fold
@ -308,11 +303,12 @@ public class FutureDocTestBase {
//A sequence of Futures, in this case Strings //A sequence of Futures, in this case Strings
Iterable<Future<String>> futures = source; Iterable<Future<String>> futures = source;
Future<Object> resultFuture = reduce(futures, new Function2<Object, String, Object>() { Future<Object> resultFuture = reduce(futures,
public Object apply(Object r, String t) { new Function2<Object, String, Object>() {
return r + t; //Just concatenate public Object apply(Object r, String t) {
} return r + t; //Just concatenate
}, ec); }
}, ec);
Object result = Await.result(resultFuture, Duration.create(1, SECONDS)); Object result = Await.result(resultFuture, Duration.create(1, SECONDS));
//#reduce //#reduce
@ -327,11 +323,13 @@ public class FutureDocTestBase {
Future<String> future = Futures.successful("Yay!"); Future<String> future = Futures.successful("Yay!");
//#successful //#successful
//#failed //#failed
Future<String> otherFuture = Futures.failed(new IllegalArgumentException("Bang!")); Future<String> otherFuture = Futures.failed(
new IllegalArgumentException("Bang!"));
//#failed //#failed
Object result = Await.result(future, Duration.create(1, SECONDS)); Object result = Await.result(future, Duration.create(1, SECONDS));
assertEquals("Yay!", result); assertEquals("Yay!", result);
Throwable result2 = Await.result(otherFuture.failed(), Duration.create(1, SECONDS)); Throwable result2 = Await.result(otherFuture.failed(),
Duration.create(1, SECONDS));
assertEquals("Bang!", result2.getMessage()); assertEquals("Bang!", result2.getMessage());
} }
@ -340,17 +338,19 @@ public class FutureDocTestBase {
//#filter //#filter
final ExecutionContext ec = system.dispatcher(); final ExecutionContext ec = system.dispatcher();
Future<Integer> future1 = Futures.successful(4); Future<Integer> future1 = Futures.successful(4);
Future<Integer> successfulFilter = future1.filter(Filter.filterOf(new Function<Integer, Boolean>() { Future<Integer> successfulFilter = future1.filter(Filter.filterOf(
public Boolean apply(Integer i) { new Function<Integer, Boolean>() {
return i % 2 == 0; public Boolean apply(Integer i) {
} return i % 2 == 0;
}), ec); }
}), ec);
Future<Integer> failedFilter = future1.filter(Filter.filterOf(new Function<Integer, Boolean>() { Future<Integer> failedFilter = future1.filter(Filter.filterOf(
public Boolean apply(Integer i) { new Function<Integer, Boolean>() {
return i % 2 != 0; public Boolean apply(Integer i) {
} return i % 2 != 0;
}), ec); }
}), ec);
//When filter fails, the returned Future will be failed with a scala.MatchError //When filter fails, the returned Future will be failed with a scala.MatchError
//#filter //#filter
} }
@ -367,12 +367,13 @@ public class FutureDocTestBase {
public void useAndThen() { public void useAndThen() {
//#and-then //#and-then
final ExecutionContext ec = system.dispatcher(); final ExecutionContext ec = system.dispatcher();
Future<String> future1 = Futures.successful("value").andThen(new OnComplete<String>() { Future<String> future1 = Futures.successful("value").andThen(
new OnComplete<String>() {
public void onComplete(Throwable failure, String result) { public void onComplete(Throwable failure, String result) {
if (failure != null) if (failure != null)
sendToIssueTracker(failure); sendToIssueTracker(failure);
} }
}, ec).andThen(new OnComplete<String>() { }, ec).andThen(new OnComplete<String>() {
public void onComplete(Throwable failure, String result) { public void onComplete(Throwable failure, String result) {
if (result != null) if (result != null)
sendToTheInternetz(result); sendToTheInternetz(result);
@ -489,11 +490,12 @@ public class FutureDocTestBase {
final ExecutionContext ec = system.dispatcher(); final ExecutionContext ec = system.dispatcher();
Future<String> future1 = Futures.successful("foo"); Future<String> future1 = Futures.successful("foo");
Future<String> future2 = Futures.successful("bar"); Future<String> future2 = Futures.successful("bar");
Future<String> future3 = future1.zip(future2).map(new Mapper<scala.Tuple2<String, String>, String>() { Future<String> future3 = future1.zip(future2).map(
public String apply(scala.Tuple2<String, String> zipped) { new Mapper<scala.Tuple2<String, String>, String>() {
return zipped._1() + " " + zipped._2(); public String apply(scala.Tuple2<String, String> zipped) {
} return zipped._1() + " " + zipped._2();
}, ec); }
}, ec);
String result = Await.result(future3, Duration.create(1, SECONDS)); String result = Await.result(future3, Duration.create(1, SECONDS));
assertEquals("foo bar", result); assertEquals("foo bar", result);
@ -505,7 +507,8 @@ public class FutureDocTestBase {
Future<String> future1 = Futures.failed(new IllegalStateException("OHNOES1")); Future<String> future1 = Futures.failed(new IllegalStateException("OHNOES1"));
Future<String> future2 = Futures.failed(new IllegalStateException("OHNOES2")); Future<String> future2 = Futures.failed(new IllegalStateException("OHNOES2"));
Future<String> future3 = Futures.successful("bar"); Future<String> future3 = Futures.successful("bar");
Future<String> future4 = future1.fallbackTo(future2).fallbackTo(future3); // Will have "bar" in this case // Will have "bar" in this case
Future<String> future4 = future1.fallbackTo(future2).fallbackTo(future3);
String result = Await.result(future4, Duration.create(1, SECONDS)); String result = Await.result(future4, Duration.create(1, SECONDS));
assertEquals("bar", result); assertEquals("bar", result);
//#fallback-to //#fallback-to

View file

@ -39,9 +39,9 @@ public class ConsistentHashingRouterDocTestBase {
system.shutdown(); system.shutdown();
} }
static
//#cache-actor //#cache-actor
public class Cache extends UntypedActor {
public static class Cache extends UntypedActor {
Map<String, String> cache = new HashMap<String, String>(); Map<String, String> cache = new HashMap<String, String>();
public void onReceive(Object msg) { public void onReceive(Object msg) {
@ -62,14 +62,20 @@ public class ConsistentHashingRouterDocTestBase {
} }
} }
public static final class Evict implements Serializable { //#cache-actor
static
//#cache-actor
public final class Evict implements Serializable {
public final String key; public final String key;
public Evict(String key) { public Evict(String key) {
this.key = key; this.key = key;
} }
} }
public static final class Get implements Serializable, ConsistentHashable { //#cache-actor
static
//#cache-actor
public final class Get implements Serializable, ConsistentHashable {
public final String key; public final String key;
public Get(String key) { public Get(String key) {
this.key = key; this.key = key;
@ -79,7 +85,10 @@ public class ConsistentHashingRouterDocTestBase {
} }
} }
public static final class Entry implements Serializable { //#cache-actor
static
//#cache-actor
public final class Entry implements Serializable {
public final String key; public final String key;
public final String value; public final String value;
public Entry(String key, String value) { public Entry(String key, String value) {
@ -88,7 +97,10 @@ public class ConsistentHashingRouterDocTestBase {
} }
} }
public static final String NOT_FOUND = "NOT_FOUND"; //#cache-actor
static
//#cache-actor
public final String NOT_FOUND = "NOT_FOUND";
//#cache-actor //#cache-actor

View file

@ -57,15 +57,18 @@ public class CustomRouterDocTestBase {
public void demonstrateDispatchers() { public void demonstrateDispatchers() {
//#dispatchers //#dispatchers
final ActorRef router = system.actorOf(new Props(MyActor.class) final ActorRef router = system.actorOf(new Props(MyActor.class)
.withRouter(new RoundRobinRouter(5).withDispatcher("head")) // head router runs on "head" dispatcher // head router runs on "head" dispatcher
.withDispatcher("workers")); // MyActor workers run on "workers" dispatcher .withRouter(new RoundRobinRouter(5).withDispatcher("head"))
// MyActor workers run on "workers" dispatcher
.withDispatcher("workers"));
//#dispatchers //#dispatchers
} }
@Test @Test
public void demonstrateSupervisor() { public void demonstrateSupervisor() {
//#supervision //#supervision
final SupervisorStrategy strategy = new OneForOneStrategy(5, Duration.parse("1 minute"), final SupervisorStrategy strategy =
new OneForOneStrategy(5, Duration.parse("1 minute"),
new Class<?>[] { Exception.class }); new Class<?>[] { Exception.class });
final ActorRef router = system.actorOf(new Props(MyActor.class) final ActorRef router = system.actorOf(new Props(MyActor.class)
.withRouter(new RoundRobinRouter(5).withSupervisorStrategy(strategy))); .withRouter(new RoundRobinRouter(5).withSupervisorStrategy(strategy)));
@ -75,15 +78,18 @@ public class CustomRouterDocTestBase {
//#crTest //#crTest
@Test @Test
public void countVotesAsIntendedNotAsInFlorida() throws Exception { public void countVotesAsIntendedNotAsInFlorida() throws Exception {
ActorRef routedActor = system.actorOf(new Props().withRouter(new VoteCountRouter())); ActorRef routedActor = system.actorOf(
new Props().withRouter(new VoteCountRouter()));
routedActor.tell(DemocratVote, null); routedActor.tell(DemocratVote, null);
routedActor.tell(DemocratVote, null); routedActor.tell(DemocratVote, null);
routedActor.tell(RepublicanVote, null); routedActor.tell(RepublicanVote, null);
routedActor.tell(DemocratVote, null); routedActor.tell(DemocratVote, null);
routedActor.tell(RepublicanVote, null); routedActor.tell(RepublicanVote, null);
Timeout timeout = new Timeout(Duration.create(1, "seconds")); Timeout timeout = new Timeout(Duration.create(1, "seconds"));
Future<Object> democratsResult = ask(routedActor, DemocratCountResult, timeout); Future<Object> democratsResult =
Future<Object> republicansResult = ask(routedActor, RepublicanCountResult, timeout); ask(routedActor, DemocratCountResult, timeout);
Future<Object> republicansResult =
ask(routedActor, RepublicanCountResult, timeout);
assertEquals(3, Await.result(democratsResult, timeout.duration())); assertEquals(3, Await.result(democratsResult, timeout.duration()));
assertEquals(2, Await.result(republicansResult, timeout.duration())); assertEquals(2, Await.result(republicansResult, timeout.duration()));
@ -99,8 +105,11 @@ public class CustomRouterDocTestBase {
//#crMessages //#crMessages
//#CustomRouter
static
//#CustomRouter
//#crActors //#crActors
public static class DemocratActor extends UntypedActor { public class DemocratActor extends UntypedActor {
int counter = 0; int counter = 0;
public void onReceive(Object msg) { public void onReceive(Object msg) {
@ -117,7 +126,12 @@ public class CustomRouterDocTestBase {
} }
} }
public static class RepublicanActor extends UntypedActor { //#crActors
//#CustomRouter
static
//#CustomRouter
//#crActors
public class RepublicanActor extends UntypedActor {
int counter = 0; int counter = 0;
public void onReceive(Object msg) { public void onReceive(Object msg) {
@ -135,9 +149,11 @@ public class CustomRouterDocTestBase {
} }
//#crActors //#crActors
//#CustomRouter
static
//#CustomRouter
//#crRouter //#crRouter
public static class VoteCountRouter extends CustomRouterConfig { public class VoteCountRouter extends CustomRouterConfig {
@Override public String routerDispatcher() { @Override public String routerDispatcher() {
return Dispatchers.DefaultDispatcherId(); return Dispatchers.DefaultDispatcherId();
@ -150,9 +166,12 @@ public class CustomRouterDocTestBase {
//#crRoute //#crRoute
@Override @Override
public CustomRoute createCustomRoute(RouteeProvider routeeProvider) { public CustomRoute createCustomRoute(RouteeProvider routeeProvider) {
final ActorRef democratActor = routeeProvider.context().actorOf(new Props(DemocratActor.class), "d"); final ActorRef democratActor =
final ActorRef republicanActor = routeeProvider.context().actorOf(new Props(RepublicanActor.class), "r"); routeeProvider.context().actorOf(new Props(DemocratActor.class), "d");
List<ActorRef> routees = Arrays.asList(new ActorRef[] { democratActor, republicanActor }); final ActorRef republicanActor =
routeeProvider.context().actorOf(new Props(RepublicanActor.class), "r");
List<ActorRef> routees =
Arrays.asList(new ActorRef[] { democratActor, republicanActor });
//#crRegisterRoutees //#crRegisterRoutees
routeeProvider.registerRoutees(routees); routeeProvider.registerRoutees(routees);
@ -165,10 +184,12 @@ public class CustomRouterDocTestBase {
switch ((Message) msg) { switch ((Message) msg) {
case DemocratVote: case DemocratVote:
case DemocratCountResult: case DemocratCountResult:
return Arrays.asList(new Destination[] { new Destination(sender, democratActor) }); return Arrays.asList(
new Destination[] { new Destination(sender, democratActor) });
case RepublicanVote: case RepublicanVote:
case RepublicanCountResult: case RepublicanCountResult:
return Arrays.asList(new Destination[] { new Destination(sender, republicanActor) }); return Arrays.asList(
new Destination[] { new Destination(sender, republicanActor) });
default: default:
throw new IllegalArgumentException("Unknown message: " + msg); throw new IllegalArgumentException("Unknown message: " + msg);
} }
@ -182,5 +203,4 @@ public class CustomRouterDocTestBase {
//#crRouter //#crRouter
//#CustomRouter //#CustomRouter
} }

View file

@ -22,14 +22,16 @@ public class ParentActor extends UntypedActor {
if (msg.equals("rrr")) { if (msg.equals("rrr")) {
//#roundRobinRouter //#roundRobinRouter
ActorRef roundRobinRouter = getContext().actorOf( ActorRef roundRobinRouter = getContext().actorOf(
new Props(PrintlnActor.class).withRouter(new RoundRobinRouter(5)), "router"); new Props(PrintlnActor.class).withRouter(new RoundRobinRouter(5)),
"router");
for (int i = 1; i <= 10; i++) { for (int i = 1; i <= 10; i++) {
roundRobinRouter.tell(i, getSelf()); roundRobinRouter.tell(i, getSelf());
} }
//#roundRobinRouter //#roundRobinRouter
} else if (msg.equals("rr")) { } else if (msg.equals("rr")) {
//#randomRouter //#randomRouter
ActorRef randomRouter = getContext().actorOf(new Props(PrintlnActor.class).withRouter(new RandomRouter(5)), ActorRef randomRouter = getContext().actorOf(
new Props(PrintlnActor.class).withRouter(new RandomRouter(5)),
"router"); "router");
for (int i = 1; i <= 10; i++) { for (int i = 1; i <= 10; i++) {
randomRouter.tell(i, getSelf()); randomRouter.tell(i, getSelf());
@ -38,28 +40,32 @@ public class ParentActor extends UntypedActor {
} else if (msg.equals("smr")) { } else if (msg.equals("smr")) {
//#smallestMailboxRouter //#smallestMailboxRouter
ActorRef smallestMailboxRouter = getContext().actorOf( ActorRef smallestMailboxRouter = getContext().actorOf(
new Props(PrintlnActor.class).withRouter(new SmallestMailboxRouter(5)), "router"); new Props(PrintlnActor.class).withRouter(new SmallestMailboxRouter(5)),
"router");
for (int i = 1; i <= 10; i++) { for (int i = 1; i <= 10; i++) {
smallestMailboxRouter.tell(i, getSelf()); smallestMailboxRouter.tell(i, getSelf());
} }
//#smallestMailboxRouter //#smallestMailboxRouter
} else if (msg.equals("br")) { } else if (msg.equals("br")) {
//#broadcastRouter //#broadcastRouter
ActorRef broadcastRouter = getContext().actorOf(new Props(PrintlnActor.class).withRouter(new BroadcastRouter(5)), ActorRef broadcastRouter = getContext().actorOf(
"router"); new Props(PrintlnActor.class).withRouter(new BroadcastRouter(5)), "router");
broadcastRouter.tell("this is a broadcast message", getSelf()); broadcastRouter.tell("this is a broadcast message", getSelf());
//#broadcastRouter //#broadcastRouter
} else if (msg.equals("sgfcr")) { } else if (msg.equals("sgfcr")) {
//#scatterGatherFirstCompletedRouter //#scatterGatherFirstCompletedRouter
ActorRef scatterGatherFirstCompletedRouter = getContext().actorOf( ActorRef scatterGatherFirstCompletedRouter = getContext().actorOf(
new Props(FibonacciActor.class).withRouter(new ScatterGatherFirstCompletedRouter(5, Duration new Props(FibonacciActor.class).withRouter(
.create(2, "seconds"))), "router"); new ScatterGatherFirstCompletedRouter(5, Duration.create(2, "seconds"))),
"router");
Timeout timeout = new Timeout(Duration.create(5, "seconds")); Timeout timeout = new Timeout(Duration.create(5, "seconds"));
Future<Object> futureResult = akka.pattern.Patterns.ask(scatterGatherFirstCompletedRouter, Future<Object> futureResult = akka.pattern.Patterns.ask(
new FibonacciActor.FibonacciNumber(10), timeout); scatterGatherFirstCompletedRouter, new FibonacciActor.FibonacciNumber(10),
timeout);
int result = (Integer) Await.result(futureResult, timeout.duration()); int result = (Integer) Await.result(futureResult, timeout.duration());
//#scatterGatherFirstCompletedRouter //#scatterGatherFirstCompletedRouter
System.out.println(String.format("The result of calculating Fibonacci for 10 is %d", result)); System.out.println(
String.format("The result of calculating Fibonacci for 10 is %d", result));
} else { } else {
unhandled(msg); unhandled(msg);
} }

View file

@ -8,7 +8,8 @@ import akka.actor.UntypedActor;
//#printlnActor //#printlnActor
public class PrintlnActor extends UntypedActor { public class PrintlnActor extends UntypedActor {
public void onReceive(Object msg) { public void onReceive(Object msg) {
System.out.println(String.format("Received message '%s' in actor %s", msg, getSelf().path().name())); System.out.println(String.format("Received message '%s' in actor %s",
msg, getSelf().path().name()));
} }
} }

View file

@ -17,7 +17,8 @@ public class RouterViaConfigExample {
public void onReceive(Object msg) { public void onReceive(Object msg) {
if (msg instanceof Message) { if (msg instanceof Message) {
Message message = (Message) msg; Message message = (Message) msg;
System.out.println(String.format("Received %s in router %s", message.getNbr(), getSelf().path().name())); System.out.println(String.format("Received %s in router %s",
message.getNbr(), getSelf().path().name()));
} else { } else {
unhandled(msg); unhandled(msg);
} }
@ -42,14 +43,16 @@ public class RouterViaConfigExample {
+ " router = round-robin\n" + " nr-of-instances = 5\n" + " }\n" + "}\n"); + " router = round-robin\n" + " nr-of-instances = 5\n" + " }\n" + "}\n");
ActorSystem system = ActorSystem.create("Example", config); ActorSystem system = ActorSystem.create("Example", config);
//#configurableRouting //#configurableRouting
ActorRef router = system.actorOf(new Props(ExampleActor.class).withRouter(new FromConfig()), "router"); ActorRef router = system.actorOf(
new Props(ExampleActor.class).withRouter(new FromConfig()), "router");
//#configurableRouting //#configurableRouting
for (int i = 1; i <= 10; i++) { for (int i = 1; i <= 10; i++) {
router.tell(new ExampleActor.Message(i), null); router.tell(new ExampleActor.Message(i), null);
} }
//#configurableRoutingWithResizer //#configurableRoutingWithResizer
ActorRef router2 = system.actorOf(new Props(ExampleActor.class).withRouter(new FromConfig()), "router2"); ActorRef router2 = system.actorOf(
new Props(ExampleActor.class).withRouter(new FromConfig()), "router2");
//#configurableRoutingWithResizer //#configurableRoutingWithResizer
for (int i = 1; i <= 10; i++) { for (int i = 1; i <= 10; i++) {
router2.tell(new ExampleActor.Message(i), null); router2.tell(new ExampleActor.Message(i), null);

View file

@ -20,7 +20,8 @@ public class RouterViaProgramExample {
public void onReceive(Object msg) { public void onReceive(Object msg) {
if (msg instanceof Message) { if (msg instanceof Message) {
Message message = (Message) msg; Message message = (Message) msg;
System.out.println(String.format("Received %s in router %s", message.getNbr(), getSelf().path().name())); System.out.println(String.format("Received %s in router %s",
message.getNbr(), getSelf().path().name()));
} else { } else {
unhandled(msg); unhandled(msg);
} }
@ -44,7 +45,8 @@ public class RouterViaProgramExample {
ActorSystem system = ActorSystem.create("RPE"); ActorSystem system = ActorSystem.create("RPE");
//#programmaticRoutingNrOfInstances //#programmaticRoutingNrOfInstances
int nrOfInstances = 5; int nrOfInstances = 5;
ActorRef router1 = system.actorOf(new Props(ExampleActor.class).withRouter(new RoundRobinRouter(nrOfInstances))); ActorRef router1 = system.actorOf(
new Props(ExampleActor.class).withRouter(new RoundRobinRouter(nrOfInstances)));
//#programmaticRoutingNrOfInstances //#programmaticRoutingNrOfInstances
for (int i = 1; i <= 6; i++) { for (int i = 1; i <= 6; i++) {
router1.tell(new ExampleActor.Message(i), null); router1.tell(new ExampleActor.Message(i), null);
@ -54,8 +56,10 @@ public class RouterViaProgramExample {
ActorRef actor1 = system.actorOf(new Props(ExampleActor.class)); ActorRef actor1 = system.actorOf(new Props(ExampleActor.class));
ActorRef actor2 = system.actorOf(new Props(ExampleActor.class)); ActorRef actor2 = system.actorOf(new Props(ExampleActor.class));
ActorRef actor3 = system.actorOf(new Props(ExampleActor.class)); ActorRef actor3 = system.actorOf(new Props(ExampleActor.class));
Iterable<ActorRef> routees = Arrays.asList(new ActorRef[] { actor1, actor2, actor3 }); Iterable<ActorRef> routees = Arrays.asList(
ActorRef router2 = system.actorOf(new Props().withRouter(RoundRobinRouter.create(routees))); new ActorRef[] { actor1, actor2, actor3 });
ActorRef router2 = system.actorOf(
new Props().withRouter(RoundRobinRouter.create(routees)));
//#programmaticRoutingRoutees //#programmaticRoutingRoutees
for (int i = 1; i <= 6; i++) { for (int i = 1; i <= 6; i++) {
router2.tell(new ExampleActor.Message(i), null); router2.tell(new ExampleActor.Message(i), null);
@ -65,7 +69,8 @@ public class RouterViaProgramExample {
int lowerBound = 2; int lowerBound = 2;
int upperBound = 15; int upperBound = 15;
DefaultResizer resizer = new DefaultResizer(lowerBound, upperBound); DefaultResizer resizer = new DefaultResizer(lowerBound, upperBound);
ActorRef router3 = system.actorOf(new Props(ExampleActor.class).withRouter(new RoundRobinRouter(nrOfInstances))); ActorRef router3 = system.actorOf(
new Props(ExampleActor.class).withRouter(new RoundRobinRouter(nrOfInstances)));
//#programmaticRoutingWithResizer //#programmaticRoutingWithResizer
for (int i = 1; i <= 6; i++) { for (int i = 1; i <= 6; i++) {
router3.tell(new ExampleActor.Message(i), null); router3.tell(new ExampleActor.Message(i), null);

View file

@ -13,118 +13,124 @@ import akka.serialization.*;
//#imports //#imports
public class SerializationDocTestBase { public class SerializationDocTestBase {
static
//#my-own-serializer //#my-own-serializer
public static class MyOwnSerializer extends JSerializer { public class MyOwnSerializer extends JSerializer {
// This is whether "fromBinary" requires a "clazz" or not // This is whether "fromBinary" requires a "clazz" or not
@Override public boolean includeManifest() { @Override public boolean includeManifest() {
return false; return false;
}
// Pick a unique identifier for your Serializer,
// you've got a couple of billions to choose from,
// 0 - 16 is reserved by Akka itself
@Override public int identifier() {
return 1234567;
}
// "toBinary" serializes the given object to an Array of Bytes
@Override public byte[] toBinary(Object obj) {
// Put the code that serializes the object here
//#...
return new byte[0];
//#...
}
// "fromBinary" deserializes the given array,
// using the type hint (if any, see "includeManifest" above)
@Override public Object fromBinaryJava(byte[] bytes,
Class<?> clazz) {
// Put your code that deserializes here
//#...
return null;
//#...
}
} }
// Pick a unique identifier for your Serializer,
// you've got a couple of billions to choose from,
// 0 - 16 is reserved by Akka itself
@Override public int identifier() {
return 1234567;
}
// "toBinary" serializes the given object to an Array of Bytes
@Override public byte[] toBinary(Object obj) {
// Put the code that serializes the object here
//#...
return new byte[0];
//#...
}
// "fromBinary" deserializes the given array,
// using the type hint (if any, see "includeManifest" above)
@Override public Object fromBinaryJava(byte[] bytes,
Class<?> clazz) {
// Put your code that deserializes here
//#...
return null;
//#...
}
}
//#my-own-serializer //#my-own-serializer
@Test public void serializeActorRefs() { @Test public void serializeActorRefs() {
final ActorSystem theActorSystem = final ActorSystem theActorSystem =
ActorSystem.create("whatever"); ActorSystem.create("whatever");
final ActorRef theActorRef = final ActorRef theActorRef =
theActorSystem.deadLetters(); // Of course this should be you theActorSystem.deadLetters(); // Of course this should be you
//#actorref-serializer //#actorref-serializer
// Serialize // Serialize
// (beneath toBinary) // (beneath toBinary)
final Address transportAddress = final Address transportAddress =
Serialization.currentTransportAddress().value(); Serialization.currentTransportAddress().value();
String identifier; String identifier;
// If there is no transportAddress, // If there is no transportAddress,
// it means that either this Serializer isn't called // it means that either this Serializer isn't called
// within a piece of code that sets it, // within a piece of code that sets it,
// so either you need to supply your own, // so either you need to supply your own,
// or simply use the local path. // or simply use the local path.
if (transportAddress == null) identifier = theActorRef.path().toString(); if (transportAddress == null) identifier = theActorRef.path().toString();
else identifier = theActorRef.path().toStringWithAddress(transportAddress); else identifier = theActorRef.path().toStringWithAddress(transportAddress);
// Then just serialize the identifier however you like // Then just serialize the identifier however you like
// Deserialize // Deserialize
// (beneath fromBinary) // (beneath fromBinary)
final ActorRef deserializedActorRef = theActorSystem.actorFor(identifier); final ActorRef deserializedActorRef = theActorSystem.actorFor(identifier);
// Then just use the ActorRef // Then just use the ActorRef
//#actorref-serializer //#actorref-serializer
theActorSystem.shutdown(); theActorSystem.shutdown();
}
static
//#external-address
public class ExternalAddressExt implements Extension {
private final ExtendedActorSystem system;
public ExternalAddressExt(ExtendedActorSystem system) {
this.system = system;
} }
//#external-address public Address getAddressFor(Address remoteAddress) {
public static class ExternalAddressExt implements Extension { final scala.Option<Address> optAddr = system.provider()
private final ExtendedActorSystem system; .getExternalAddressFor(remoteAddress);
if (optAddr.isDefined()) {
public ExternalAddressExt(ExtendedActorSystem system) { return optAddr.get();
this.system = system; } else {
} throw new UnsupportedOperationException(
"cannot send to remote address " + remoteAddress);
public Address getAddressFor(Address remoteAddress) {
final scala.Option<Address> optAddr = system.provider()
.getExternalAddressFor(remoteAddress);
if (optAddr.isDefined()) {
return optAddr.get();
} else {
throw new UnsupportedOperationException(
"cannot send to remote address " + remoteAddress);
}
} }
} }
}
public static class ExternalAddress extends //#external-address
AbstractExtensionId<ExternalAddressExt> implements ExtensionIdProvider { static
public static final ExternalAddress ID = new ExternalAddress(); //#external-address
public class ExternalAddress extends
AbstractExtensionId<ExternalAddressExt> implements ExtensionIdProvider {
public static final ExternalAddress ID = new ExternalAddress();
public ExternalAddress lookup() { public ExternalAddress lookup() {
return ID; return ID;
}
public ExternalAddressExt createExtension(ExtendedActorSystem system) {
return new ExternalAddressExt(system);
}
} }
//#external-address public ExternalAddressExt createExtension(ExtendedActorSystem system) {
return new ExternalAddressExt(system);
public void demonstrateExternalAddress() {
// this is not meant to be run, only to be compiled
final ActorSystem system = ActorSystem.create();
final Address remoteAddr = new Address("", "");
// #external-address
final Address addr = ExternalAddress.ID.get(system).getAddressFor(remoteAddr);
// #external-address
} }
}
//#external-address
public void demonstrateExternalAddress() {
// this is not meant to be run, only to be compiled
final ActorSystem system = ActorSystem.create();
final Address remoteAddr = new Address("", "");
// #external-address
final Address addr = ExternalAddress.ID.get(system).getAddressFor(remoteAddr);
// #external-address
}
static
//#external-address-default //#external-address-default
public static class DefaultAddressExt implements Extension { public class DefaultAddressExt implements Extension {
private final ExtendedActorSystem system; private final ExtendedActorSystem system;
public DefaultAddressExt(ExtendedActorSystem system) { public DefaultAddressExt(ExtendedActorSystem system) {
@ -141,7 +147,10 @@ public class SerializationDocTestBase {
} }
} }
public static class DefaultAddress extends //#external-address-default
static
//#external-address-default
public class DefaultAddress extends
AbstractExtensionId<DefaultAddressExt> implements ExtensionIdProvider { AbstractExtensionId<DefaultAddressExt> implements ExtensionIdProvider {
public static final DefaultAddress ID = new DefaultAddress(); public static final DefaultAddress ID = new DefaultAddress();

View file

@ -20,7 +20,8 @@ public class CoordinatedCounter extends UntypedActor {
if (message instanceof Increment) { if (message instanceof Increment) {
Increment increment = (Increment) message; Increment increment = (Increment) message;
if (increment.hasFriend()) { if (increment.hasFriend()) {
increment.getFriend().tell(coordinated.coordinate(new Increment()), getSelf()); increment.getFriend().tell(
coordinated.coordinate(new Increment()), getSelf());
} }
coordinated.atomic(new Runnable() { coordinated.atomic(new Runnable() {
public void run() { public void run() {

View file

@ -30,7 +30,8 @@ public class TransactorDocTest {
counter1.tell(new Coordinated(new Increment(counter2), timeout), null); counter1.tell(new Coordinated(new Increment(counter2), timeout), null);
Integer count = (Integer) Await.result(ask(counter1, "GetCount", timeout), timeout.duration()); Integer count = (Integer) Await.result(
ask(counter1, "GetCount", timeout), timeout.duration());
//#coordinated-example //#coordinated-example
assertEquals(count, new Integer(1)); assertEquals(count, new Integer(1));

View file

@ -3,32 +3,28 @@
*/ */
package docs.zeromq; package docs.zeromq;
//#pub-socket //#import-pub-socket
import akka.zeromq.Bind; import akka.zeromq.Bind;
import akka.zeromq.ZeroMQExtension; import akka.zeromq.ZeroMQExtension;
//#import-pub-socket
//#pub-socket //#import-sub-socket
//#sub-socket
import akka.zeromq.Connect; import akka.zeromq.Connect;
import akka.zeromq.Listener; import akka.zeromq.Listener;
import akka.zeromq.Subscribe; import akka.zeromq.Subscribe;
//#import-sub-socket
//#sub-socket //#import-unsub-topic-socket
//#unsub-topic-socket
import akka.zeromq.Unsubscribe; import akka.zeromq.Unsubscribe;
//#import-unsub-topic-socket
//#unsub-topic-socket //#import-pub-topic
//#pub-topic
import akka.zeromq.Frame; import akka.zeromq.Frame;
import akka.zeromq.ZMQMessage; import akka.zeromq.ZMQMessage;
//#import-pub-topic
//#pub-topic
import akka.zeromq.HighWatermark; import akka.zeromq.HighWatermark;
import akka.zeromq.SocketOption; import akka.zeromq.SocketOption;
import akka.zeromq.ZeroMQVersion; import akka.zeromq.ZeroMQVersion;
//#health //#import-health
import akka.actor.ActorRef; import akka.actor.ActorRef;
import akka.actor.UntypedActor; import akka.actor.UntypedActor;
import akka.actor.Props; import akka.actor.Props;
@ -39,7 +35,7 @@ import akka.serialization.SerializationExtension;
import akka.serialization.Serialization; import akka.serialization.Serialization;
import java.io.Serializable; import java.io.Serializable;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
//#health //#import-health
import com.typesafe.config.ConfigFactory; import com.typesafe.config.ConfigFactory;
@ -77,18 +73,21 @@ public class ZeromqDocTestBase {
Assume.assumeTrue(checkZeroMQInstallation()); Assume.assumeTrue(checkZeroMQInstallation());
//#pub-socket //#pub-socket
ActorRef pubSocket = ZeroMQExtension.get(system).newPubSocket(new Bind("tcp://127.0.0.1:1233")); ActorRef pubSocket = ZeroMQExtension.get(system).newPubSocket(
new Bind("tcp://127.0.0.1:1233"));
//#pub-socket //#pub-socket
//#sub-socket //#sub-socket
ActorRef listener = system.actorOf(new Props(ListenerActor.class)); ActorRef listener = system.actorOf(new Props(ListenerActor.class));
ActorRef subSocket = ZeroMQExtension.get(system).newSubSocket(new Connect("tcp://127.0.0.1:1233"), ActorRef subSocket = ZeroMQExtension.get(system).newSubSocket(
new Listener(listener), Subscribe.all()); new Connect("tcp://127.0.0.1:1233"),
new Listener(listener), Subscribe.all());
//#sub-socket //#sub-socket
//#sub-topic-socket //#sub-topic-socket
ActorRef subTopicSocket = ZeroMQExtension.get(system).newSubSocket(new Connect("tcp://127.0.0.1:1233"), ActorRef subTopicSocket = ZeroMQExtension.get(system).newSubSocket(
new Listener(listener), new Subscribe("foo.bar")); new Connect("tcp://127.0.0.1:1233"),
new Listener(listener), new Subscribe("foo.bar"));
//#sub-topic-socket //#sub-topic-socket
//#unsub-topic-socket //#unsub-topic-socket
@ -102,7 +101,8 @@ public class ZeromqDocTestBase {
//#high-watermark //#high-watermark
ActorRef highWatermarkSocket = ZeroMQExtension.get(system).newRouterSocket( ActorRef highWatermarkSocket = ZeroMQExtension.get(system).newRouterSocket(
new SocketOption[] { new Listener(listener), new Bind("tcp://127.0.0.1:1233"), new HighWatermark(50000) }); new SocketOption[] { new Listener(listener),
new Bind("tcp://127.0.0.1:1233"), new HighWatermark(50000) });
//#high-watermark //#high-watermark
} }
@ -139,20 +139,23 @@ public class ZeromqDocTestBase {
} }
} }
static
//#listener-actor //#listener-actor
public static class ListenerActor extends UntypedActor { public class ListenerActor extends UntypedActor {
public void onReceive(Object message) throws Exception { public void onReceive(Object message) throws Exception {
//... //...
} }
} }
//#listener-actor //#listener-actor
static
//#health //#health
public final Object TICK = "TICK";
public static final Object TICK = "TICK"; //#health
static
public static class Heap implements Serializable { //#health
public class Heap implements Serializable {
public final long timestamp; public final long timestamp;
public final long used; public final long used;
public final long max; public final long max;
@ -164,7 +167,10 @@ public class ZeromqDocTestBase {
} }
} }
public static class Load implements Serializable { //#health
static
//#health
public class Load implements Serializable {
public final long timestamp; public final long timestamp;
public final double loadAverage; public final double loadAverage;
@ -174,9 +180,13 @@ public class ZeromqDocTestBase {
} }
} }
public static class HealthProbe extends UntypedActor { //#health
static
//#health
public class HealthProbe extends UntypedActor {
ActorRef pubSocket = ZeroMQExtension.get(getContext().system()).newPubSocket(new Bind("tcp://127.0.0.1:1237")); ActorRef pubSocket = ZeroMQExtension.get(getContext().system()).newPubSocket(
new Bind("tcp://127.0.0.1:1237"));
MemoryMXBean memory = ManagementFactory.getMemoryMXBean(); MemoryMXBean memory = ManagementFactory.getMemoryMXBean();
OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean(); OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
Serialization ser = SerializationExtension.get(getContext().system()); Serialization ser = SerializationExtension.get(getContext().system());
@ -184,7 +194,8 @@ public class ZeromqDocTestBase {
@Override @Override
public void preStart() { public void preStart() {
getContext().system().scheduler() getContext().system().scheduler()
.schedule(Duration.create(1, "second"), Duration.create(1, "second"), getSelf(), TICK, getContext().dispatcher()); .schedule(Duration.create(1, "second"), Duration.create(1, "second"),
getSelf(), TICK, getContext().dispatcher());
} }
@Override @Override
@ -202,25 +213,29 @@ public class ZeromqDocTestBase {
byte[] heapPayload = ser.serializerFor(Heap.class).toBinary( byte[] heapPayload = ser.serializerFor(Heap.class).toBinary(
new Heap(timestamp, currentHeap.getUsed(), currentHeap.getMax())); new Heap(timestamp, currentHeap.getUsed(), currentHeap.getMax()));
// the first frame is the topic, second is the message // the first frame is the topic, second is the message
pubSocket.tell(new ZMQMessage(new Frame("health.heap"), new Frame(heapPayload)), getSelf()); pubSocket.tell(new ZMQMessage(new Frame("health.heap"),
new Frame(heapPayload)), getSelf());
// use akka SerializationExtension to convert to bytes // use akka SerializationExtension to convert to bytes
byte[] loadPayload = ser.serializerFor(Load.class).toBinary(new Load(timestamp, os.getSystemLoadAverage())); byte[] loadPayload = ser.serializerFor(Load.class).toBinary(
new Load(timestamp, os.getSystemLoadAverage()));
// the first frame is the topic, second is the message // the first frame is the topic, second is the message
pubSocket.tell(new ZMQMessage(new Frame("health.load"), new Frame(loadPayload)), getSelf()); pubSocket.tell(new ZMQMessage(new Frame("health.load"),
new Frame(loadPayload)), getSelf());
} else { } else {
unhandled(message); unhandled(message);
} }
} }
} }
//#health //#health
static
//#logger //#logger
public static class Logger extends UntypedActor { public class Logger extends UntypedActor {
ActorRef subSocket = ZeroMQExtension.get(getContext().system()).newSubSocket(new Connect("tcp://127.0.0.1:1237"), ActorRef subSocket = ZeroMQExtension.get(getContext().system()).newSubSocket(
new Connect("tcp://127.0.0.1:1237"),
new Listener(getSelf()), new Subscribe("health")); new Listener(getSelf()), new Subscribe("health"));
Serialization ser = SerializationExtension.get(getContext().system()); Serialization ser = SerializationExtension.get(getContext().system());
SimpleDateFormat timestampFormat = new SimpleDateFormat("HH:mm:ss.SSS"); SimpleDateFormat timestampFormat = new SimpleDateFormat("HH:mm:ss.SSS");
@ -233,10 +248,12 @@ public class ZeromqDocTestBase {
// the first frame is the topic, second is the message // the first frame is the topic, second is the message
if (m.firstFrameAsString().equals("health.heap")) { if (m.firstFrameAsString().equals("health.heap")) {
Heap heap = (Heap) ser.serializerFor(Heap.class).fromBinary(m.payload(1)); Heap heap = (Heap) ser.serializerFor(Heap.class).fromBinary(m.payload(1));
log.info("Used heap {} bytes, at {}", heap.used, timestampFormat.format(new Date(heap.timestamp))); log.info("Used heap {} bytes, at {}", heap.used,
timestampFormat.format(new Date(heap.timestamp)));
} else if (m.firstFrameAsString().equals("health.load")) { } else if (m.firstFrameAsString().equals("health.load")) {
Load load = (Load) ser.serializerFor(Load.class).fromBinary(m.payload(1)); Load load = (Load) ser.serializerFor(Load.class).fromBinary(m.payload(1));
log.info("Load average {}, at {}", load.loadAverage, timestampFormat.format(new Date(load.timestamp))); log.info("Load average {}, at {}", load.loadAverage,
timestampFormat.format(new Date(load.timestamp)));
} }
} else { } else {
unhandled(message); unhandled(message);
@ -247,11 +264,13 @@ public class ZeromqDocTestBase {
//#logger //#logger
static
//#alerter //#alerter
public static class HeapAlerter extends UntypedActor { public class HeapAlerter extends UntypedActor {
ActorRef subSocket = ZeroMQExtension.get(getContext().system()).newSubSocket(new Connect("tcp://127.0.0.1:1237"), ActorRef subSocket = ZeroMQExtension.get(getContext().system()).newSubSocket(
new Listener(getSelf()), new Subscribe("health.heap")); new Connect("tcp://127.0.0.1:1237"),
new Listener(getSelf()), new Subscribe("health.heap"));
Serialization ser = SerializationExtension.get(getContext().system()); Serialization ser = SerializationExtension.get(getContext().system());
LoggingAdapter log = Logging.getLogger(getContext().system(), this); LoggingAdapter log = Logging.getLogger(getContext().system(), this);
int count = 0; int count = 0;
@ -269,7 +288,8 @@ public class ZeromqDocTestBase {
count = 0; count = 0;
} }
if (count > 10) { if (count > 10) {
log.warning("Need more memory, using {} %", (100.0 * heap.used / heap.max)); log.warning("Need more memory, using {} %",
(100.0 * heap.used / heap.max));
} }
} }
} else { } else {

View file

@ -26,12 +26,18 @@ So let's create a sample extension that just lets us count the number of times s
First, we define what our ``Extension`` should do: First, we define what our ``Extension`` should do:
.. includecode:: code/docs/extension/ExtensionDocTestBase.java .. includecode:: code/docs/extension/ExtensionDocTestBase.java
:include: imports,extension :include: imports
.. includecode:: code/docs/extension/ExtensionDocTestBase.java
:include: extension
Then we need to create an ``ExtensionId`` for our extension so we can grab ahold of it. Then we need to create an ``ExtensionId`` for our extension so we can grab ahold of it.
.. includecode:: code/docs/extension/ExtensionDocTestBase.java .. includecode:: code/docs/extension/ExtensionDocTestBase.java
:include: imports,extensionid :include: imports
.. includecode:: code/docs/extension/ExtensionDocTestBase.java
:include: extensionid
Wicked! Now all we need to do is to actually use it: Wicked! Now all we need to do is to actually use it:
@ -78,8 +84,10 @@ Sample configuration:
The ``Extension``: The ``Extension``:
.. includecode:: code/docs/extension/SettingsExtensionDocTestBase.java .. includecode:: code/docs/extension/SettingsExtensionDocTestBase.java
:include: imports,extension,extensionid :include: imports
.. includecode:: code/docs/extension/SettingsExtensionDocTestBase.java
:include: extension,extensionid
Use it: Use it:

View file

@ -20,7 +20,10 @@ it will use its default dispatcher as the ``ExecutionContext``, or you can use t
by the ``ExecutionContexts`` class to wrap ``Executors`` and ``ExecutorServices``, or even create your own. by the ``ExecutionContexts`` class to wrap ``Executors`` and ``ExecutorServices``, or even create your own.
.. includecode:: code/docs/future/FutureDocTestBase.java .. includecode:: code/docs/future/FutureDocTestBase.java
:include: imports1,imports7,diy-execution-context :include: imports1,imports7
.. includecode:: code/docs/future/FutureDocTestBase.java
:include: diy-execution-context
Use with Actors Use with Actors
--------------- ---------------
@ -32,7 +35,10 @@ Using the ``ActorRef``\'s ``ask`` method to send a message will return a ``Futur
To wait for and retrieve the actual result the simplest method is: To wait for and retrieve the actual result the simplest method is:
.. includecode:: code/docs/future/FutureDocTestBase.java .. includecode:: code/docs/future/FutureDocTestBase.java
:include: imports1,ask-blocking :include: imports1
.. includecode:: code/docs/future/FutureDocTestBase.java
:include: ask-blocking
This will cause the current thread to block and wait for the ``UntypedActor`` to 'complete' the ``Future`` with it's reply. This will cause the current thread to block and wait for the ``UntypedActor`` to 'complete' the ``Future`` with it's reply.
Blocking is discouraged though as it can cause performance problem. Blocking is discouraged though as it can cause performance problem.
@ -49,7 +55,10 @@ the extra utility of an ``UntypedActor``. If you find yourself creating a pool o
of performing a calculation in parallel, there is an easier (and faster) way: of performing a calculation in parallel, there is an easier (and faster) way:
.. includecode:: code/docs/future/FutureDocTestBase.java .. includecode:: code/docs/future/FutureDocTestBase.java
:include: imports2,future-eval :include: imports2
.. includecode:: code/docs/future/FutureDocTestBase.java
:include: future-eval
In the above code the block passed to ``future`` will be executed by the default ``Dispatcher``, In the above code the block passed to ``future`` will be executed by the default ``Dispatcher``,
with the return value of the block used to complete the ``Future`` (in this case, the result would be the string: "HelloWorld"). with the return value of the block used to complete the ``Future`` (in this case, the result would be the string: "HelloWorld").
@ -80,7 +89,10 @@ some operation on the result of the ``Future``, and returning a new result.
The return value of the ``map`` method is another ``Future`` that will contain the new result: The return value of the ``map`` method is another ``Future`` that will contain the new result:
.. includecode:: code/docs/future/FutureDocTestBase.java .. includecode:: code/docs/future/FutureDocTestBase.java
:include: imports2,map :include: imports2
.. includecode:: code/docs/future/FutureDocTestBase.java
:include: map
In this example we are joining two strings together within a ``Future``. Instead of waiting for f1 to complete, In this example we are joining two strings together within a ``Future``. Instead of waiting for f1 to complete,
we apply our function that calculates the length of the string using the ``map`` method. we apply our function that calculates the length of the string using the ``map`` method.
@ -131,7 +143,10 @@ It is very often desirable to be able to combine different Futures with each oth
below are some examples on how that can be done in a non-blocking fashion. below are some examples on how that can be done in a non-blocking fashion.
.. includecode:: code/docs/future/FutureDocTestBase.java .. includecode:: code/docs/future/FutureDocTestBase.java
:include: imports3,sequence :include: imports3
.. includecode:: code/docs/future/FutureDocTestBase.java
:include: sequence
To better explain what happened in the example, ``Future.sequence`` is taking the ``Iterable<Future<Integer>>`` To better explain what happened in the example, ``Future.sequence`` is taking the ``Iterable<Future<Integer>>``
and turning it into a ``Future<Iterable<Integer>>``. We can then use ``map`` to work with the ``Iterable<Integer>`` directly, and turning it into a ``Future<Iterable<Integer>>``. We can then use ``map`` to work with the ``Iterable<Integer>`` directly,
@ -141,7 +156,10 @@ The ``traverse`` method is similar to ``sequence``, but it takes a sequence of `
and returns a ``Future<Iterable<B>>``, enabling parallel ``map`` over the sequence, if you use ``Futures.future`` to create the ``Future``. and returns a ``Future<Iterable<B>>``, enabling parallel ``map`` over the sequence, if you use ``Futures.future`` to create the ``Future``.
.. includecode:: code/docs/future/FutureDocTestBase.java .. includecode:: code/docs/future/FutureDocTestBase.java
:include: imports4,traverse :include: imports4
.. includecode:: code/docs/future/FutureDocTestBase.java
:include: traverse
It's as simple as that! It's as simple as that!
@ -152,7 +170,10 @@ and then applies the function to all elements in the sequence of futures, non-bl
the execution will be started when the last of the Futures is completed. the execution will be started when the last of the Futures is completed.
.. includecode:: code/docs/future/FutureDocTestBase.java .. includecode:: code/docs/future/FutureDocTestBase.java
:include: imports5,fold :include: imports5
.. includecode:: code/docs/future/FutureDocTestBase.java
:include: fold
That's all it takes! That's all it takes!
@ -162,7 +183,10 @@ In some cases you don't have a start-value and you're able to use the value of t
in the sequence as the start-value, you can use ``reduce``, it works like this: in the sequence as the start-value, you can use ``reduce``, it works like this:
.. includecode:: code/docs/future/FutureDocTestBase.java .. includecode:: code/docs/future/FutureDocTestBase.java
:include: imports6,reduce :include: imports6
.. includecode:: code/docs/future/FutureDocTestBase.java
:include: reduce
Same as with ``fold``, the execution will be started when the last of the Futures is completed, you can also parallelize Same as with ``fold``, the execution will be started when the last of the Futures is completed, you can also parallelize
it by chunking your futures into sub-sequences and reduce them, and then reduce the reduced results again. it by chunking your futures into sub-sequences and reduce them, and then reduce the reduced results again.
@ -242,4 +266,7 @@ After
``akka.pattern.Patterns.after`` makes it easy to complete a ``Future`` with a value or exception after a timeout. ``akka.pattern.Patterns.after`` makes it easy to complete a ``Future`` with a value or exception after a timeout.
.. includecode:: code/docs/future/FutureDocTestBase.java .. includecode:: code/docs/future/FutureDocTestBase.java
:include: imports8,after :include: imports8
.. includecode:: code/docs/future/FutureDocTestBase.java
:include: after

View file

@ -12,7 +12,10 @@ Create a ``LoggingAdapter`` and use the ``error``, ``warning``, ``info``, or ``d
as illustrated in this example: as illustrated in this example:
.. includecode:: code/docs/event/LoggingDocTestBase.java .. includecode:: code/docs/event/LoggingDocTestBase.java
:include: imports,my-actor :include: imports
.. includecode:: code/docs/event/LoggingDocTestBase.java
:include: my-actor
The first parameter to ``Logging.getLogger`` could also be any The first parameter to ``Logging.getLogger`` could also be any
:class:`LoggingBus`, specifically ``system.eventStream()``; in the demonstrated :class:`LoggingBus`, specifically ``system.eventStream()``; in the demonstrated
@ -74,7 +77,7 @@ by Actors:
akka { akka {
actor { actor {
debug { debug {
# enable DEBUG logging of all AutoReceiveMessages (Kill, PoisonPill and the like) # enable DEBUG logging of all AutoReceiveMessages (Kill, PoisonPill et.c.)
autoreceive = on autoreceive = on
} }
} }
@ -129,7 +132,8 @@ If you want to see all messages that are sent through remoting at DEBUG log leve
akka { akka {
remote { remote {
# If this is "on", Akka will log all outbound messages at DEBUG level, if off then they are not logged # If this is "on", Akka will log all outbound messages at DEBUG level,
# if off then they are not logged
log-sent-messages = on log-sent-messages = on
} }
} }
@ -141,7 +145,8 @@ If you want to see all messages that are received through remoting at DEBUG log
akka { akka {
remote { remote {
# If this is "on", Akka will log all inbound messages at DEBUG level, if off then they are not logged # If this is "on", Akka will log all inbound messages at DEBUG level,
# if off then they are not logged
log-received-messages = on log-received-messages = on
} }
} }
@ -171,8 +176,10 @@ event handler available in the 'akka-slf4j' module.
Example of creating a listener: Example of creating a listener:
.. includecode:: code/docs/event/LoggingDocTestBase.java .. includecode:: code/docs/event/LoggingDocTestBase.java
:include: imports,imports-listener,my-event-listener :include: imports,imports-listener
.. includecode:: code/docs/event/LoggingDocTestBase.java
:include: my-event-listener
.. _slf4j-java: .. _slf4j-java:
@ -218,7 +225,7 @@ the first case and ``LoggerFactory.getLogger(String s)`` in the second).
.. code-block:: scala .. code-block:: scala
final LoggingAdapter log = Logging.getLogger(system.eventStream(), "my.nice.string"); final LoggingAdapter log = Logging.getLogger(system.eventStream(), "my.string");
Logging Thread and Akka Source in MDC Logging Thread and Akka Source in MDC
------------------------------------- -------------------------------------

View file

@ -65,11 +65,11 @@ Looking up Remote Actors
``actorFor(path)`` will obtain an ``ActorRef`` to an Actor on a remote node:: ``actorFor(path)`` will obtain an ``ActorRef`` to an Actor on a remote node::
ActorRef actor = context.actorFor("akka://app@10.0.0.1:2552/user/serviceA/retrieval"); ActorRef actor = context.actorFor("akka://app@10.0.0.1:2552/user/serviceA/worker");
As you can see from the example above the following pattern is used to find an ``ActorRef`` on a remote node:: As you can see from the example above the following pattern is used to find an ``ActorRef`` on a remote node::
akka://<actorsystemname>@<hostname>:<port>/<actor path> akka://<actorsystemname>@<hostname>:<port>/<actor path>
Once you obtained a reference to the actor you can interact with it they same way you would with a local actor, e.g.:: Once you obtained a reference to the actor you can interact with it they same way you would with a local actor, e.g.::

View file

@ -304,10 +304,16 @@ the same time for one router. The ``withHashMapper`` is tried first.
Code example: Code example:
.. includecode:: code/docs/jrouting/ConsistentHashingRouterDocTestBase.java .. includecode:: code/docs/jrouting/ConsistentHashingRouterDocTestBase.java
:include: imports1,cache-actor :include: imports1
.. includecode:: code/docs/jrouting/ConsistentHashingRouterDocTestBase.java .. includecode:: code/docs/jrouting/ConsistentHashingRouterDocTestBase.java
:include: imports2,consistent-hashing-router :include: cache-actor
.. includecode:: code/docs/jrouting/ConsistentHashingRouterDocTestBase.java
:include: imports2
.. includecode:: code/docs/jrouting/ConsistentHashingRouterDocTestBase.java
:include: consistent-hashing-router
In the above example you see that the ``Get`` message implements ``ConsistentHashable`` itself, In the above example you see that the ``Get`` message implements ``ConsistentHashable`` itself,
while the ``Entry`` message is wrapped in a ``ConsistentHashableEnvelope``. The ``Evict`` while the ``Entry`` message is wrapped in a ``ConsistentHashableEnvelope``. The ``Evict``

View file

@ -25,14 +25,26 @@ scheduled operation.
Some examples Some examples
------------- -------------
Schedule to send the "foo"-message to the testActor after 50ms:
.. includecode:: code/docs/actor/SchedulerDocTestBase.java .. includecode:: code/docs/actor/SchedulerDocTestBase.java
:include: imports1,schedule-one-off-message :include: imports1
.. includecode:: code/docs/actor/SchedulerDocTestBase.java
:include: schedule-one-off-message
Schedule a Runnable, that sends the current time to the testActor, to be executed after 50ms:
.. includecode:: code/docs/actor/SchedulerDocTestBase.java .. includecode:: code/docs/actor/SchedulerDocTestBase.java
:include: schedule-one-off-thunk :include: schedule-one-off-thunk
Schedule to send the "Tick"-message to the ``tickActor`` after 0ms repeating every 50ms:
.. includecode:: code/docs/actor/SchedulerDocTestBase.java .. includecode:: code/docs/actor/SchedulerDocTestBase.java
:include: imports1,imports2,schedule-recurring :include: imports1,imports2
.. includecode:: code/docs/actor/SchedulerDocTestBase.java
:include: schedule-recurring
From ``akka.actor.ActorSystem`` From ``akka.actor.ActorSystem``
------------------------------- -------------------------------

View file

@ -76,7 +76,10 @@ If you want to programmatically serialize/deserialize using Akka Serialization,
here's some examples: here's some examples:
.. includecode:: code/docs/serialization/SerializationDocTestBase.java .. includecode:: code/docs/serialization/SerializationDocTestBase.java
:include: imports,programmatic :include: imports
.. includecode:: code/docs/serialization/SerializationDocTestBase.java
:include: programmatic
For more information, have a look at the ``ScalaDoc`` for ``akka.serialization._`` For more information, have a look at the ``ScalaDoc`` for ``akka.serialization._``
@ -94,7 +97,10 @@ First you need to create a class definition of your ``Serializer``,
which is done by extending ``akka.serialization.JSerializer``, like this: which is done by extending ``akka.serialization.JSerializer``, like this:
.. includecode:: code/docs/serialization/SerializationDocTestBase.java .. includecode:: code/docs/serialization/SerializationDocTestBase.java
:include: imports,my-own-serializer :include: imports
.. includecode:: code/docs/serialization/SerializationDocTestBase.java
:include: my-own-serializer
:exclude: ... :exclude: ...
Then you only need to fill in the blanks, bind it to a name in your :ref:`configuration` and then Then you only need to fill in the blanks, bind it to a name in your :ref:`configuration` and then
@ -107,7 +113,10 @@ All ActorRefs are serializable using JavaSerializer, but in case you are writing
you might want to know how to serialize and deserialize them properly, here's the magic incantation: you might want to know how to serialize and deserialize them properly, here's the magic incantation:
.. includecode:: code/docs/serialization/SerializationDocTestBase.java .. includecode:: code/docs/serialization/SerializationDocTestBase.java
:include: imports,actorref-serializer :include: imports
.. includecode:: code/docs/serialization/SerializationDocTestBase.java
:include: actorref-serializer
.. note:: .. note::

View file

@ -53,16 +53,21 @@ Creating Typed Actors
To create a Typed Actor you need to have one or more interfaces, and one implementation. To create a Typed Actor you need to have one or more interfaces, and one implementation.
The following imports are assumed:
.. includecode:: code/docs/actor/TypedActorDocTestBase.java
:include: imports
Our example interface: Our example interface:
.. includecode:: code/docs/actor/TypedActorDocTestBase.java .. includecode:: code/docs/actor/TypedActorDocTestBase.java
:include: imports,typed-actor-iface :include: typed-actor-iface
:exclude: typed-actor-iface-methods :exclude: typed-actor-iface-methods
Our example implementation of that interface: Our example implementation of that interface:
.. includecode:: code/docs/actor/TypedActorDocTestBase.java .. includecode:: code/docs/actor/TypedActorDocTestBase.java
:include: imports,typed-actor-impl :include: typed-actor-impl
:exclude: typed-actor-impl-methods :exclude: typed-actor-impl-methods
The most trivial way of creating a Typed Actor instance The most trivial way of creating a Typed Actor instance
@ -81,12 +86,12 @@ Since you supply a ``Props``, you can specify which dispatcher to use, what the
Now, our ``Squarer`` doesn't have any methods, so we'd better add those. Now, our ``Squarer`` doesn't have any methods, so we'd better add those.
.. includecode:: code/docs/actor/TypedActorDocTestBase.java .. includecode:: code/docs/actor/TypedActorDocTestBase.java
:include: imports,typed-actor-iface :include: typed-actor-iface
Alright, now we've got some methods we can call, but we need to implement those in ``SquarerImpl``. Alright, now we've got some methods we can call, but we need to implement those in ``SquarerImpl``.
.. includecode:: code/docs/actor/TypedActorDocTestBase.java .. includecode:: code/docs/actor/TypedActorDocTestBase.java
:include: imports,typed-actor-impl :include: typed-actor-impl
Excellent, now we have an interface and an implementation of that interface, Excellent, now we have an interface and an implementation of that interface,
and we know how to create a Typed Actor from that, so let's look at calling these methods. and we know how to create a Typed Actor from that, so let's look at calling these methods.

View file

@ -61,7 +61,10 @@ Creating Actors with default constructor
---------------------------------------- ----------------------------------------
.. includecode:: code/docs/actor/UntypedActorDocTestBase.java .. includecode:: code/docs/actor/UntypedActorDocTestBase.java
:include: imports,system-actorOf :include: imports
.. includecode:: code/docs/actor/UntypedActorDocTestBase.java
:include: system-actorOf
The call to :meth:`actorOf` returns an instance of ``ActorRef``. This is a handle to The call to :meth:`actorOf` returns an instance of ``ActorRef``. This is a handle to
the ``UntypedActor`` instance which you can use to interact with the ``UntypedActor``. The the ``UntypedActor`` instance which you can use to interact with the ``UntypedActor``. The
@ -248,8 +251,8 @@ actors may look up other actors by specifying absolute or relative
paths—logical or physical—and receive back an :class:`ActorRef` with the paths—logical or physical—and receive back an :class:`ActorRef` with the
result:: result::
getContext().actorFor("/user/serviceA/aggregator") // will look up this absolute path getContext().actorFor("/user/serviceA/actor") // will look up this absolute path
getContext().actorFor("../joe") // will look up sibling beneath same supervisor getContext().actorFor("../joe") // will look up sibling beneath same supervisor
The supplied path is parsed as a :class:`java.net.URI`, which basically means The supplied path is parsed as a :class:`java.net.URI`, which basically means
that it is split on ``/`` into path elements. If the path starts with ``/``, it that it is split on ``/`` into path elements. If the path starts with ``/``, it
@ -499,7 +502,10 @@ in the mailbox.
Use it like this: Use it like this:
.. includecode:: code/docs/actor/UntypedActorDocTestBase.java .. includecode:: code/docs/actor/UntypedActorDocTestBase.java
:include: import-actors,poison-pill :include: import-actors
.. includecode:: code/docs/actor/UntypedActorDocTestBase.java
:include: poison-pill
Graceful Stop Graceful Stop
------------- -------------
@ -508,7 +514,10 @@ Graceful Stop
termination of several actors: termination of several actors:
.. includecode:: code/docs/actor/UntypedActorDocTestBase.java .. includecode:: code/docs/actor/UntypedActorDocTestBase.java
:include: import-gracefulStop,gracefulStop :include: import-gracefulStop
.. includecode:: code/docs/actor/UntypedActorDocTestBase.java
:include: gracefulStop
When ``gracefulStop()`` returns successfully, the actors ``postStop()`` hook When ``gracefulStop()`` returns successfully, the actors ``postStop()`` hook
will have been executed: there exists a happens-before edge between the end of will have been executed: there exists a happens-before edge between the end of
@ -542,7 +551,10 @@ The hotswapped code is kept in a Stack which can be pushed and popped.
To hotswap the Actor using ``getContext().become``: To hotswap the Actor using ``getContext().become``:
.. includecode:: code/docs/actor/UntypedActorDocTestBase.java .. includecode:: code/docs/actor/UntypedActorDocTestBase.java
:include: import-procedure,hot-swap-actor :include: import-procedure
.. includecode:: code/docs/actor/UntypedActorDocTestBase.java
:include: hot-swap-actor
The ``become`` method is useful for many different things, such as to implement The ``become`` method is useful for many different things, such as to implement
a Finite State Machine (FSM). a Finite State Machine (FSM).
@ -622,7 +634,10 @@ through regular supervisor semantics.
Use it like this: Use it like this:
.. includecode:: code/docs/actor/UntypedActorDocTestBase.java .. includecode:: code/docs/actor/UntypedActorDocTestBase.java
:include: import-actors,kill :include: import-actors
.. includecode:: code/docs/actor/UntypedActorDocTestBase.java
:include: kill
Actors and exceptions Actors and exceptions
===================== =====================

View file

@ -19,12 +19,16 @@ Connection
ZeroMQ supports multiple connectivity patterns, each aimed to meet a different set of requirements. Currently, this module supports publisher-subscriber connections and connections based on dealers and routers. For connecting or accepting connections, a socket must be created. ZeroMQ supports multiple connectivity patterns, each aimed to meet a different set of requirements. Currently, this module supports publisher-subscriber connections and connections based on dealers and routers. For connecting or accepting connections, a socket must be created.
Sockets are always created using the ``akka.zeromq.ZeroMQExtension``, for example: Sockets are always created using the ``akka.zeromq.ZeroMQExtension``, for example:
.. includecode:: code/docs/zeromq/ZeromqDocTestBase.java#import-pub-socket
.. includecode:: code/docs/zeromq/ZeromqDocTestBase.java#pub-socket .. includecode:: code/docs/zeromq/ZeromqDocTestBase.java#pub-socket
Above examples will create a ZeroMQ Publisher socket that is Bound to the port 21231 on localhost. Above examples will create a ZeroMQ Publisher socket that is Bound to the port 21231 on localhost.
Similarly you can create a subscription socket, with a listener, that subscribes to all messages from the publisher using: Similarly you can create a subscription socket, with a listener, that subscribes to all messages from the publisher using:
.. includecode:: code/docs/zeromq/ZeromqDocTestBase.java#import-sub-socket
.. includecode:: code/docs/zeromq/ZeromqDocTestBase.java#sub-socket .. includecode:: code/docs/zeromq/ZeromqDocTestBase.java#sub-socket
.. includecode:: code/docs/zeromq/ZeromqDocTestBase.java#listener-actor .. includecode:: code/docs/zeromq/ZeromqDocTestBase.java#listener-actor
@ -50,10 +54,14 @@ It is a prefix match so it is subscribed to all topics starting with ``foo.bar``
To unsubscribe from a topic you do the following: To unsubscribe from a topic you do the following:
.. includecode:: code/docs/zeromq/ZeromqDocTestBase.java#import-unsub-topic-socket
.. includecode:: code/docs/zeromq/ZeromqDocTestBase.java#unsub-topic-socket .. includecode:: code/docs/zeromq/ZeromqDocTestBase.java#unsub-topic-socket
To publish messages to a topic you must use two Frames with the topic in the first frame. To publish messages to a topic you must use two Frames with the topic in the first frame.
.. includecode:: code/docs/zeromq/ZeromqDocTestBase.java#import-pub-topic
.. includecode:: code/docs/zeromq/ZeromqDocTestBase.java#pub-topic .. includecode:: code/docs/zeromq/ZeromqDocTestBase.java#pub-topic
Pub-Sub in Action Pub-Sub in Action
@ -64,6 +72,8 @@ The following example illustrates one publisher with two subscribers.
The publisher monitors current heap usage and system load and periodically publishes ``Heap`` events on the ``"health.heap"`` topic The publisher monitors current heap usage and system load and periodically publishes ``Heap`` events on the ``"health.heap"`` topic
and ``Load`` events on the ``"health.load"`` topic. and ``Load`` events on the ``"health.load"`` topic.
.. includecode:: code/docs/zeromq/ZeromqDocTestBase.java#import-health
.. includecode:: code/docs/zeromq/ZeromqDocTestBase.java#health .. includecode:: code/docs/zeromq/ZeromqDocTestBase.java#health
.. includecode:: code/docs/zeromq/ZeromqDocTestBase.java#health2 .. includecode:: code/docs/zeromq/ZeromqDocTestBase.java#health2

View file

@ -14,10 +14,12 @@ akka {
# directory below which this queue resides # directory below which this queue resides
directory-path = "./_mb" directory-path = "./_mb"
# attempting to add an item after the queue reaches this size (in items) will fail. # attempting to add an item after the queue reaches this size (in items)
# will fail.
max-items = 2147483647 max-items = 2147483647
# attempting to add an item after the queue reaches this size (in bytes) will fail. # attempting to add an item after the queue reaches this size (in bytes)
# will fail.
max-size = 2147483647 bytes max-size = 2147483647 bytes
# attempting to add an item larger than this size (in bytes) will fail. # attempting to add an item larger than this size (in bytes) will fail.
@ -35,7 +37,8 @@ akka {
# maximum overflow (multiplier) of a journal file before we re-create it. # maximum overflow (multiplier) of a journal file before we re-create it.
max-journal-overflow = 10 max-journal-overflow = 10
# absolute maximum size of a journal file until we rebuild it, no matter what. # absolute maximum size of a journal file until we rebuild it, no matter
# what.
max-journal-size-absolute = 9223372036854775807 bytes max-journal-size-absolute = 9223372036854775807 bytes
# whether to drop older items (instead of newer) when the queue is full # whether to drop older items (instead of newer) when the queue is full
@ -52,10 +55,12 @@ akka {
# maximum number of failures before opening breaker # maximum number of failures before opening breaker
max-failures = 3 max-failures = 3
# duration of time beyond which a call is assumed to be timed out and considered a failure # duration of time beyond which a call is assumed to be timed out and
# considered a failure
call-timeout = 3 seconds call-timeout = 3 seconds
# duration of time to wait until attempting to reset the breaker during which all calls fail-fast # duration of time to wait until attempting to reset the breaker during
# which all calls fail-fast
reset-timeout = 30 seconds reset-timeout = 30 seconds
} }
} }

View file

@ -19,8 +19,9 @@ akka {
serialization-bindings { serialization-bindings {
# Since com.google.protobuf.Message does not extend Serializable but GeneratedMessage # Since com.google.protobuf.Message does not extend Serializable but
# does, need to use the more specific one here in order to avoid ambiguity # GeneratedMessage does, need to use the more specific one here in order
# to avoid ambiguity
"com.google.protobuf.GeneratedMessage" = proto "com.google.protobuf.GeneratedMessage" = proto
"akka.remote.DaemonMsgCreate" = daemon-create "akka.remote.DaemonMsgCreate" = daemon-create
} }
@ -29,8 +30,8 @@ akka {
default { default {
# if this is set to a valid remote address, the named actor will be deployed # if this is set to a valid remote address, the named actor will be
# at that node e.g. "akka://sys@host:port" # deployed at that node e.g. "akka://sys@host:port"
remote = "" remote = ""
target { target {
@ -58,48 +59,56 @@ akka {
# default is a TCP-based remote transport based on Netty # default is a TCP-based remote transport based on Netty
transport = "akka.remote.netty.NettyRemoteTransport" transport = "akka.remote.netty.NettyRemoteTransport"
# Enable untrusted mode for full security of server managed actors, prevents system messages to be send # Enable untrusted mode for full security of server managed actors, prevents
# by clients, e.g. messages like 'Create', 'Suspend', 'Resume', 'Terminate', 'Supervise', 'Link' etc. # system messages to be send by clients, e.g. messages like 'Create',
# 'Suspend', 'Resume', 'Terminate', 'Supervise', 'Link' etc.
untrusted-mode = off untrusted-mode = off
# Timeout for ACK of cluster operations, like checking actor out etc. # Timeout for ACK of cluster operations, like checking actor out etc.
remote-daemon-ack-timeout = 30s remote-daemon-ack-timeout = 30s
# If this is "on", Akka will log all inbound messages at DEBUG level, if off then they are not logged # If this is "on", Akka will log all inbound messages at DEBUG level, if off
# then they are not logged
log-received-messages = off log-received-messages = off
# If this is "on", Akka will log all outbound messages at DEBUG level, if off then they are not logged # If this is "on", Akka will log all outbound messages at DEBUG level, if off
# then they are not logged
log-sent-messages = off log-sent-messages = off
# If this is "on", Akka will log all RemoteLifeCycleEvents at the level defined for each, if off then they are not logged # If this is "on", Akka will log all RemoteLifeCycleEvents at the level
# Failures to deserialize received messages also fall under this flag. # defined for each, if off then they are not logged Failures to deserialize
# received messages also fall under this flag.
log-remote-lifecycle-events = on log-remote-lifecycle-events = on
# Each property is annotated with (I) or (O) or (I&O), where I stands for “inbound” and O for “outbound” connections. # Each property is annotated with (I) or (O) or (I&O), where I stands for
# The NettyRemoteTransport always starts the server role to allow inbound connections, and it starts # “inbound” and O for “outbound” connections. The NettyRemoteTransport always
# active client connections whenever sending to a destination which is not yet connected; if configured # starts the server role to allow inbound connections, and it starts active
# it reuses inbound connections for replies, which is called a passive client connection (i.e. from server # client connections whenever sending to a destination which is not yet
# to client). # connected; if configured it reuses inbound connections for replies, which
# is called a passive client connection (i.e. from server to client).
netty { netty {
# (O) In case of increased latency / overflow how long should we wait (blocking the sender) # (O) In case of increased latency / overflow how long should we wait
# until we deem the send to be cancelled? # (blocking the sender) until we deem the send to be cancelled?
# 0 means "never backoff", any positive number will indicate time to block at most. # 0 means "never backoff", any positive number will indicate the time to
# block at most.
backoff-timeout = 0ms backoff-timeout = 0ms
# (I&O) Generate your own with '$AKKA_HOME/scripts/generate_config_with_secure_cookie.sh' # (I&O) Generate your own with the script availbale in
# or using 'akka.util.Crypt.generateSecureCookie' # '$AKKA_HOME/scripts/generate_config_with_secure_cookie.sh' or using
# 'akka.util.Crypt.generateSecureCookie'
secure-cookie = "" secure-cookie = ""
# (I) Should the remote server require that its peers share the same secure-cookie # (I) Should the remote server require that its peers share the same
# (defined in the 'remote' section)? # secure-cookie (defined in the 'remote' section)?
require-cookie = off require-cookie = off
# (I) Reuse inbound connections for outbound messages # (I) Reuse inbound connections for outbound messages
use-passive-connections = on use-passive-connections = on
# (I) EXPERIMENTAL If "<id.of.dispatcher>" then the specified dispatcher will be used to accept inbound connections, # (I) EXPERIMENTAL If "<id.of.dispatcher>" then the specified dispatcher
# and perform IO. If "" then dedicated threads will be used. # will be used to accept inbound connections, and perform IO. If "" then
# dedicated threads will be used.
use-dispatcher-for-io = "" use-dispatcher-for-io = ""
# (I) The hostname or ip to bind the remoting to, # (I) The hostname or ip to bind the remoting to,
@ -111,11 +120,13 @@ akka {
# This port needs to be unique for each actor system on the same machine. # This port needs to be unique for each actor system on the same machine.
port = 2552 port = 2552
# (O) The address of a local network interface (IP Address) to bind to when creating # (O) The address of a local network interface (IP Address) to bind to when
# outbound connections. Set to "" or "auto" for automatic selection of local address. # creating outbound connections. Set to "" or "auto" for automatic selection
# of local address.
outbound-local-address = "auto" outbound-local-address = "auto"
# (I&O) Increase this if you want to be able to send messages with large payloads # (I&O) Increase this if you want to be able to send messages with large
# payloads
message-frame-size = 1 MiB message-frame-size = 1 MiB
# (O) Sets the connectTimeoutMillis of all outbound connections, # (O) Sets the connectTimeoutMillis of all outbound connections,
@ -125,11 +136,14 @@ akka {
# (I) Sets the size of the connection backlog # (I) Sets the size of the connection backlog
backlog = 4096 backlog = 4096
# (I) Length in akka.time-unit how long core threads will be kept alive if idling # (I) Length in akka.time-unit how long core threads will be kept alive if
# idling
execution-pool-keepalive = 60s execution-pool-keepalive = 60s
# (I) Size in number of threads of the core pool of the remote execution unit. # (I) Size in number of threads of the core pool of the remote execution
# A value of 0 will turn this off, which is can lead to deadlocks under some configurations! # unit.
# A value of 0 will turn this off, which is can lead to deadlocks under
# some configurations!
execution-pool-size = 4 execution-pool-size = 4
# (I) Maximum channel size, 0 for off # (I) Maximum channel size, 0 for off
@ -138,16 +152,20 @@ akka {
# (I) Maximum total size of all channels, 0 for off # (I) Maximum total size of all channels, 0 for off
max-total-memory-size = 0b max-total-memory-size = 0b
# (I&O) Sets the high water mark for the in and outbound sockets, set to 0b for platform default # (I&O) Sets the high water mark for the in and outbound sockets,
# set to 0b for platform default
write-buffer-high-water-mark = 0b write-buffer-high-water-mark = 0b
# (I&O) Sets the low water mark for the in and outbound sockets, set to 0b for platform default # (I&O) Sets the low water mark for the in and outbound sockets,
# set to 0b for platform default
write-buffer-low-water-mark = 0b write-buffer-low-water-mark = 0b
# (I&O) Sets the send buffer size of the Sockets, set to 0b for platform default # (I&O) Sets the send buffer size of the Sockets,
# set to 0b for platform default
send-buffer-size = 0b send-buffer-size = 0b
# (I&O) Sets the receive buffer size of the Sockets, set to 0b for platform default # (I&O) Sets the receive buffer size of the Sockets,
# set to 0b for platform default
receive-buffer-size = 0b receive-buffer-size = 0b
# (O) Time between reconnect attempts for active clients # (O) Time between reconnect attempts for active clients
@ -164,9 +182,9 @@ akka {
# A value of 0 will turn this feature off # A value of 0 will turn this feature off
write-timeout = 10s write-timeout = 10s
# (O) Inactivity period of both reads and writes (lowest resolution is seconds) # (O) Inactivity period of both reads and writes (lowest resolution is
# after which active client connection is shutdown; # seconds) after which active client connection is shutdown; will be
# will be re-established in case of new communication requests # re-established in case of new communication requests.
# A value of 0 will turn this feature off # A value of 0 will turn this feature off
all-timeout = 0s all-timeout = 0s
@ -197,26 +215,33 @@ akka {
# 'TLSv1.1', 'TLSv1.2' # 'TLSv1.1', 'TLSv1.2'
protocol = "TLSv1" protocol = "TLSv1"
# Examples: [ "TLS_RSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_256_CBC_SHA" ] # Example: ["TLS_RSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_256_CBC_SHA"]
# You need to install the JCE Unlimited Strength Jurisdiction Policy Files to use AES 256 # You need to install the JCE Unlimited Strength Jurisdiction Policy
# Files to use AES 256.
# More info here: http://docs.oracle.com/javase/7/docs/technotes/guides/security/SunProviders.html#SunJCEProvider # More info here: http://docs.oracle.com/javase/7/docs/technotes/guides/security/SunProviders.html#SunJCEProvider
enabled-algorithms = ["TLS_RSA_WITH_AES_128_CBC_SHA"] enabled-algorithms = ["TLS_RSA_WITH_AES_128_CBC_SHA"]
# Using /dev/./urandom is only necessary when using SHA1PRNG on Linux to prevent blocking # Using /dev/./urandom is only necessary when using SHA1PRNG on Linux to
# It is NOT as secure because it reuses the seed # prevent blocking. It is NOT as secure because it reuses the seed.
# '' => defaults to /dev/random or whatever is set in java.security for example: securerandom.source=file:/dev/random # '' => defaults to /dev/random or whatever is set in java.security for
# '/dev/./urandom' => NOT '/dev/urandom' as that doesn't work according to: http://bugs.sun.com/view_bug.do?bug_id=6202721 # example: securerandom.source=file:/dev/random
# '/dev/./urandom' => NOT '/dev/urandom' as that doesn't work according
# to: http://bugs.sun.com/view_bug.do?bug_id=6202721
sha1prng-random-source = "" sha1prng-random-source = ""
# There are three options, in increasing order of security: # There are three options, in increasing order of security:
# "" or SecureRandom => (default) # "" or SecureRandom => (default)
# "SHA1PRNG" => Can be slow because of blocking issues on Linux # "SHA1PRNG" => Can be slow because of blocking issues on Linux
# "AES128CounterSecureRNG" => fastest startup and based on AES encryption algorithm # "AES128CounterSecureRNG" => fastest startup and based on AES encryption
# algorithm
# "AES256CounterSecureRNG" # "AES256CounterSecureRNG"
# The following use one of 3 possible seed sources, depending on availability: /dev/random, random.org and SecureRandom (provided by Java) # The following use one of 3 possible seed sources, depending on
# availability: /dev/random, random.org and SecureRandom (provided by Java)
# "AES128CounterInetRNG" # "AES128CounterInetRNG"
# "AES256CounterInetRNG" (Install JCE Unlimited Strength Jurisdiction Policy Files first) # "AES256CounterInetRNG" (Install JCE Unlimited Strength Jurisdiction
# Setting a value here may require you to supply the appropriate cipher suite (see enabled-algorithms section above) # Policy Files first)
# Setting a value here may require you to supply the appropriate cipher
# suite (see enabled-algorithms section above)
random-number-generator = "" random-number-generator = ""
} }
} }

View file

@ -10,33 +10,33 @@ import java.text.NumberFormat;
//#actor //#actor
public class JCreationActor extends UntypedActor { public class JCreationActor extends UntypedActor {
private static final NumberFormat formatter = new DecimalFormat("#0.00"); private static final NumberFormat formatter = new DecimalFormat("#0.00");
@Override @Override
public void onReceive(Object message) throws Exception { public void onReceive(Object message) throws Exception {
if (message instanceof InternalMsg.MathOpMsg) { if (message instanceof InternalMsg.MathOpMsg) {
// forward math op to server actor // forward math op to server actor
InternalMsg.MathOpMsg msg = (InternalMsg.MathOpMsg) message; InternalMsg.MathOpMsg msg = (InternalMsg.MathOpMsg) message;
msg.getActor().tell(msg.getMathOp(), getSelf()); msg.getActor().tell(msg.getMathOp(), getSelf());
} else if (message instanceof Op.MathResult) { } else if (message instanceof Op.MathResult) {
// receive reply from server actor // receive reply from server actor
if (message instanceof Op.MultiplicationResult) { if (message instanceof Op.MultiplicationResult) {
Op.MultiplicationResult result = (Op.MultiplicationResult) message; Op.MultiplicationResult result = (Op.MultiplicationResult) message;
System.out.println("Mul result: " + result.getN1() + " * " + System.out.println("Mul result: " + result.getN1() + " * " +
result.getN2() + " = " + result.getResult()); result.getN2() + " = " + result.getResult());
} else if (message instanceof Op.DivisionResult) { } else if (message instanceof Op.DivisionResult) {
Op.DivisionResult result = (Op.DivisionResult) message; Op.DivisionResult result = (Op.DivisionResult) message;
System.out.println("Div result: " + result.getN1() + " / " + System.out.println("Div result: " + result.getN1() + " / " +
result.getN2() + " = " + formatter.format(result.getResult())); result.getN2() + " = " + formatter.format(result.getResult()));
} }
} else { } else {
unhandled(message); unhandled(message);
}
} }
}
} }
//#actor //#actor

View file

@ -36,4 +36,4 @@ public class JCreationApplication implements Bootable {
system.shutdown(); system.shutdown();
} }
} }
// #setup //#setup

View file

@ -21,8 +21,8 @@ public class JLookupApplication implements Bootable {
system = ActorSystem.create("LookupApplication", ConfigFactory.load() system = ActorSystem.create("LookupApplication", ConfigFactory.load()
.getConfig("remotelookup")); .getConfig("remotelookup"));
actor = system.actorOf(new Props(JLookupActor.class)); actor = system.actorOf(new Props(JLookupActor.class));
remoteActor = system remoteActor = system.actorFor(
.actorFor("akka://CalculatorApplication@127.0.0.1:2552/user/simpleCalculator"); "akka://CalculatorApplication@127.0.0.1:2552/user/simpleCalculator");
} }
public void doSomething(Op.MathOp mathOp) { public void doSomething(Op.MathOp mathOp) {

View file

@ -17,7 +17,8 @@ akka {
socket-dispatcher { socket-dispatcher {
# A zeromq socket needs to be pinned to the thread that created it. # A zeromq socket needs to be pinned to the thread that created it.
# Changing this value results in weird errors and race conditions within zeromq # Changing this value results in weird errors and race conditions within
# zeromq
executor = thread-pool-executor executor = thread-pool-executor
type = "PinnedDispatcher" type = "PinnedDispatcher"
} }