diff --git a/akka-actor-tests/src/test/java/akka/actor/JavaExtension.java b/akka-actor-tests/src/test/java/akka/actor/JavaExtension.java index 28b87bb5db..616675eb77 100644 --- a/akka-actor-tests/src/test/java/akka/actor/JavaExtension.java +++ b/akka-actor-tests/src/test/java/akka/actor/JavaExtension.java @@ -40,10 +40,10 @@ public class JavaExtension { static final ExtensionKey key = new ExtensionKey(OtherExtension.class) { }; - public final ActorSystemImpl system; + public final ExtendedActorSystem system; - public OtherExtension(ActorSystemImpl i) { - system = i; + public OtherExtension(ExtendedActorSystem system) { + this.system = system; } } diff --git a/akka-actor-tests/src/test/scala/akka/actor/ActorRefSpec.scala b/akka-actor-tests/src/test/scala/akka/actor/ActorRefSpec.scala index a2c3c7da5a..e8c667bc7e 100644 --- a/akka-actor-tests/src/test/scala/akka/actor/ActorRefSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/actor/ActorRefSpec.scala @@ -11,11 +11,10 @@ import akka.testkit._ import akka.util.Timeout import akka.util.duration._ import java.lang.IllegalStateException -import akka.util.ReflectiveAccess -import akka.serialization.Serialization import java.util.concurrent.{ CountDownLatch, TimeUnit } import akka.dispatch.{ Await, DefaultPromise, Promise, Future } import akka.pattern.ask +import akka.serialization.JavaSerializer object ActorRefSpec { @@ -240,6 +239,7 @@ class ActorRefSpec extends AkkaSpec with DefaultTimeout { "be serializable using Java Serialization on local node" in { val a = system.actorOf(Props[InnerActor]) + val esys = system.asInstanceOf[ExtendedActorSystem] import java.io._ @@ -251,14 +251,21 @@ class ActorRefSpec extends AkkaSpec with DefaultTimeout { out.flush out.close - Serialization.currentSystem.withValue(system.asInstanceOf[ActorSystemImpl]) { - val in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray)) + val bytes = baos.toByteArray + + JavaSerializer.currentSystem.withValue(esys) { + val in = new ObjectInputStream(new ByteArrayInputStream(bytes)) val readA = in.readObject a.isInstanceOf[LocalActorRef] must be === true readA.isInstanceOf[LocalActorRef] must be === true (readA eq a) must be === true } + + val ser = new JavaSerializer(esys) + val readA = ser.fromBinary(bytes, None) + readA.isInstanceOf[LocalActorRef] must be === true + (readA eq a) must be === true } "throw an exception on deserialize if no system in scope" in { @@ -297,7 +304,7 @@ class ActorRefSpec extends AkkaSpec with DefaultTimeout { out.flush out.close - Serialization.currentSystem.withValue(sysImpl) { + JavaSerializer.currentSystem.withValue(sysImpl) { val in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray)) in.readObject must be === new EmptyLocalActorRef(sysImpl.provider, system.actorFor("/").path / "non-existing", system.eventStream) } diff --git a/akka-actor-tests/src/test/scala/akka/actor/TypedActorSpec.scala b/akka-actor-tests/src/test/scala/akka/actor/TypedActorSpec.scala index b83fe78338..4e8bc4d7b4 100644 --- a/akka-actor-tests/src/test/scala/akka/actor/TypedActorSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/actor/TypedActorSpec.scala @@ -8,7 +8,6 @@ import org.scalatest.{ BeforeAndAfterAll, BeforeAndAfterEach } import akka.util.Duration import akka.util.Timeout import akka.util.duration._ -import akka.serialization.Serialization import java.util.concurrent.atomic.AtomicReference import annotation.tailrec import akka.testkit.{ EventFilter, filterEvents, AkkaSpec } @@ -19,6 +18,7 @@ import akka.japi.{ Creator, Option ⇒ JOption } import akka.testkit.DefaultTimeout import akka.dispatch.{ Await, Dispatchers, Future, Promise } import akka.pattern.ask +import akka.serialization.JavaSerializer object TypedActorSpec { @@ -367,7 +367,7 @@ class TypedActorSpec extends AkkaSpec(TypedActorSpec.config) "be able to serialize and deserialize invocations" in { import java.io._ - Serialization.currentSystem.withValue(system.asInstanceOf[ActorSystemImpl]) { + JavaSerializer.currentSystem.withValue(system.asInstanceOf[ExtendedActorSystem]) { val m = TypedActor.MethodCall(classOf[Foo].getDeclaredMethod("pigdog"), Array[AnyRef]()) val baos = new ByteArrayOutputStream(8192 * 4) val out = new ObjectOutputStream(baos) @@ -386,7 +386,7 @@ class TypedActorSpec extends AkkaSpec(TypedActorSpec.config) "be able to serialize and deserialize invocations' parameters" in { import java.io._ val someFoo: Foo = new Bar - Serialization.currentSystem.withValue(system.asInstanceOf[ActorSystemImpl]) { + JavaSerializer.currentSystem.withValue(system.asInstanceOf[ExtendedActorSystem]) { val m = TypedActor.MethodCall(classOf[Foo].getDeclaredMethod("testMethodCallSerialization", Array[Class[_]](classOf[Foo], classOf[String], classOf[Int]): _*), Array[AnyRef](someFoo, null, 1.asInstanceOf[AnyRef])) val baos = new ByteArrayOutputStream(8192 * 4) val out = new ObjectOutputStream(baos) diff --git a/akka-actor-tests/src/test/scala/akka/actor/dispatch/ActorModelSpec.scala b/akka-actor-tests/src/test/scala/akka/actor/dispatch/ActorModelSpec.scala index 8c949f8776..46bf609c7a 100644 --- a/akka-actor-tests/src/test/scala/akka/actor/dispatch/ActorModelSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/actor/dispatch/ActorModelSpec.scala @@ -50,6 +50,8 @@ object ActorModelSpec { case object Restart extends ActorModelMessage + case object DoubleStop extends ActorModelMessage + case class ThrowException(e: Throwable) extends ActorModelMessage val Ping = "Ping" @@ -86,6 +88,7 @@ object ActorModelSpec { case Restart ⇒ ack; busy.switchOff(); throw new Exception("Restart requested") case Interrupt ⇒ ack; sender ! Status.Failure(new ActorInterruptedException(new InterruptedException("Ping!"))); busy.switchOff(); throw new InterruptedException("Ping!") case ThrowException(e: Throwable) ⇒ ack; busy.switchOff(); throw e + case DoubleStop ⇒ ack; context.stop(self); context.stop(self); busy.switchOff } } @@ -190,13 +193,13 @@ object ActorModelSpec { } def assertRef(actorRef: ActorRef, dispatcher: MessageDispatcher = null)( - suspensions: Long = statsFor(actorRef).suspensions.get(), - resumes: Long = statsFor(actorRef).resumes.get(), - registers: Long = statsFor(actorRef).registers.get(), - unregisters: Long = statsFor(actorRef).unregisters.get(), - msgsReceived: Long = statsFor(actorRef).msgsReceived.get(), - msgsProcessed: Long = statsFor(actorRef).msgsProcessed.get(), - restarts: Long = statsFor(actorRef).restarts.get())(implicit system: ActorSystem) { + suspensions: Long = statsFor(actorRef, dispatcher).suspensions.get(), + resumes: Long = statsFor(actorRef, dispatcher).resumes.get(), + registers: Long = statsFor(actorRef, dispatcher).registers.get(), + unregisters: Long = statsFor(actorRef, dispatcher).unregisters.get(), + msgsReceived: Long = statsFor(actorRef, dispatcher).msgsReceived.get(), + msgsProcessed: Long = statsFor(actorRef, dispatcher).msgsProcessed.get(), + restarts: Long = statsFor(actorRef, dispatcher).restarts.get())(implicit system: ActorSystem) { val stats = statsFor(actorRef, Option(dispatcher).getOrElse(actorRef.asInstanceOf[LocalActorRef].underlying.dispatcher)) val deadline = System.currentTimeMillis + 1000 try { @@ -426,6 +429,14 @@ abstract class ActorModelSpec(config: String) extends AkkaSpec(config) with Defa assert(f5.value.isEmpty) } } + + "not double-deregister" in { + implicit val dispatcher = interceptedDispatcher() + val a = newTestActor(dispatcher.id) + a ! DoubleStop + awaitCond(statsFor(a, dispatcher).registers.get == 1) + awaitCond(statsFor(a, dispatcher).unregisters.get == 1) + } } } diff --git a/akka-actor-tests/src/test/scala/akka/config/ConfigSpec.scala b/akka-actor-tests/src/test/scala/akka/config/ConfigSpec.scala index 9a0eab8830..dd5149ad8e 100644 --- a/akka-actor-tests/src/test/scala/akka/config/ConfigSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/config/ConfigSpec.scala @@ -29,10 +29,10 @@ class ConfigSpec extends AkkaSpec(ConfigFactory.defaultReference) { getBoolean("akka.actor.serialize-messages") must equal(false) settings.SerializeAllMessages must equal(false) - getInt("akka.scheduler.ticksPerWheel") must equal(512) + getInt("akka.scheduler.ticks-per-wheel") must equal(512) settings.SchedulerTicksPerWheel must equal(512) - getMilliseconds("akka.scheduler.tickDuration") must equal(100) + getMilliseconds("akka.scheduler.tick-duration") must equal(100) settings.SchedulerTickDuration must equal(100 millis) settings.Daemonicity must be(false) @@ -49,7 +49,7 @@ class ConfigSpec extends AkkaSpec(ConfigFactory.defaultReference) { c.getString("executor") must equal("fork-join-executor") c.getInt("mailbox-capacity") must equal(-1) c.getMilliseconds("mailbox-push-timeout-time") must equal(10 * 1000) - c.getString("mailboxType") must be("") + c.getString("mailbox-type") must be("") c.getMilliseconds("shutdown-timeout") must equal(1 * 1000) c.getInt("throughput") must equal(5) c.getMilliseconds("throughput-deadline-time") must equal(0) diff --git a/akka-actor-tests/src/test/scala/akka/dispatch/FutureSpec.scala b/akka-actor-tests/src/test/scala/akka/dispatch/FutureSpec.scala index e058218f2d..6163123632 100644 --- a/akka-actor-tests/src/test/scala/akka/dispatch/FutureSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/dispatch/FutureSpec.scala @@ -860,7 +860,7 @@ class FutureSpec extends AkkaSpec with Checkers with BeforeAndAfterAll with Defa val l1, l2 = new TestLatch val complex = Future() map { _ ⇒ - Future.blocking(system.dispatcher) + Future.blocking() val nested = Future(()) nested foreach (_ ⇒ l1.open()) Await.ready(l1, TestLatch.DefaultTimeout) // make sure nested is completed diff --git a/akka-actor-tests/src/test/scala/akka/dispatch/MailboxConfigSpec.scala b/akka-actor-tests/src/test/scala/akka/dispatch/MailboxConfigSpec.scala index b797827680..4f787a730f 100644 --- a/akka-actor-tests/src/test/scala/akka/dispatch/MailboxConfigSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/dispatch/MailboxConfigSpec.scala @@ -153,7 +153,7 @@ class PriorityMailboxSpec extends MailboxSpec { object CustomMailboxSpec { val config = """ my-dispatcher { - mailboxType = "akka.dispatch.CustomMailboxSpec$MyMailboxType" + mailbox-type = "akka.dispatch.CustomMailboxSpec$MyMailboxType" } """ diff --git a/akka-actor-tests/src/test/scala/akka/dispatch/PriorityDispatcherSpec.scala b/akka-actor-tests/src/test/scala/akka/dispatch/PriorityDispatcherSpec.scala index 2bb4ab73e5..855c4f6965 100644 --- a/akka-actor-tests/src/test/scala/akka/dispatch/PriorityDispatcherSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/dispatch/PriorityDispatcherSpec.scala @@ -10,10 +10,10 @@ import com.typesafe.config.Config object PriorityDispatcherSpec { val config = """ unbounded-prio-dispatcher { - mailboxType = "akka.dispatch.PriorityDispatcherSpec$Unbounded" + mailbox-type = "akka.dispatch.PriorityDispatcherSpec$Unbounded" } bounded-prio-dispatcher { - mailboxType = "akka.dispatch.PriorityDispatcherSpec$Bounded" + mailbox-type = "akka.dispatch.PriorityDispatcherSpec$Bounded" } """ diff --git a/akka-actor-tests/src/test/scala/akka/routing/ResizerSpec.scala b/akka-actor-tests/src/test/scala/akka/routing/ResizerSpec.scala index 863922491b..2130afe107 100644 --- a/akka-actor-tests/src/test/scala/akka/routing/ResizerSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/routing/ResizerSpec.scala @@ -24,6 +24,9 @@ object ResizerSpec { } } } + bal-disp { + type = BalancingDispatcher + } """ class TestActor extends Actor { @@ -133,7 +136,7 @@ class ResizerSpec extends AkkaSpec(ResizerSpec.config) with DefaultTimeout with pressureThreshold = 0, messagesPerResize = 1) - val router = system.actorOf(Props[BusyActor].withRouter(RoundRobinRouter(resizer = Some(resizer)))) + val router = system.actorOf(Props[BusyActor].withRouter(RoundRobinRouter(resizer = Some(resizer))).withDispatcher("bal-disp")) val latch1 = new TestLatch(1) router ! (latch1, busy) @@ -179,10 +182,10 @@ class ResizerSpec extends AkkaSpec(ResizerSpec.config) with DefaultTimeout with Await.result(router ? CurrentRoutees, 5 seconds).asInstanceOf[RouterRoutees].routees.size must be(2) def loop(loops: Int, t: Int, latch: TestLatch, count: AtomicInteger) = { - (10 millis).dilated.sleep + (100 millis).dilated.sleep for (m ← 0 until loops) { router.!((t, latch, count)) - (10 millis).dilated.sleep + (100 millis).dilated.sleep } } @@ -198,7 +201,7 @@ class ResizerSpec extends AkkaSpec(ResizerSpec.config) with DefaultTimeout with // a whole bunch should max it out val count2 = new AtomicInteger val latch2 = TestLatch(10) - loop(10, 200, latch2, count2) + loop(10, 500, latch2, count2) Await.ready(latch2, TestLatch.DefaultTimeout) count2.get must be(10) @@ -238,7 +241,7 @@ class ResizerSpec extends AkkaSpec(ResizerSpec.config) with DefaultTimeout with // let it cool down for (m ← 0 to 3) { router ! 1 - (200 millis).dilated.sleep + (500 millis).dilated.sleep } Await.result(router ? CurrentRoutees, 5 seconds).asInstanceOf[RouterRoutees].routees.size must be < (z) diff --git a/akka-actor-tests/src/test/scala/akka/routing/RoutingSpec.scala b/akka-actor-tests/src/test/scala/akka/routing/RoutingSpec.scala index 9de51bdabf..ad3702d556 100644 --- a/akka-actor-tests/src/test/scala/akka/routing/RoutingSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/routing/RoutingSpec.scala @@ -15,6 +15,7 @@ import com.typesafe.config.ConfigFactory import akka.pattern.ask import java.util.concurrent.ConcurrentHashMap import com.typesafe.config.Config +import akka.dispatch.Dispatchers object RoutingSpec { @@ -51,6 +52,7 @@ object RoutingSpec { case (sender, message) ⇒ Nil } } + def routerDispatcher: String = Dispatchers.DefaultDispatcherId } } @@ -539,6 +541,8 @@ class RoutingSpec extends AkkaSpec(RoutingSpec.config) with DefaultTimeout with //#crRouter case class VoteCountRouter() extends RouterConfig { + def routerDispatcher: String = Dispatchers.DefaultDispatcherId + //#crRoute def createRoute(routeeProps: Props, routeeProvider: RouteeProvider): Route = { val democratActor = routeeProvider.context.actorOf(Props(new DemocratActor()), "d") diff --git a/akka-actor-tests/src/test/scala/akka/serialization/SerializeSpec.scala b/akka-actor-tests/src/test/scala/akka/serialization/SerializeSpec.scala index 2ca3581783..82cd6e0e06 100644 --- a/akka-actor-tests/src/test/scala/akka/serialization/SerializeSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/serialization/SerializeSpec.scala @@ -89,7 +89,7 @@ class SerializeSpec extends AkkaSpec(SerializeSpec.config) { case Left(exception) ⇒ fail(exception) case Right(bytes) ⇒ bytes } - deserialize(b.asInstanceOf[Array[Byte]], classOf[Address], None) match { + deserialize(b.asInstanceOf[Array[Byte]], classOf[Address]) match { case Left(exception) ⇒ fail(exception) case Right(add) ⇒ assert(add === addr) } @@ -101,7 +101,7 @@ class SerializeSpec extends AkkaSpec(SerializeSpec.config) { case Left(exception) ⇒ fail(exception) case Right(bytes) ⇒ bytes } - deserialize(b.asInstanceOf[Array[Byte]], classOf[Person], None) match { + deserialize(b.asInstanceOf[Array[Byte]], classOf[Person]) match { case Left(exception) ⇒ fail(exception) case Right(p) ⇒ assert(p === person) } @@ -114,7 +114,7 @@ class SerializeSpec extends AkkaSpec(SerializeSpec.config) { case Left(exception) ⇒ fail(exception) case Right(bytes) ⇒ bytes } - deserialize(b.asInstanceOf[Array[Byte]], classOf[Record], None) match { + deserialize(b.asInstanceOf[Array[Byte]], classOf[Record]) match { case Left(exception) ⇒ fail(exception) case Right(p) ⇒ assert(p === r) } @@ -146,7 +146,7 @@ class SerializeSpec extends AkkaSpec(SerializeSpec.config) { out.close() val in = new ObjectInputStream(new ByteArrayInputStream(outbuf.toByteArray)) - Serialization.currentSystem.withValue(a.asInstanceOf[ActorSystemImpl]) { + JavaSerializer.currentSystem.withValue(a.asInstanceOf[ActorSystemImpl]) { val deadLetters = in.readObject().asInstanceOf[DeadLetterActorRef] (deadLetters eq a.deadLetters) must be(true) } @@ -285,8 +285,5 @@ class TestSerializer extends Serializer { Array.empty[Byte] } - def fromBinary(bytes: Array[Byte], clazz: Option[Class[_]] = None, - classLoader: Option[ClassLoader] = None): AnyRef = { - null - } + def fromBinary(bytes: Array[Byte], clazz: Option[Class[_]]): AnyRef = null } diff --git a/akka-actor/src/main/resources/reference.conf b/akka-actor/src/main/resources/reference.conf index ce9bf684e8..bc52938a7d 100644 --- a/akka-actor/src/main/resources/reference.conf +++ b/akka-actor/src/main/resources/reference.conf @@ -26,7 +26,7 @@ akka { # Log the complete configuration at INFO level when the actor system is started. # This is useful when you are uncertain of what configuration is used. - logConfigOnStart = off + log-config-on-start = off # List FQCN of extensions which shall be loaded at actor system startup. # Should be on the format: 'extensions = ["foo", "bar"]' etc. @@ -37,7 +37,7 @@ akka { daemonic = off # JVM shutdown, System.exit(-1), in case of a fatal error, such as OutOfMemoryError - jvmExitOnFatalError = on + jvm-exit-on-fatal-error = on actor { @@ -185,37 +185,37 @@ akka { # This will be used if you have set "executor = "thread-pool-executor"" thread-pool-executor { - # Keep alive time for threads - keep-alive-time = 60s + # Keep alive time for threads + keep-alive-time = 60s - # Min number of threads to cap factor-based core number to - core-pool-size-min = 8 + # Min number of threads to cap factor-based core number to + core-pool-size-min = 8 - # No of core threads ... ceil(available processors * factor) - core-pool-size-factor = 3.0 + # No of core threads ... ceil(available processors * factor) + core-pool-size-factor = 3.0 - # Max number of threads to cap factor-based number to - core-pool-size-max = 64 + # Max number of threads to cap factor-based number to + core-pool-size-max = 64 - # Hint: max-pool-size is only used for bounded task queues - # minimum number of threads to cap factor-based max number to - max-pool-size-min = 8 + # Hint: max-pool-size is only used for bounded task queues + # minimum number of threads to cap factor-based max number to + max-pool-size-min = 8 - # Max no of threads ... ceil(available processors * factor) - max-pool-size-factor = 3.0 + # Max no of threads ... ceil(available processors * factor) + max-pool-size-factor = 3.0 - # Max number of threads to cap factor-based max number to - max-pool-size-max = 64 + # Max number of threads to cap factor-based max number to + max-pool-size-max = 64 - # Specifies the bounded capacity of the task queue (< 1 == unbounded) - task-queue-size = -1 + # Specifies the bounded capacity of the task queue (< 1 == unbounded) + task-queue-size = -1 - # Specifies which type of task queue will be used, can be "array" or - # "linked" (default) - task-queue-type = "linked" + # Specifies which type of task queue will be used, can be "array" or + # "linked" (default) + task-queue-type = "linked" - # Allow core threads to time out - allow-core-timeout = on + # Allow core threads to time out + allow-core-timeout = on } # How long time the dispatcher will wait for new actors until it shuts down @@ -245,7 +245,7 @@ akka { # FQCN of the MailboxType, if not specified the default bounded or unbounded # mailbox is used. The Class of the FQCN must have a constructor with a # com.typesafe.config.Config parameter. - mailboxType = "" + mailbox-type = "" } debug { @@ -272,7 +272,7 @@ akka { } # 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 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 @@ -294,7 +294,7 @@ akka { # or larger tick duration. # 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/ - tickDuration = 100ms - ticksPerWheel = 512 + tick-duration = 100ms + ticks-per-wheel = 512 } } diff --git a/akka-actor/src/main/scala/akka/AkkaException.scala b/akka-actor/src/main/scala/akka/AkkaException.scala index 2abd10c270..85de2504d3 100644 --- a/akka-actor/src/main/scala/akka/AkkaException.scala +++ b/akka-actor/src/main/scala/akka/AkkaException.scala @@ -5,10 +5,8 @@ package akka import akka.actor.newUuid -import java.net.{ InetAddress, UnknownHostException } object AkkaException { - val hostname = try InetAddress.getLocalHost.getHostAddress catch { case e: UnknownHostException ⇒ "unknown host" } def toStringWithStackTrace(throwable: Throwable): String = throwable match { case null ⇒ "Unknown Throwable: was 'null'" @@ -36,7 +34,7 @@ object AkkaException { */ //TODO add @SerialVersionUID(1L) when SI-4804 is fixed class AkkaException(message: String = "", cause: Throwable = null) extends RuntimeException(message, cause) with Serializable { - val uuid = "%s_%s".format(AkkaException.hostname, newUuid) + lazy val uuid = newUuid.toString override lazy val toString = "%s:%s\n[%s]".format(getClass.getName, message, uuid) diff --git a/akka-actor/src/main/scala/akka/actor/ActorCell.scala b/akka-actor/src/main/scala/akka/actor/ActorCell.scala index 2b56bef5d1..aa718e12c8 100644 --- a/akka-actor/src/main/scala/akka/actor/ActorCell.scala +++ b/akka-actor/src/main/scala/akka/actor/ActorCell.scala @@ -220,7 +220,7 @@ private[akka] class ActorCell( val ser = SerializationExtension(system) ser.serialize(props.creator) match { case Left(t) ⇒ throw t - case Right(bytes) ⇒ ser.deserialize(bytes, props.creator.getClass, None) match { + case Right(bytes) ⇒ ser.deserialize(bytes, props.creator.getClass) match { case Left(t) ⇒ throw t case _ ⇒ //All good } diff --git a/akka-actor/src/main/scala/akka/actor/ActorRef.scala b/akka-actor/src/main/scala/akka/actor/ActorRef.scala index 7485d8b11c..bd2c0cf196 100644 --- a/akka-actor/src/main/scala/akka/actor/ActorRef.scala +++ b/akka-actor/src/main/scala/akka/actor/ActorRef.scala @@ -8,7 +8,7 @@ import akka.dispatch._ import akka.util._ import scala.collection.immutable.Stack import java.lang.{ UnsupportedOperationException, IllegalStateException } -import akka.serialization.Serialization +import akka.serialization.{ Serialization, JavaSerializer } import akka.event.EventStream import scala.annotation.tailrec import java.util.concurrent.{ ConcurrentHashMap } @@ -335,7 +335,7 @@ private[akka] class LocalActorRef private[akka] ( */ //TODO add @SerialVersionUID(1L) when SI-4804 is fixed case class SerializedActorRef private (path: String) { - import akka.serialization.Serialization.currentSystem + import akka.serialization.JavaSerializer.currentSystem @throws(classOf[java.io.ObjectStreamException]) def readResolve(): AnyRef = currentSystem.value match { @@ -401,7 +401,7 @@ private[akka] object DeadLetterActorRef { //TODO add @SerialVersionUID(1L) when SI-4804 is fixed class SerializedDeadLetterActorRef extends Serializable { //TODO implement as Protobuf for performance? @throws(classOf[java.io.ObjectStreamException]) - private def readResolve(): AnyRef = Serialization.currentSystem.value.deadLetters + private def readResolve(): AnyRef = JavaSerializer.currentSystem.value.deadLetters } val serialized = new SerializedDeadLetterActorRef diff --git a/akka-actor/src/main/scala/akka/actor/ActorRefProvider.scala b/akka-actor/src/main/scala/akka/actor/ActorRefProvider.scala index 688c036380..a176c6271d 100644 --- a/akka-actor/src/main/scala/akka/actor/ActorRefProvider.scala +++ b/akka-actor/src/main/scala/akka/actor/ActorRefProvider.scala @@ -318,12 +318,12 @@ class LocalActorRefProvider( settings: ActorSystem.Settings, eventStream: EventStream, scheduler: Scheduler, - classloader: ClassLoader) = + dynamicAccess: DynamicAccess) = this(_systemName, settings, eventStream, scheduler, - new Deployer(settings, classloader)) + new Deployer(settings, dynamicAccess)) val rootPath: ActorPath = RootActorPath(Address("akka", _systemName)) diff --git a/akka-actor/src/main/scala/akka/actor/ActorSystem.scala b/akka-actor/src/main/scala/akka/actor/ActorSystem.scala index 72f2505994..ac77628c2c 100644 --- a/akka-actor/src/main/scala/akka/actor/ActorSystem.scala +++ b/akka-actor/src/main/scala/akka/actor/ActorSystem.scala @@ -77,7 +77,7 @@ object ActorSystem { final val LogLevel = getString("akka.loglevel") final val StdoutLogLevel = getString("akka.stdout-loglevel") final val EventHandlers: Seq[String] = getStringList("akka.event-handlers").asScala - final val LogConfigOnStart = config.getBoolean("akka.logConfigOnStart") + final val LogConfigOnStart = config.getBoolean("akka.log-config-on-start") final val AddLoggingReceive = getBoolean("akka.actor.debug.receive") final val DebugAutoReceive = getBoolean("akka.actor.debug.autoreceive") final val DebugLifecycle = getBoolean("akka.actor.debug.lifecycle") @@ -89,10 +89,10 @@ object ActorSystem { case x ⇒ Some(x) } - final val SchedulerTickDuration = Duration(getMilliseconds("akka.scheduler.tickDuration"), MILLISECONDS) - final val SchedulerTicksPerWheel = getInt("akka.scheduler.ticksPerWheel") + final val SchedulerTickDuration = Duration(getMilliseconds("akka.scheduler.tick-duration"), MILLISECONDS) + final val SchedulerTicksPerWheel = getInt("akka.scheduler.ticks-per-wheel") final val Daemonicity = getBoolean("akka.daemonic") - final val JvmExitOnFatalError = getBoolean("akka.jvmExitOnFatalError") + final val JvmExitOnFatalError = getBoolean("akka.jvm-exit-on-fatal-error") if (ConfigVersion != Version) throw new ConfigurationException("Akka JAR version [" + Version + "] does not match the provided config version [" + ConfigVersion + "]") @@ -325,16 +325,16 @@ abstract class ExtendedActorSystem extends ActorSystem { def deathWatch: DeathWatch /** - * ClassLoader which is used for reflective accesses internally. This is set - * to the context class loader, if one is set, or the class loader which + * ClassLoader wrapper which is used for reflective accesses internally. This is set + * to use the context class loader, if one is set, or the class loader which * loaded the ActorSystem implementation. The context class loader is also * set on all threads created by the ActorSystem, if one was set during * creation. */ - def internalClassLoader: ClassLoader + def dynamicAccess: DynamicAccess } -class ActorSystemImpl(val name: String, applicationConfig: Config) extends ExtendedActorSystem { +class ActorSystemImpl protected[akka] (val name: String, applicationConfig: Config) extends ExtendedActorSystem { if (!name.matches("""^\w+$""")) throw new IllegalArgumentException("invalid ActorSystem name [" + name + "], must contain only word characters (i.e. [a-zA-Z_0-9])") @@ -358,6 +358,35 @@ class ActorSystemImpl(val name: String, applicationConfig: Config) extends Exten final val threadFactory: MonitorableThreadFactory = MonitorableThreadFactory(name, settings.Daemonicity, Option(Thread.currentThread.getContextClassLoader), uncaughtExceptionHandler) + /** + * This is an extension point: by overriding this method, subclasses can + * control all reflection activities of an actor system. + */ + protected def createDynamicAccess(): DynamicAccess = new ReflectiveDynamicAccess(findClassLoader) + + protected def findClassLoader: ClassLoader = { + def findCaller(get: Int ⇒ Class[_]): ClassLoader = { + val frames = Iterator.from(2).map(get) + frames dropWhile { c ⇒ + c != null && + (c.getName.startsWith("akka.actor.ActorSystem") || + c.getName.startsWith("scala.Option") || + c.getName.startsWith("scala.collection.Iterator") || + c.getName.startsWith("akka.util.Reflect")) + } next () match { + case null ⇒ getClass.getClassLoader + case c ⇒ c.getClassLoader + } + } + + Option(Thread.currentThread.getContextClassLoader) orElse + (Reflect.getCallerClass map findCaller) getOrElse + getClass.getClassLoader + } + + private val _pm: DynamicAccess = createDynamicAccess() + def dynamicAccess: DynamicAccess = _pm + def logConfiguration(): Unit = log.info(settings.toString) protected def systemImpl = this @@ -408,17 +437,15 @@ class ActorSystemImpl(val name: String, applicationConfig: Config) extends Exten val scheduler: Scheduler = createScheduler() - val internalClassLoader = Option(Thread.currentThread.getContextClassLoader) getOrElse getClass.getClassLoader - val provider: ActorRefProvider = { val arguments = Seq( classOf[String] -> name, classOf[Settings] -> settings, classOf[EventStream] -> eventStream, classOf[Scheduler] -> scheduler, - classOf[ClassLoader] -> internalClassLoader) + classOf[DynamicAccess] -> dynamicAccess) - ReflectiveAccess.createInstance[ActorRefProvider](ProviderClass, arguments, internalClassLoader) match { + dynamicAccess.createInstanceFor[ActorRefProvider](ProviderClass, arguments) match { case Left(e) ⇒ throw e case Right(p) ⇒ p } @@ -440,7 +467,7 @@ class ActorSystemImpl(val name: String, applicationConfig: Config) extends Exten def locker: Locker = provider.locker val dispatchers: Dispatchers = new Dispatchers(settings, DefaultDispatcherPrerequisites( - threadFactory, eventStream, deadLetterMailbox, scheduler, internalClassLoader)) + threadFactory, eventStream, deadLetterMailbox, scheduler, dynamicAccess)) val dispatcher: MessageDispatcher = dispatchers.defaultGlobalDispatcher @@ -559,8 +586,7 @@ class ActorSystemImpl(val name: String, applicationConfig: Config) extends Exten private def loadExtensions() { import scala.collection.JavaConversions._ settings.config.getStringList("akka.extensions") foreach { fqcn ⇒ - import ReflectiveAccess.{ getObjectFor, createInstance, noParams, noArgs } - getObjectFor[AnyRef](fqcn, internalClassLoader).fold(_ ⇒ createInstance[AnyRef](fqcn, noParams, noArgs), Right(_)) match { + dynamicAccess.getObjectFor[AnyRef](fqcn).fold(_ ⇒ dynamicAccess.createInstanceFor[AnyRef](fqcn, Seq()), Right(_)) match { case Right(p: ExtensionIdProvider) ⇒ registerExtension(p.lookup()); case Right(p: ExtensionId[_]) ⇒ registerExtension(p); case Right(other) ⇒ log.error("[{}] is not an 'ExtensionIdProvider' or 'ExtensionId', skipping...", fqcn) diff --git a/akka-actor/src/main/scala/akka/actor/Deployer.scala b/akka-actor/src/main/scala/akka/actor/Deployer.scala index 3da1a7ca35..cc6db0bbda 100644 --- a/akka-actor/src/main/scala/akka/actor/Deployer.scala +++ b/akka-actor/src/main/scala/akka/actor/Deployer.scala @@ -8,7 +8,6 @@ import akka.util.Duration import com.typesafe.config._ import akka.routing._ import java.util.concurrent.{ TimeUnit, ConcurrentHashMap } -import akka.util.ReflectiveAccess /** * This class represents deployment configuration for a given actor path. It is @@ -86,7 +85,7 @@ case object NoScopeGiven extends Scope { * * @author Jonas Bonér */ -class Deployer(val settings: ActorSystem.Settings, val classloader: ClassLoader) { +class Deployer(val settings: ActorSystem.Settings, val dynamicAccess: DynamicAccess) { import scala.collection.JavaConverters._ @@ -128,7 +127,7 @@ class Deployer(val settings: ActorSystem.Settings, val classloader: ClassLoader) case "broadcast" ⇒ BroadcastRouter(nrOfInstances, routees, resizer) case fqn ⇒ val args = Seq(classOf[Config] -> deployment) - ReflectiveAccess.createInstance[RouterConfig](fqn, args, classloader) match { + dynamicAccess.createInstanceFor[RouterConfig](fqn, args) match { case Right(router) ⇒ router case Left(exception) ⇒ throw new IllegalArgumentException( diff --git a/akka-actor/src/main/scala/akka/actor/DynamicAccess.scala b/akka-actor/src/main/scala/akka/actor/DynamicAccess.scala new file mode 100644 index 0000000000..8d3ac68852 --- /dev/null +++ b/akka-actor/src/main/scala/akka/actor/DynamicAccess.scala @@ -0,0 +1,129 @@ +/** + * Copyright (C) 2009-2012 Typesafe Inc. + */ +package akka.actor + +import akka.util.NonFatal +import java.lang.reflect.InvocationTargetException + +/** + * The DynamicAccess implementation is the class which is used for + * loading all configurable parts of an actor system (the + * [[akka.actor.ReflectiveDynamicAccess]] is the default implementation). + * + * This is an internal facility and users are not expected to encounter it + * unless they are extending Akka in ways which go beyond simple Extensions. + */ +trait DynamicAccess { + + /** + * Convenience method which given a `Class[_]` object and a constructor description + * will create a new instance of that class. + * + * {{{ + * val obj = DynamicAccess.createInstanceFor(clazz, Seq(classOf[Config] -> config, classOf[String] -> name)) + * }}} + */ + def createInstanceFor[T: ClassManifest](clazz: Class[_], args: Seq[(Class[_], AnyRef)]): Either[Throwable, T] = { + val types = args.map(_._1).toArray + val values = args.map(_._2).toArray + withErrorHandling { + val constructor = clazz.getDeclaredConstructor(types: _*) + constructor.setAccessible(true) + val obj = constructor.newInstance(values: _*).asInstanceOf[T] + val t = classManifest[T].erasure + if (t.isInstance(obj)) Right(obj) else Left(new ClassCastException(clazz + " is not a subtype of " + t)) + } + } + + /** + * Obtain a `Class[_]` object loaded with the right class loader (i.e. the one + * returned by `classLoader`). + */ + def getClassFor[T: ClassManifest](fqcn: String): Either[Throwable, Class[_ <: T]] + + /** + * Obtain an object conforming to the type T, which is expected to be + * instantiated from a class designated by the fully-qualified class name + * given, where the constructor is selected and invoked according to the + * `args` argument. The exact usage of args depends on which type is requested, + * see the relevant requesting code for details. + */ + def createInstanceFor[T: ClassManifest](fqcn: String, args: Seq[(Class[_], AnyRef)]): Either[Throwable, T] + + /** + * Obtain the Scala “object” instance for the given fully-qualified class name, if there is one. + */ + def getObjectFor[T: ClassManifest](fqcn: String): Either[Throwable, T] + + /** + * This is the class loader to be used in those special cases where the + * other factory method are not applicable (e.g. when constructing a ClassLoaderBinaryInputStream). + */ + def classLoader: ClassLoader + + /** + * Caught exception is returned as Left(exception). + * Unwraps `InvocationTargetException` if its getTargetException is an `Exception`. + * Other `Throwable`, such as `Error` is thrown. + */ + @inline + final def withErrorHandling[T](body: ⇒ Either[Throwable, T]): Either[Throwable, T] = + try body catch { + case e: InvocationTargetException ⇒ + e.getTargetException match { + case NonFatal(t) ⇒ Left(t) + case t ⇒ throw t + } + case NonFatal(e) ⇒ Left(e) + } + +} + +/** + * This is the default [[akka.actor.DynamicAccess]] implementation used by [[akka.actor.ActorSystemImpl]] + * unless overridden. It uses reflection to turn fully-qualified class names into `Class[_]` objects + * and creates instances from there using `getDeclaredConstructor()` and invoking that. The class loader + * to be used for all this is determined by the [[akka.actor.ActorSystemImpl]]’s `findClassLoader` method + * by default. + */ +class ReflectiveDynamicAccess(val classLoader: ClassLoader) extends DynamicAccess { + + override def getClassFor[T: ClassManifest](fqcn: String): Either[Throwable, Class[_ <: T]] = + try { + val c = classLoader.loadClass(fqcn).asInstanceOf[Class[_ <: T]] + val t = classManifest[T].erasure + if (t.isAssignableFrom(c)) Right(c) else Left(new ClassCastException(t + " is not assignable from " + c)) + } catch { + case NonFatal(e) ⇒ Left(e) + } + + override def createInstanceFor[T: ClassManifest](fqcn: String, args: Seq[(Class[_], AnyRef)]): Either[Throwable, T] = + getClassFor(fqcn).fold(Left(_), { c ⇒ + val types = args.map(_._1).toArray + val values = args.map(_._2).toArray + withErrorHandling { + val constructor = c.getDeclaredConstructor(types: _*) + constructor.setAccessible(true) + val obj = constructor.newInstance(values: _*) + val t = classManifest[T].erasure + if (t.isInstance(obj)) Right(obj) else Left(new ClassCastException(fqcn + " is not a subtype of " + t)) + } + }) + + override def getObjectFor[T: ClassManifest](fqcn: String): Either[Throwable, T] = { + getClassFor(fqcn).fold(Left(_), { c ⇒ + withErrorHandling { + val module = c.getDeclaredField("MODULE$") + module.setAccessible(true) + val t = classManifest[T].erasure + module.get(null) match { + case null ⇒ Left(new NullPointerException) + case x if !t.isInstance(x) ⇒ Left(new ClassCastException(fqcn + " is not a subtype of " + t)) + case x ⇒ Right(x.asInstanceOf[T]) + } + } + }) + } + +} \ No newline at end of file diff --git a/akka-actor/src/main/scala/akka/actor/Extension.scala b/akka-actor/src/main/scala/akka/actor/Extension.scala index 547257e850..3adad6fd4b 100644 --- a/akka-actor/src/main/scala/akka/actor/Extension.scala +++ b/akka-actor/src/main/scala/akka/actor/Extension.scala @@ -3,8 +3,6 @@ */ package akka.actor -import akka.util.ReflectiveAccess - /** * The basic ActorSystem covers all that is needed for locally running actors, * using futures and so on. In addition, more features can hook into it and @@ -73,12 +71,12 @@ trait ExtensionIdProvider { /** * This is a one-stop-shop if all you want is an extension which is - * constructed with the ActorSystemImpl as its only constructor argument: + * constructed with the ExtendedActorSystem as its only constructor argument: * * {{{ * object MyExt extends ExtensionKey[Ext] * - * class Ext(system: ActorSystemImpl) extends MyExt { + * class Ext(system: ExtendedActorSystem) extends MyExt { * ... * } * }}} @@ -89,7 +87,7 @@ trait ExtensionIdProvider { * public class MyExt extends Extension { * static final ExtensionKey key = new ExtensionKey(MyExt.class); * - * public MyExt(ActorSystemImpl system) { + * public MyExt(ExtendedActorSystem system) { * ... * } * }}} @@ -99,7 +97,7 @@ abstract class ExtensionKey[T <: Extension](implicit m: ClassManifest[T]) extend override def lookup(): ExtensionId[T] = this def createExtension(system: ExtendedActorSystem): T = - ReflectiveAccess.createInstance[T](m.erasure, Array[Class[_]](classOf[ActorSystemImpl]), Array[AnyRef](system)) match { + system.dynamicAccess.createInstanceFor[T](m.erasure, Seq(classOf[ExtendedActorSystem] -> system)) match { case Left(ex) ⇒ throw ex case Right(r) ⇒ r } diff --git a/akka-actor/src/main/scala/akka/actor/TypedActor.scala b/akka-actor/src/main/scala/akka/actor/TypedActor.scala index d86dc7dca4..98329203e9 100644 --- a/akka-actor/src/main/scala/akka/actor/TypedActor.scala +++ b/akka-actor/src/main/scala/akka/actor/TypedActor.scala @@ -129,7 +129,8 @@ object TypedActor extends ExtensionId[TypedActorExtension] with ExtensionIdProvi val serializedParameters = Array.ofDim[(Int, Class[_], Array[Byte])](ps.length) for (i ← 0 until ps.length) { val p = ps(i) - val s = SerializationExtension(Serialization.currentSystem.value).findSerializerFor(p) + val system = akka.serialization.JavaSerializer.currentSystem.value + val s = SerializationExtension(system).findSerializerFor(p) val m = if (s.includeManifest) p.getClass else null serializedParameters(i) = (s.identifier, m, s toBinary parameters(i)) //Mutable for the sake of sanity } @@ -146,7 +147,7 @@ object TypedActor extends ExtensionId[TypedActorExtension] with ExtensionIdProvi //TODO implement writeObject and readObject to serialize //TODO Possible optimization is to special encode the parameter-types to conserve space private def readResolve(): AnyRef = { - val system = akka.serialization.Serialization.currentSystem.value + val system = akka.serialization.JavaSerializer.currentSystem.value if (system eq null) throw new IllegalStateException( "Trying to deserialize a SerializedMethodCall without an ActorSystem in scope." + " Use akka.serialization.Serialization.currentSystem.withValue(system) { ... }") @@ -158,7 +159,8 @@ object TypedActor extends ExtensionId[TypedActorExtension] with ExtensionIdProvi val deserializedParameters: Array[AnyRef] = Array.ofDim[AnyRef](a.length) //Mutable for the sake of sanity for (i ← 0 until a.length) { val (sId, manifest, bytes) = a(i) - deserializedParameters(i) = serialization.serializerByIdentity(sId).fromBinary(bytes, Option(manifest)) + deserializedParameters(i) = + serialization.serializerByIdentity(sId).fromBinary(bytes, Option(manifest)) } deserializedParameters diff --git a/akka-actor/src/main/scala/akka/dispatch/AbstractDispatcher.scala b/akka-actor/src/main/scala/akka/dispatch/AbstractDispatcher.scala index 8ac5aba564..1b31be630c 100644 --- a/akka-actor/src/main/scala/akka/dispatch/AbstractDispatcher.scala +++ b/akka-actor/src/main/scala/akka/dispatch/AbstractDispatcher.scala @@ -12,7 +12,6 @@ import akka.actor.ActorSystem import scala.annotation.tailrec import akka.event.EventStream import com.typesafe.config.Config -import akka.util.ReflectiveAccess import akka.serialization.SerializationExtension import akka.util.NonFatal import akka.event.Logging.LogEventException @@ -26,7 +25,7 @@ final case class Envelope(val message: Any, val sender: ActorRef)(system: ActorS val ser = SerializationExtension(system) ser.serialize(msg) match { //Verify serializability case Left(t) ⇒ throw t - case Right(bytes) ⇒ ser.deserialize(bytes, msg.getClass, None) match { //Verify deserializability + case Right(bytes) ⇒ ser.deserialize(bytes, msg.getClass) match { //Verify deserializability case Left(t) ⇒ throw t case _ ⇒ //All good } @@ -354,12 +353,12 @@ abstract class MessageDispatcherConfigurator(val config: Config, val prerequisit /** * Returns a factory for the [[akka.dispatch.Mailbox]] given the configuration. * Default implementation instantiate the [[akka.dispatch.MailboxType]] specified - * as FQCN in mailboxType config property. If mailboxType is unspecified (empty) + * as FQCN in mailbox-type config property. If mailbox-type is unspecified (empty) * then [[akka.dispatch.UnboundedMailbox]] is used when capacity is < 1, * otherwise [[akka.dispatch.BoundedMailbox]]. */ def mailboxType(): MailboxType = { - config.getString("mailboxType") match { + config.getString("mailbox-type") match { case "" ⇒ val capacity = config.getInt("mailbox-capacity") if (capacity < 1) UnboundedMailbox() @@ -369,7 +368,7 @@ abstract class MessageDispatcherConfigurator(val config: Config, val prerequisit } case fqcn ⇒ val args = Seq(classOf[Config] -> config) - ReflectiveAccess.createInstance[MailboxType](fqcn, args, prerequisites.classloader) match { + prerequisites.dynamicAccess.createInstanceFor[MailboxType](fqcn, args) match { case Right(instance) ⇒ instance case Left(exception) ⇒ throw new IllegalArgumentException( @@ -385,8 +384,10 @@ abstract class MessageDispatcherConfigurator(val config: Config, val prerequisit case null | "" | "fork-join-executor" ⇒ new ForkJoinExecutorConfigurator(config.getConfig("fork-join-executor"), prerequisites) case "thread-pool-executor" ⇒ new ThreadPoolExecutorConfigurator(config.getConfig("thread-pool-executor"), prerequisites) case fqcn ⇒ - val constructorSignature = Array[Class[_]](classOf[Config], classOf[DispatcherPrerequisites]) - ReflectiveAccess.createInstance[ExecutorServiceConfigurator](fqcn, constructorSignature, Array[AnyRef](config, prerequisites), prerequisites.classloader) match { + val args = Seq( + classOf[Config] -> config, + classOf[DispatcherPrerequisites] -> prerequisites) + prerequisites.dynamicAccess.createInstanceFor[ExecutorServiceConfigurator](fqcn, args) match { case Right(instance) ⇒ instance case Left(exception) ⇒ throw new IllegalArgumentException( ("""Cannot instantiate ExecutorServiceConfigurator ("executor = [%s]"), defined in [%s], diff --git a/akka-actor/src/main/scala/akka/dispatch/Dispatchers.scala b/akka-actor/src/main/scala/akka/dispatch/Dispatchers.scala index d71604fd1a..8e99e05b06 100644 --- a/akka-actor/src/main/scala/akka/dispatch/Dispatchers.scala +++ b/akka-actor/src/main/scala/akka/dispatch/Dispatchers.scala @@ -4,22 +4,24 @@ package akka.dispatch -import akka.actor.newUuid -import akka.util.{ Duration, ReflectiveAccess } -import akka.actor.ActorSystem -import akka.event.EventStream -import akka.actor.Scheduler -import com.typesafe.config.Config -import com.typesafe.config.ConfigFactory +import java.util.concurrent.{ ConcurrentHashMap, TimeUnit, ThreadFactory } + +import scala.collection.JavaConverters.mapAsJavaMapConverter + +import com.typesafe.config.{ ConfigFactory, Config } + +import Dispatchers.DefaultDispatcherId +import akka.actor.{ Scheduler, DynamicAccess, ActorSystem } import akka.event.Logging.Warning -import java.util.concurrent.{ ThreadFactory, TimeUnit, ConcurrentHashMap } +import akka.event.EventStream +import akka.util.Duration trait DispatcherPrerequisites { def threadFactory: ThreadFactory def eventStream: EventStream def deadLetterMailbox: Mailbox def scheduler: Scheduler - def classloader: ClassLoader + def dynamicAccess: DynamicAccess } case class DefaultDispatcherPrerequisites( @@ -27,7 +29,7 @@ case class DefaultDispatcherPrerequisites( val eventStream: EventStream, val deadLetterMailbox: Mailbox, val scheduler: Scheduler, - val classloader: ClassLoader) extends DispatcherPrerequisites + val dynamicAccess: DynamicAccess) extends DispatcherPrerequisites object Dispatchers { /** @@ -137,7 +139,7 @@ class Dispatchers(val settings: ActorSystem.Settings, val prerequisites: Dispatc case "PinnedDispatcher" ⇒ new PinnedDispatcherConfigurator(cfg, prerequisites) case fqn ⇒ val args = Seq(classOf[Config] -> cfg, classOf[DispatcherPrerequisites] -> prerequisites) - ReflectiveAccess.createInstance[MessageDispatcherConfigurator](fqn, args, prerequisites.classloader) match { + prerequisites.dynamicAccess.createInstanceFor[MessageDispatcherConfigurator](fqn, args) match { case Right(configurator) ⇒ configurator case Left(exception) ⇒ throw new IllegalArgumentException( diff --git a/akka-actor/src/main/scala/akka/dispatch/Future.scala b/akka-actor/src/main/scala/akka/dispatch/Future.scala index 97ff17c075..d6f4751d6e 100644 --- a/akka-actor/src/main/scala/akka/dispatch/Future.scala +++ b/akka-actor/src/main/scala/akka/dispatch/Future.scala @@ -20,6 +20,7 @@ import akka.event.Logging.Debug import java.util.concurrent.TimeUnit.NANOSECONDS import java.util.concurrent.{ ExecutionException, Callable, TimeoutException } import java.util.concurrent.atomic.{ AtomicInteger, AtomicReferenceFieldUpdater } +import akka.pattern.AskTimeoutException object Await { @@ -151,6 +152,26 @@ object Futures { for (r ← fr; b ← fb) yield { r add b; r } } } + + /** + * Signals that the current thread of execution will potentially engage + * in blocking calls after the call to this method, giving the system a + * chance to spawn new threads, reuse old threads or otherwise, to prevent + * starvation and/or unfairness. + * + * Assures that any Future tasks initiated in the current thread will be + * executed asynchronously, including any tasks currently queued to be + * executed in the current thread. This is needed if the current task may + * block, causing delays in executing the remaining tasks which in some + * cases may cause a deadlock. + * + * Usage: Call this method in a callback (map, flatMap etc also count) to a Future, + * if you will be doing blocking in the callback. + * + * Note: Calling 'Await.result(future)' or 'Await.ready(future)' will automatically trigger this method. + * + */ + def blocking(): Unit = Future.blocking() } object Future { @@ -317,17 +338,22 @@ object Future { * } * */ - def blocking(implicit executor: ExecutionContext): Unit = + def blocking(): Unit = _taskStack.get match { case stack if (stack ne null) && stack.nonEmpty ⇒ + val executionContext = _executionContext.get match { + case null ⇒ throw new IllegalStateException("'blocking' needs to be invoked inside a Future callback.") + case some ⇒ some + } val tasks = stack.elems stack.clear() _taskStack.remove() - dispatchTask(() ⇒ _taskStack.get.elems = tasks, true) + dispatchTask(() ⇒ _taskStack.get.elems = tasks, true)(executionContext) case _ ⇒ _taskStack.remove() } private val _taskStack = new ThreadLocal[Stack[() ⇒ Unit]]() + private val _executionContext = new ThreadLocal[ExecutionContext]() /** * Internal API, do not call @@ -339,7 +365,7 @@ object Future { new Runnable { def run = try { - + _executionContext set executor val taskStack = Stack.empty[() ⇒ Unit] taskStack push task _taskStack set taskStack @@ -352,7 +378,10 @@ object Future { case NonFatal(e) ⇒ executor.reportFailure(e) } } - } finally { _taskStack.remove() } + } finally { + _executionContext.remove() + _taskStack.remove() + } }) } @@ -795,8 +824,9 @@ class DefaultPromise[T](implicit val executor: ExecutionContext) extends Abstrac def result(atMost: Duration)(implicit permit: CanAwait): T = ready(atMost).value.get match { - case Left(e) ⇒ throw e - case Right(r) ⇒ r + case Left(e: AskTimeoutException) ⇒ throw new AskTimeoutException(e.getMessage, e) // to get meaningful stack trace + case Left(e) ⇒ throw e + case Right(r) ⇒ r } def value: Option[Either[Throwable, T]] = getState match { diff --git a/akka-actor/src/main/scala/akka/dispatch/Mailbox.scala b/akka-actor/src/main/scala/akka/dispatch/Mailbox.scala index 58a498b948..d2acdacdcd 100644 --- a/akka-actor/src/main/scala/akka/dispatch/Mailbox.scala +++ b/akka-actor/src/main/scala/akka/dispatch/Mailbox.scala @@ -190,7 +190,7 @@ private[akka] abstract class Mailbox(val actor: ActorCell) extends MessageQueue final def processAllSystemMessages() { var nextMessage = systemDrain() try { - while (nextMessage ne null) { + while ((nextMessage ne null) && !isClosed) { if (debug) println(actor.self + " processing system message " + nextMessage + " with children " + actor.childrenRefs) actor systemInvoke nextMessage nextMessage = nextMessage.next diff --git a/akka-actor/src/main/scala/akka/event/Logging.scala b/akka-actor/src/main/scala/akka/event/Logging.scala index a6622b7875..a58ea3fdb9 100644 --- a/akka-actor/src/main/scala/akka/event/Logging.scala +++ b/akka-actor/src/main/scala/akka/event/Logging.scala @@ -6,7 +6,6 @@ package akka.event import akka.actor._ import akka.AkkaException import akka.actor.ActorSystem.Settings -import akka.util.ReflectiveAccess import akka.config.ConfigurationException import akka.util.ReentrantGuard import akka.util.duration._ @@ -101,7 +100,7 @@ trait LoggingBus extends ActorEventBus { if loggerName != StandardOutLoggerName } yield { try { - ReflectiveAccess.getClassFor[Actor](loggerName, system.internalClassLoader) match { + system.dynamicAccess.getClassFor[Actor](loggerName) match { case Right(actorClass) ⇒ addLogger(system, actorClass, level, logName) case Left(exception) ⇒ throw exception } @@ -350,7 +349,7 @@ object Logging { object Extension extends ExtensionKey[LogExt] - class LogExt(system: ActorSystemImpl) extends Extension { + class LogExt(system: ExtendedActorSystem) extends Extension { private val loggerId = new AtomicInteger def id() = loggerId.incrementAndGet() } diff --git a/akka-actor/src/main/scala/akka/routing/Routing.scala b/akka-actor/src/main/scala/akka/routing/Routing.scala index c200be0871..da2c81d1a7 100644 --- a/akka-actor/src/main/scala/akka/routing/Routing.scala +++ b/akka-actor/src/main/scala/akka/routing/Routing.scala @@ -9,15 +9,14 @@ import akka.util.duration._ import akka.config.ConfigurationException import akka.pattern.pipe import akka.pattern.AskSupport - import com.typesafe.config.Config - import scala.collection.JavaConversions.iterableAsScalaIterable - import java.util.concurrent.atomic.{ AtomicLong, AtomicBoolean } import java.util.concurrent.TimeUnit - +import java.util.concurrent.locks.ReentrantLock import akka.jsr166y.ThreadLocalRandom +import akka.util.Unsafe +import akka.dispatch.Dispatchers /** * A RoutedActorRef is an ActorRef that has a set of connected ActorRef and it uses a Router to @@ -26,25 +25,88 @@ import akka.jsr166y.ThreadLocalRandom private[akka] class RoutedActorRef(_system: ActorSystemImpl, _props: Props, _supervisor: InternalActorRef, _path: ActorPath) extends LocalActorRef( _system, - _props.copy(creator = () ⇒ _props.routerConfig.createActor()), + _props.copy(creator = () ⇒ _props.routerConfig.createActor(), dispatcher = _props.routerConfig.routerDispatcher), _supervisor, _path) { - private val routeeProps = _props.copy(routerConfig = NoRouter) - private val resizeProgress = new AtomicBoolean + /* + * CAUTION: RoutedActorRef is PROBLEMATIC + * ====================================== + * + * We are constructing/assembling the children outside of the scope of the + * Router actor, inserting them in its childrenRef list, which is not at all + * synchronized. This is done exactly once at start-up, all other accesses + * are done from the Router actor. This means that the only thing which is + * really hairy is making sure that the Router does not touch its childrenRefs + * before we are done with them: lock the monitor of the actor cell (hence the + * override of newActorCell) and use that to block the Router constructor for + * as long as it takes to setup the RoutedActorRef itself. + */ + override def newActorCell( + system: ActorSystemImpl, + ref: InternalActorRef, + props: Props, + supervisor: InternalActorRef, + receiveTimeout: Option[Duration]): ActorCell = + { + val cell = super.newActorCell(system, ref, props, supervisor, receiveTimeout) + Unsafe.instance.monitorEnter(cell) + cell + } + + private[akka] val routerConfig = _props.routerConfig + private[akka] val routeeProps = _props.copy(routerConfig = NoRouter) + private[akka] val resizeInProgress = new AtomicBoolean private val resizeCounter = new AtomicLong @volatile private var _routees: IndexedSeq[ActorRef] = IndexedSeq.empty[ActorRef] // this MUST be initialized during createRoute def routees = _routees + @volatile + private var _routeeProvider: RouteeProvider = _ + def routeeProvider = _routeeProvider + + val route = + try { + _routeeProvider = routerConfig.createRouteeProvider(actorContext) + val r = routerConfig.createRoute(routeeProps, routeeProvider) + // initial resize, before message send + routerConfig.resizer foreach { r ⇒ + if (r.isTimeForResize(resizeCounter.getAndIncrement())) + r.resize(routeeProps, routeeProvider) + } + r + } finally { + assert(Thread.holdsLock(actorContext)) + Unsafe.instance.monitorExit(actorContext) // unblock Router’s constructor + } + + if (routerConfig.resizer.isEmpty && _routees.isEmpty) + throw new ActorInitializationException("router " + routerConfig + " did not register routees!") + + /* + * end of construction + */ + + def applyRoute(sender: ActorRef, message: Any): Iterable[Destination] = message match { + case _: AutoReceivedMessage ⇒ Destination(this, this) :: Nil + case Terminated(_) ⇒ Destination(this, this) :: Nil + case CurrentRoutees ⇒ + sender ! RouterRoutees(_routees) + Nil + case _ ⇒ + if (route.isDefinedAt(sender, message)) route(sender, message) + else Nil + } + /** * Adds the routees to existing routees. * Adds death watch of the routees so that they are removed when terminated. * Not thread safe, but intended to be called from protected points, such as * `RouterConfig.createRoute` and `Resizer.resize` */ - private[akka] def addRoutees(newRoutees: IndexedSeq[ActorRef]) { + private[akka] def addRoutees(newRoutees: IndexedSeq[ActorRef]): Unit = { _routees = _routees ++ newRoutees // subscribe to Terminated messages for all route destinations, to be handled by Router actor newRoutees foreach underlying.watch @@ -56,34 +118,11 @@ private[akka] class RoutedActorRef(_system: ActorSystemImpl, _props: Props, _sup * Not thread safe, but intended to be called from protected points, such as * `Resizer.resize` */ - private[akka] def removeRoutees(abandonedRoutees: IndexedSeq[ActorRef]) { + private[akka] def removeRoutees(abandonedRoutees: IndexedSeq[ActorRef]): Unit = { _routees = _routees diff abandonedRoutees abandonedRoutees foreach underlying.unwatch } - private val routeeProvider = _props.routerConfig.createRouteeProvider(actorContext) - val route = _props.routerConfig.createRoute(routeeProps, routeeProvider) - // initial resize, before message send - resize() - - def applyRoute(sender: ActorRef, message: Any): Iterable[Destination] = message match { - case _: AutoReceivedMessage ⇒ Nil - case Terminated(_) ⇒ Nil - case CurrentRoutees ⇒ - sender ! RouterRoutees(_routees) - Nil - case _ ⇒ - if (route.isDefinedAt(sender, message)) route(sender, message) - else Nil - } - - if (_props.routerConfig.resizer.isEmpty && _routees.isEmpty) - throw new ActorInitializationException("router " + _props.routerConfig + " did not register routees!") - - _routees match { - case x ⇒ _routees = x // volatile write to publish the route before sending messages - } - override def !(message: Any)(implicit sender: ActorRef = null): Unit = { resize() @@ -95,20 +134,15 @@ private[akka] class RoutedActorRef(_system: ActorSystemImpl, _props: Props, _sup } applyRoute(s, message) match { - case Nil ⇒ super.!(message)(s) - case refs ⇒ refs foreach (p ⇒ p.recipient.!(msg)(p.sender)) + case Destination(_, x) :: Nil if x eq this ⇒ super.!(message)(s) + case refs ⇒ refs foreach (p ⇒ p.recipient.!(msg)(p.sender)) } } - def resize() { - for (r ← _props.routerConfig.resizer) { - if (r.isTimeForResize(resizeCounter.getAndIncrement()) && resizeProgress.compareAndSet(false, true)) { - try { - r.resize(routeeProps, routeeProvider) - } finally { - resizeProgress.set(false) - } - } + def resize(): Unit = { + for (r ← routerConfig.resizer) { + if (r.isTimeForResize(resizeCounter.getAndIncrement()) && resizeInProgress.compareAndSet(false, true)) + super.!(Router.Resize) } } } @@ -139,6 +173,11 @@ trait RouterConfig { def createActor(): Router = new Router {} + /** + * Dispatcher ID to use for running the “head” actor, i.e. the [[akka.routing.Router]]. + */ + def routerDispatcher: String + /** * Overridable merge strategy, by default completely prefers “this” (i.e. no merge). */ @@ -246,13 +285,20 @@ trait CustomRoute { */ trait Router extends Actor { - val ref = self match { - case x: RoutedActorRef ⇒ x - case _ ⇒ throw new ActorInitializationException("Router actor can only be used in RoutedActorRef") + // make sure that we synchronize properly to get the childrenRefs into our CPU cache + val ref = context.synchronized { + self match { + case x: RoutedActorRef ⇒ x + case _ ⇒ throw new ActorInitializationException("Router actor can only be used in RoutedActorRef") + } } final def receive = ({ + case Router.Resize ⇒ + try ref.routerConfig.resizer foreach (_.resize(ref.routeeProps, ref.routeeProvider)) + finally assert(ref.resizeInProgress.getAndSet(false)) + case Terminated(child) ⇒ ref.removeRoutees(IndexedSeq(child)) if (ref.routees.isEmpty) context.stop(self) @@ -264,6 +310,10 @@ trait Router extends Actor { } } +private object Router { + case object Resize +} + /** * Used to broadcast a message to all connections in a router; only the * contained message will be forwarded, i.e. the `Broadcast(...)` @@ -302,6 +352,7 @@ case class Destination(sender: ActorRef, recipient: ActorRef) //TODO add @SerialVersionUID(1L) when SI-4804 is fixed case object NoRouter extends RouterConfig { def createRoute(props: Props, routeeProvider: RouteeProvider): Route = null + def routerDispatcher: String = "" override def withFallback(other: RouterConfig): RouterConfig = other } @@ -311,13 +362,20 @@ case object NoRouter extends RouterConfig { case object FromConfig extends RouterConfig { def createRoute(props: Props, routeeProvider: RouteeProvider): Route = throw new ConfigurationException("router " + routeeProvider.context.self + " needs external configuration from file (e.g. application.conf)") + def routerDispatcher: String = Dispatchers.DefaultDispatcherId } /** * Java API: Router configuration which has no default, i.e. external configuration is required. + * + * This can be used when the dispatcher to be used for the head Router needs to be configured + * (defaults to default-dispatcher). */ //TODO add @SerialVersionUID(1L) when SI-4804 is fixed -case class FromConfig() extends RouterConfig { +case class FromConfig(val routerDispatcher: String = Dispatchers.DefaultDispatcherId) extends RouterConfig { + + def this() = this(Dispatchers.DefaultDispatcherId) + def createRoute(props: Props, routeeProvider: RouteeProvider): Route = throw new ConfigurationException("router " + routeeProvider.context.self + " needs external configuration from file (e.g. application.conf)") } @@ -348,7 +406,8 @@ object RoundRobinRouter { * using `actorFor` in [[akka.actor.ActorRefProvider]] */ //TODO add @SerialVersionUID(1L) when SI-4804 is fixed -case class RoundRobinRouter(nrOfInstances: Int = 0, routees: Iterable[String] = Nil, override val resizer: Option[Resizer] = None) +case class RoundRobinRouter(nrOfInstances: Int = 0, routees: Iterable[String] = Nil, override val resizer: Option[Resizer] = None, + val routerDispatcher: String = Dispatchers.DefaultDispatcherId) extends RouterConfig with RoundRobinLike { /** @@ -374,6 +433,11 @@ case class RoundRobinRouter(nrOfInstances: Int = 0, routees: Iterable[String] = * Java API */ def this(resizer: Resizer) = this(resizer = Some(resizer)) + + /** + * Java API for setting routerDispatcher + */ + def withDispatcher(dispatcherId: String) = copy(routerDispatcher = dispatcherId) } trait RoundRobinLike { this: RouterConfig ⇒ @@ -428,7 +492,8 @@ object RandomRouter { * using `actorFor` in [[akka.actor.ActorRefProvider]] */ //TODO add @SerialVersionUID(1L) when SI-4804 is fixed -case class RandomRouter(nrOfInstances: Int = 0, routees: Iterable[String] = Nil, override val resizer: Option[Resizer] = None) +case class RandomRouter(nrOfInstances: Int = 0, routees: Iterable[String] = Nil, override val resizer: Option[Resizer] = None, + val routerDispatcher: String = Dispatchers.DefaultDispatcherId) extends RouterConfig with RandomLike { /** @@ -454,6 +519,11 @@ case class RandomRouter(nrOfInstances: Int = 0, routees: Iterable[String] = Nil, * Java API */ def this(resizer: Resizer) = this(resizer = Some(resizer)) + + /** + * Java API for setting routerDispatcher + */ + def withDispatcher(dispatcherId: String) = copy(routerDispatcher = dispatcherId) } trait RandomLike { this: RouterConfig ⇒ @@ -514,7 +584,8 @@ object SmallestMailboxRouter { * using `actorFor` in [[akka.actor.ActorRefProvider]] */ //TODO add @SerialVersionUID(1L) when SI-4804 is fixed -case class SmallestMailboxRouter(nrOfInstances: Int = 0, routees: Iterable[String] = Nil, override val resizer: Option[Resizer] = None) +case class SmallestMailboxRouter(nrOfInstances: Int = 0, routees: Iterable[String] = Nil, override val resizer: Option[Resizer] = None, + val routerDispatcher: String = Dispatchers.DefaultDispatcherId) extends RouterConfig with SmallestMailboxLike { /** @@ -540,6 +611,11 @@ case class SmallestMailboxRouter(nrOfInstances: Int = 0, routees: Iterable[Strin * Java API */ def this(resizer: Resizer) = this(resizer = Some(resizer)) + + /** + * Java API for setting routerDispatcher + */ + def withDispatcher(dispatcherId: String) = copy(routerDispatcher = dispatcherId) } trait SmallestMailboxLike { this: RouterConfig ⇒ @@ -659,7 +735,8 @@ object BroadcastRouter { * using `actorFor` in [[akka.actor.ActorRefProvider]] */ //TODO add @SerialVersionUID(1L) when SI-4804 is fixed -case class BroadcastRouter(nrOfInstances: Int = 0, routees: Iterable[String] = Nil, override val resizer: Option[Resizer] = None) +case class BroadcastRouter(nrOfInstances: Int = 0, routees: Iterable[String] = Nil, override val resizer: Option[Resizer] = None, + val routerDispatcher: String = Dispatchers.DefaultDispatcherId) extends RouterConfig with BroadcastLike { /** @@ -686,6 +763,10 @@ case class BroadcastRouter(nrOfInstances: Int = 0, routees: Iterable[String] = N */ def this(resizer: Resizer) = this(resizer = Some(resizer)) + /** + * Java API for setting routerDispatcher + */ + def withDispatcher(dispatcherId: String) = copy(routerDispatcher = dispatcherId) } trait BroadcastLike { this: RouterConfig ⇒ @@ -732,7 +813,8 @@ object ScatterGatherFirstCompletedRouter { */ //TODO add @SerialVersionUID(1L) when SI-4804 is fixed case class ScatterGatherFirstCompletedRouter(nrOfInstances: Int = 0, routees: Iterable[String] = Nil, within: Duration, - override val resizer: Option[Resizer] = None) + override val resizer: Option[Resizer] = None, + val routerDispatcher: String = Dispatchers.DefaultDispatcherId) extends RouterConfig with ScatterGatherFirstCompletedLike { if (within <= Duration.Zero) throw new IllegalArgumentException( @@ -761,6 +843,11 @@ case class ScatterGatherFirstCompletedRouter(nrOfInstances: Int = 0, routees: It * Java API */ def this(resizer: Resizer, w: Duration) = this(resizer = Some(resizer), within = w) + + /** + * Java API for setting routerDispatcher + */ + def withDispatcher(dispatcherId: String) = copy(routerDispatcher = dispatcherId) } trait ScatterGatherFirstCompletedLike { this: RouterConfig ⇒ @@ -795,14 +882,22 @@ trait Resizer { * for the initial resize and continues with 1 for the first message. Make sure to perform * initial resize before first message (messageCounter == 0), because there is no guarantee * that resize will be done when concurrent messages are in play. + * + * CAUTION: this method is invoked from the thread which tries to send a + * message to the pool, i.e. the ActorRef.!() method, hence it may be called + * concurrently. */ def isTimeForResize(messageCounter: Long): Boolean + /** * Decide if the capacity of the router need to be changed. Will be invoked when `isTimeForResize` * returns true and no other resize is in progress. * Create and register more routees with `routeeProvider.registerRoutees(newRoutees) * or remove routees with `routeeProvider.unregisterRoutees(abandonedRoutees)` and * sending [[akka.actor.PoisonPill]] to them. + * + * This method is invoked only in the context of the Router actor in order to safely + * create/stop children. */ def resize(props: Props, routeeProvider: RouteeProvider) } diff --git a/akka-actor/src/main/scala/akka/serialization/Format.scala b/akka-actor/src/main/scala/akka/serialization/Format.scala deleted file mode 100644 index 57ae659cec..0000000000 --- a/akka-actor/src/main/scala/akka/serialization/Format.scala +++ /dev/null @@ -1,103 +0,0 @@ -package akka.serialization - -/** - * Copyright (C) 2009-2012 Typesafe Inc. - */ - -import akka.actor.Actor - -/** - * trait Serializer { - * @volatile - * var classLoader: Option[ClassLoader] = None - * def deepClone(obj: AnyRef): AnyRef = fromBinary(toBinary(obj), Some(obj.getClass)) - * - * def toBinary(obj: AnyRef): Array[Byte] - * def fromBinary(bytes: Array[Byte], clazz: Option[Class[_]]): AnyRef - * } - */ - -/** - * - * object Format { - * implicit object Default extends Serializer { - * import java.io.{ ObjectOutputStream, ByteArrayOutputStream, ObjectInputStream, ByteArrayInputStream } - * //import org.apache.commons.io.input.ClassLoaderObjectInputStream - * - * def toBinary(obj: AnyRef): Array[Byte] = { - * val bos = new ByteArrayOutputStream - * val out = new ObjectOutputStream(bos) - * out.writeObject(obj) - * out.close() - * bos.toByteArray - * } - * - * def fromBinary(bytes: Array[Byte], clazz: Option[Class[_]], classLoader: Option[ClassLoader] = None): AnyRef = { - * val in = - * //if (classLoader.isDefined) new ClassLoaderObjectInputStream(classLoader.get, new ByteArrayInputStream(bytes)) else - * new ObjectInputStream(new ByteArrayInputStream(bytes)) - * val obj = in.readObject - * in.close() - * obj - * } - * - * def identifier: Byte = 111 //Pick a number and hope no one has chosen the same :-) 0 - 16 is reserved for Akka internals - * - * } - * - * val defaultSerializerName = Default.getClass.getName - * } - */ - -trait FromBinary[T <: Actor] { - def fromBinary(bytes: Array[Byte], act: T): T -} - -trait ToBinary[T <: Actor] { - def toBinary(t: T): Array[Byte] -} - -/** - * Type class definition for Actor Serialization. - * Client needs to implement Format[] for the respective actor. - */ -trait Format[T <: Actor] extends FromBinary[T] with ToBinary[T] - -/** - * A default implementation for a stateless actor - * - * Create a Format object with the client actor as the implementation of the type class - * - *
- * object BinaryFormatMyStatelessActor  {
- *   implicit object MyStatelessActorFormat extends StatelessActorFormat[MyStatelessActor]
- * }
- * 
- */ -trait StatelessActorFormat[T <: Actor] extends Format[T] { - def fromBinary(bytes: Array[Byte], act: T) = act - - def toBinary(ac: T) = Array.empty[Byte] -} - -/** - * A default implementation of the type class for a Format that specifies a serializer - * - * Create a Format object with the client actor as the implementation of the type class and - * a serializer object - * - *
- * object BinaryFormatMyJavaSerializableActor  {
- *   implicit object MyJavaSerializableActorFormat extends SerializerBasedActorFormat[MyJavaSerializableActor]  {
- *     val serializer = Serializers.Java
- *   }
- * }
- * 
- */ -trait SerializerBasedActorFormat[T <: Actor] extends Format[T] { - val serializer: Serializer - - def fromBinary(bytes: Array[Byte], act: T) = serializer.fromBinary(bytes, Some(act.getClass)).asInstanceOf[T] - - def toBinary(ac: T) = serializer.toBinary(ac) -} diff --git a/akka-actor/src/main/scala/akka/serialization/Serialization.scala b/akka-actor/src/main/scala/akka/serialization/Serialization.scala index fbcd5e9f9e..ce0f56a238 100644 --- a/akka-actor/src/main/scala/akka/serialization/Serialization.scala +++ b/akka-actor/src/main/scala/akka/serialization/Serialization.scala @@ -5,12 +5,12 @@ package akka.serialization import akka.AkkaException -import akka.util.ReflectiveAccess import scala.util.DynamicVariable import com.typesafe.config.Config -import akka.actor.{ Extension, ActorSystem, ExtendedActorSystem, Address } -import java.util.concurrent.ConcurrentHashMap +import akka.actor.{ Extension, ExtendedActorSystem, Address, DynamicAccess } import akka.event.Logging +import java.util.concurrent.ConcurrentHashMap +import akka.util.NonFatal import scala.collection.mutable.ArrayBuffer import java.io.NotSerializableException @@ -23,19 +23,6 @@ object Serialization { */ type ClassSerializer = (Class[_], Serializer) - /** - * This holds a reference to the current ActorSystem (the surrounding context) - * during serialization and deserialization. - * - * If you are using Serializers yourself, outside of SerializationExtension, - * you'll need to surround the serialization/deserialization with: - * - * currentSystem.withValue(system) { - * ...code... - * } - */ - val currentSystem = new DynamicVariable[ActorSystem](null) - /** * This holds a reference to the current transport address to be inserted * into local actor refs during serialization. @@ -71,8 +58,9 @@ class Serialization(val system: ExtendedActorSystem) extends Extension { * Serializes the given AnyRef/java.lang.Object according to the Serialization configuration * to either an Array of Bytes or an Exception if one was thrown. */ - def serialize(o: AnyRef): Either[Exception, Array[Byte]] = - try { Right(findSerializerFor(o).toBinary(o)) } catch { case e: Exception ⇒ Left(e) } + def serialize(o: AnyRef): Either[Throwable, Array[Byte]] = + try Right(findSerializerFor(o).toBinary(o)) + catch { case NonFatal(e) ⇒ Left(e) } /** * Deserializes the given array of bytes using the specified serializer id, @@ -81,26 +69,18 @@ class Serialization(val system: ExtendedActorSystem) extends Extension { */ def deserialize(bytes: Array[Byte], serializerId: Int, - clazz: Option[Class[_]], - classLoader: ClassLoader): Either[Exception, AnyRef] = - try { - currentSystem.withValue(system) { - Right(serializerByIdentity(serializerId).fromBinary(bytes, clazz, Some(classLoader))) - } - } catch { case e: Exception ⇒ Left(e) } + clazz: Option[Class[_]]): Either[Throwable, AnyRef] = + try Right(serializerByIdentity(serializerId).fromBinary(bytes, clazz)) + catch { case NonFatal(e) ⇒ Left(e) } /** * Deserializes the given array of bytes using the specified type to look up what Serializer should be used. * You can specify an optional ClassLoader to load the object into. * Returns either the resulting object or an Exception if one was thrown. */ - def deserialize( - bytes: Array[Byte], - clazz: Class[_], - classLoader: Option[ClassLoader]): Either[Exception, AnyRef] = - try { - currentSystem.withValue(system) { Right(serializerFor(clazz).fromBinary(bytes, Some(clazz), classLoader)) } - } catch { case e: Exception ⇒ Left(e) } + def deserialize(bytes: Array[Byte], clazz: Class[_]): Either[Throwable, AnyRef] = + try Right(serializerFor(clazz).fromBinary(bytes, Some(clazz))) + catch { case NonFatal(e) ⇒ Left(e) } /** * Returns the Serializer configured for the given object, returns the NullSerializer if it's null. @@ -149,10 +129,12 @@ class Serialization(val system: ExtendedActorSystem) extends Extension { } /** - * Tries to instantiate the specified Serializer by the FQN + * Tries to load the specified Serializer by the fully-qualified name; the actual + * loading is performed by the system’s [[akka.actor.DynamicAccess]]. */ - def serializerOf(serializerFQN: String): Either[Exception, Serializer] = - ReflectiveAccess.createInstance(serializerFQN, ReflectiveAccess.noParams, ReflectiveAccess.noArgs, system.internalClassLoader) + def serializerOf(serializerFQN: String): Either[Throwable, Serializer] = + system.dynamicAccess.createInstanceFor[Serializer](serializerFQN, Seq(classOf[ExtendedActorSystem] -> system)).fold(_ ⇒ + system.dynamicAccess.createInstanceFor[Serializer](serializerFQN, Seq()), Right(_)) /** * A Map of serializer from alias to implementation (class implementing akka.serialization.Serializer) @@ -169,7 +151,7 @@ class Serialization(val system: ExtendedActorSystem) extends Extension { */ private[akka] val bindings: Seq[ClassSerializer] = { val configuredBindings = for ((k: String, v: String) ← settings.SerializationBindings if v != "none") yield { - val c = ReflectiveAccess.getClassFor(k, system.internalClassLoader).fold(throw _, identity[Class[_]]) + val c = system.dynamicAccess.getClassFor(k).fold(throw _, identity[Class[_]]) (c, serializers(v)) } sort(configuredBindings) diff --git a/akka-actor/src/main/scala/akka/serialization/Serializer.scala b/akka-actor/src/main/scala/akka/serialization/Serializer.scala index 5a9ba97a98..2721432460 100644 --- a/akka-actor/src/main/scala/akka/serialization/Serializer.scala +++ b/akka-actor/src/main/scala/akka/serialization/Serializer.scala @@ -6,11 +6,31 @@ package akka.serialization import java.io.{ ObjectOutputStream, ByteArrayOutputStream, ObjectInputStream, ByteArrayInputStream } import akka.util.ClassLoaderObjectInputStream +import akka.actor.DynamicAccess +import akka.actor.ExtendedActorSystem +import scala.util.DynamicVariable /** - * A Serializer represents a bimap between an object and an array of bytes representing that object + * A Serializer represents a bimap between an object and an array of bytes representing that object. + * + * Serializers are loaded using reflection during [[akka.actor.ActorSystem]] + * start-up, where two constructors are tried in order: + * + *
    + *
  • taking exactly one argument of type [[akka.actor.ExtendedActorSystem]]; + * this should be the preferred one because all reflective loading of classes + * during deserialization should use ExtendedActorSystem.dynamicAccess (see + * [[akka.actor.DynamicAccess]]), and
  • + *
  • without arguments, which is only an option if the serializer does not + * load classes using reflection.
  • + *
+ * + * Be sure to always use the PropertyManager for loading classes! This is necessary to + * avoid strange match errors and inequalities which arise from different class loaders loading + * the same class. */ trait Serializer { + /** * Completely unique value to identify this implementation of Serializer, used to optimize network traffic * Values from 0 to 16 is reserved for Akka internal usage @@ -28,42 +48,61 @@ trait Serializer { def includeManifest: Boolean /** - * Deserializes the given Array of Bytes into an AnyRef + * Produces an object from an array of bytes, with an optional type-hint; + * the class should be loaded using ActorSystem.dynamicAccess. */ - def fromBinary(bytes: Array[Byte]): AnyRef = fromBinary(bytes, None, None) + def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]]): AnyRef /** - * Deserializes the given Array of Bytes into an AnyRef with an optional type hint + * Java API: deserialize without type hint */ - def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]]): AnyRef = fromBinary(bytes, manifest, None) + final def fromBinary(bytes: Array[Byte]): AnyRef = fromBinary(bytes, None) /** - * Produces an object from an array of bytes, with an optional type-hint and a classloader to load the class into + * Java API: deserialize with type hint */ - def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]], classLoader: Option[ClassLoader]): AnyRef + final def fromBinary(bytes: Array[Byte], clazz: Class[_]): AnyRef = fromBinary(bytes, Option(clazz)) } /** - * Java API for creating a Serializer + * Java API for creating a Serializer: make sure to include a constructor which + * takes exactly one argument of type [[akka.actor.ExtendedActorSystem]], because + * that is the preferred constructor which will be invoked when reflectively instantiating + * the JSerializer (also possible with empty constructor). */ abstract class JSerializer extends Serializer { - def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]] = None, classLoader: Option[ClassLoader] = None): AnyRef = - fromBinary(bytes, manifest.orNull, classLoader.orNull) + final def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]]): AnyRef = + fromBinaryJava(bytes, manifest.orNull) /** - * This method should be overridden, - * manifest and classLoader may be null. + * This method must be implemented, manifest may be null. */ - def fromBinary(bytes: Array[Byte], manifest: Class[_], classLoader: ClassLoader): AnyRef + protected def fromBinaryJava(bytes: Array[Byte], manifest: Class[_]): AnyRef } -object JavaSerializer extends JavaSerializer object NullSerializer extends NullSerializer +object JavaSerializer { + + /** + * This holds a reference to the current ActorSystem (the surrounding context) + * during serialization and deserialization. + * + * If you are using Serializers yourself, outside of SerializationExtension, + * you'll need to surround the serialization/deserialization with: + * + * currentSystem.withValue(system) { + * ...code... + * } + */ + val currentSystem = new DynamicVariable[ExtendedActorSystem](null) + +} + /** * This Serializer uses standard Java Serialization */ -class JavaSerializer extends Serializer { +class JavaSerializer(val system: ExtendedActorSystem) extends Serializer { def includeManifest: Boolean = false @@ -77,12 +116,11 @@ class JavaSerializer extends Serializer { bos.toByteArray } - def fromBinary(bytes: Array[Byte], clazz: Option[Class[_]] = None, - classLoader: Option[ClassLoader] = None): AnyRef = { - val in = - if (classLoader.isDefined) new ClassLoaderObjectInputStream(classLoader.get, new ByteArrayInputStream(bytes)) else - new ObjectInputStream(new ByteArrayInputStream(bytes)) - val obj = in.readObject + def fromBinary(bytes: Array[Byte], clazz: Option[Class[_]]): AnyRef = { + val in = new ClassLoaderObjectInputStream(system.dynamicAccess.classLoader, new ByteArrayInputStream(bytes)) + val obj = JavaSerializer.currentSystem.withValue(system) { + in.readObject + } in.close() obj } @@ -96,5 +134,5 @@ class NullSerializer extends Serializer { def includeManifest: Boolean = false def identifier = 0 def toBinary(o: AnyRef) = nullAsBytes - def fromBinary(bytes: Array[Byte], clazz: Option[Class[_]] = None, classLoader: Option[ClassLoader] = None): AnyRef = null + def fromBinary(bytes: Array[Byte], clazz: Option[Class[_]]): AnyRef = null } diff --git a/akka-actor/src/main/scala/akka/util/Reflect.scala b/akka-actor/src/main/scala/akka/util/Reflect.scala new file mode 100644 index 0000000000..25c56a983f --- /dev/null +++ b/akka-actor/src/main/scala/akka/util/Reflect.scala @@ -0,0 +1,30 @@ +/** + * Copyright (C) 2009-2012 Typesafe Inc. + */ +package akka.util + +/** + * Collection of internal reflection utilities which may or may not be + * available (most services specific to HotSpot, but fails gracefully). + */ +object Reflect { + + /** + * This optionally holds a function which looks N levels above itself + * on the call stack and returns the `Class[_]` object for the code + * executing in that stack frame. Implemented using + * `sun.reflect.Reflection.getCallerClass` if available, None otherwise. + * + * Hint: when comparing to Thread.currentThread.getStackTrace, add two levels. + */ + val getCallerClass: Option[Int ⇒ Class[_]] = { + try { + val c = Class.forName("sun.reflect.Reflection"); + val m = c.getMethod("getCallerClass", Array(classOf[Int]): _*) + Some((i: Int) ⇒ m.invoke(null, Array[AnyRef](i.asInstanceOf[java.lang.Integer]): _*).asInstanceOf[Class[_]]) + } catch { + case NonFatal(e) ⇒ None + } + } + +} \ No newline at end of file diff --git a/akka-actor/src/main/scala/akka/util/ReflectiveAccess.scala b/akka-actor/src/main/scala/akka/util/ReflectiveAccess.scala deleted file mode 100644 index 19ad3bc995..0000000000 --- a/akka-actor/src/main/scala/akka/util/ReflectiveAccess.scala +++ /dev/null @@ -1,126 +0,0 @@ -/** - * Copyright (C) 2009-2012 Typesafe Inc. - */ - -package akka.util - -import java.lang.reflect.InvocationTargetException - -object ReflectiveAccess { - - val loader = getClass.getClassLoader - val noParams: Array[Class[_]] = Array() - val noArgs: Array[AnyRef] = Array() - - def createInstance[T](clazz: Class[_], - params: Array[Class[_]], - args: Array[AnyRef]): Either[Exception, T] = withErrorHandling { - assert(clazz ne null) - assert(params ne null) - assert(args ne null) - val ctor = clazz.getDeclaredConstructor(params: _*) - ctor.setAccessible(true) - Right(ctor.newInstance(args: _*).asInstanceOf[T]) - } - - def createInstance[T](fqn: String, - params: Array[Class[_]], - args: Array[AnyRef], - classloader: ClassLoader = loader): Either[Exception, T] = withErrorHandling { - assert(params ne null) - assert(args ne null) - getClassFor(fqn, classloader) match { - case Right(value) ⇒ - val ctor = value.getDeclaredConstructor(params: _*) - ctor.setAccessible(true) - Right(ctor.newInstance(args: _*).asInstanceOf[T]) - case Left(exception) ⇒ Left(exception) //We could just cast this to Either[Exception, T] but it's ugly - } - } - - def createInstance[T](clazz: Class[_], args: Seq[(Class[_], AnyRef)]): Either[Exception, T] = - createInstance(clazz, args.map(_._1).toArray, args.map(_._2).toArray) - - def createInstance[T](fqcn: String, args: Seq[(Class[_], AnyRef)], classloader: ClassLoader): Either[Exception, T] = - createInstance(fqcn, args.map(_._1).toArray, args.map(_._2).toArray, classloader) - - def createInstance[T](fqcn: String, args: Seq[(Class[_], AnyRef)]): Either[Exception, T] = - createInstance(fqcn, args.map(_._1).toArray, args.map(_._2).toArray, loader) - - //Obtains a reference to fqn.MODULE$ - def getObjectFor[T](fqn: String, classloader: ClassLoader = loader): Either[Exception, T] = try { - getClassFor(fqn, classloader) match { - case Right(value) ⇒ - val instance = value.getDeclaredField("MODULE$") - instance.setAccessible(true) - val obj = instance.get(null) - if (obj eq null) Left(new NullPointerException) else Right(obj.asInstanceOf[T]) - case Left(exception) ⇒ Left(exception) //We could just cast this to Either[Exception, T] but it's ugly - } - } catch { - case e: Exception ⇒ - Left(e) - } - - def getClassFor[T](fqn: String, classloader: ClassLoader = loader): Either[Exception, Class[T]] = try { - assert(fqn ne null) - - // First, use the specified CL - val first = try { - Right(classloader.loadClass(fqn).asInstanceOf[Class[T]]) - } catch { - case c: ClassNotFoundException ⇒ Left(c) - } - - if (first.isRight) first - else { - // Second option is to use the ContextClassLoader - val second = try { - Right(Thread.currentThread.getContextClassLoader.loadClass(fqn).asInstanceOf[Class[T]]) - } catch { - case c: ClassNotFoundException ⇒ Left(c) - } - - if (second.isRight) second - else { - val third = try { - if (classloader ne loader) Right(loader.loadClass(fqn).asInstanceOf[Class[T]]) else Left(null) //Horrid - } catch { - case c: ClassNotFoundException ⇒ Left(c) - } - - if (third.isRight) third - else { - try { - Right(Class.forName(fqn).asInstanceOf[Class[T]]) // Last option is Class.forName - } catch { - case c: ClassNotFoundException ⇒ Left(c) - } - } - } - } - } catch { - case e: Exception ⇒ Left(e) - } - - /** - * Caught exception is returned as Left(exception). - * Unwraps `InvocationTargetException` if its getTargetException is an `Exception`. - * Other `Throwable`, such as `Error` is thrown. - */ - @inline - private final def withErrorHandling[T](body: ⇒ Either[Exception, T]): Either[Exception, T] = { - try { - body - } catch { - case e: InvocationTargetException ⇒ e.getTargetException match { - case t: Exception ⇒ Left(t) - case t ⇒ throw t - } - case e: Exception ⇒ - Left(e) - } - } - -} - diff --git a/akka-agent/src/main/resources/reference.conf b/akka-agent/src/main/resources/reference.conf index 7009c0f432..2c3b121ba7 100644 --- a/akka-agent/src/main/resources/reference.conf +++ b/akka-agent/src/main/resources/reference.conf @@ -19,6 +19,5 @@ akka { executor = thread-pool-executor type = PinnedDispatcher } - } } diff --git a/akka-cluster/src/main/resources/reference.conf b/akka-cluster/src/main/resources/reference.conf index 749c138a26..e097d34f3e 100644 --- a/akka-cluster/src/main/resources/reference.conf +++ b/akka-cluster/src/main/resources/reference.conf @@ -26,7 +26,7 @@ akka { } gossip { - initialDelay = 5s + initial-delay = 5s frequency = 1s } } diff --git a/akka-cluster/src/main/scala/akka/cluster/ClusterSettings.scala b/akka-cluster/src/main/scala/akka/cluster/ClusterSettings.scala index 820290ea14..e88c3ae72c 100644 --- a/akka-cluster/src/main/scala/akka/cluster/ClusterSettings.scala +++ b/akka-cluster/src/main/scala/akka/cluster/ClusterSettings.scala @@ -18,7 +18,7 @@ class ClusterSettings(val config: Config, val systemName: String) { val FailureDetectorMaxSampleSize = getInt("akka.cluster.failure-detector.max-sample-size") val SeedNodeConnectionTimeout = Duration(config.getMilliseconds("akka.cluster.seed-node-connection-timeout"), MILLISECONDS) val MaxTimeToRetryJoiningCluster = Duration(config.getMilliseconds("akka.cluster.max-time-to-retry-joining-cluster"), MILLISECONDS) - val InitialDelayForGossip = Duration(getMilliseconds("akka.cluster.gossip.initialDelay"), MILLISECONDS) + val InitialDelayForGossip = Duration(getMilliseconds("akka.cluster.gossip.initial-delay"), MILLISECONDS) val GossipFrequency = Duration(getMilliseconds("akka.cluster.gossip.frequency"), MILLISECONDS) val SeedNodes = Set.empty[Address] ++ getStringList("akka.cluster.seed-nodes").asScala.collect { case AddressExtractor(addr) ⇒ addr diff --git a/akka-docs/dev/multi-jvm-testing.rst b/akka-docs/dev/multi-jvm-testing.rst index 85b8ae5027..18154b6c27 100644 --- a/akka-docs/dev/multi-jvm-testing.rst +++ b/akka-docs/dev/multi-jvm-testing.rst @@ -18,28 +18,46 @@ The multi-JVM testing is an sbt plugin that you can find here: http://github.com/typesafehub/sbt-multi-jvm -You can add it as a plugin by adding the following to your plugins/build.sbt:: +You can add it as a plugin by adding the following to your project/plugins.sbt:: resolvers += Classpaths.typesafeResolver addSbtPlugin("com.typesafe.sbtmultijvm" % "sbt-multi-jvm" % "0.1.9") -You can then add multi-JVM testing to a project by including the ``MultiJvm`` +You can then add multi-JVM testing to ``project/Build.scala`` by including the ``MultiJvm`` settings and config. For example, here is how the akka-remote project adds multi-JVM testing:: - import MultiJvmPlugin.{ MultiJvm, extraOptions } + import sbt._ + import Keys._ + import com.typesafe.sbtmultijvm.MultiJvmPlugin + import com.typesafe.sbtmultijvm.MultiJvmPlugin.{ MultiJvm, extraOptions } - lazy val cluster = Project( - id = "akka-remote", - base = file("akka-remote"), - settings = defaultSettings ++ MultiJvmPlugin.settings ++ Seq( - extraOptions in MultiJvm <<= (sourceDirectory in MultiJvm) { src => - (name: String) => (src ** (name + ".conf")).get.headOption.map("-Dconfig.file=" + _.absolutePath).toSeq - }, - test in Test <<= (test in Test) dependsOn (test in MultiJvm) + object AkkaBuild extends Build { + + lazy val remote = Project( + id = "akka-remote", + base = file("akka-remote"), + settings = defaultSettings ++ MultiJvmPlugin.settings ++ Seq( + extraOptions in MultiJvm <<= (sourceDirectory in MultiJvm) { src => + (name: String) => (src ** (name + ".conf")).get.headOption.map("-Dconfig.file=" + _.absolutePath).toSeq + }, + test in Test <<= (test in Test) dependsOn (test in MultiJvm) + ) + ) configs (MultiJvm) + + lazy val buildSettings = Defaults.defaultSettings ++ Seq( + organization := "com.typesafe.akka", + version := "2.0-SNAPSHOT", + scalaVersion := "2.9.1", + crossPaths := false ) - ) configs (MultiJvm) + + lazy val defaultSettings = buildSettings ++ Seq( + resolvers += "Typesafe Repo" at "http://repo.typesafe.com/typesafe/releases/" + ) + + } You can specify JVM options for the forked JVMs:: @@ -87,8 +105,8 @@ options after the test names and ``--``. For example: Creating application tests ========================== -The tests are discovered, and combined, through a naming convention. A test is -named with the following pattern: +The tests are discovered, and combined, through a naming convention. MultiJvm tests are +located in ``src/multi-jvm/scala`` directory. A test is named with the following pattern: .. code-block:: none diff --git a/akka-docs/general/configuration.rst b/akka-docs/general/configuration.rst index 1a01ba24c9..3e2bca240d 100644 --- a/akka-docs/general/configuration.rst +++ b/akka-docs/general/configuration.rst @@ -172,12 +172,12 @@ More advanced include and substitution mechanisms are explained in the `HOCON future = Futures.successful("foo", system.dispatcher()); + //#onSuccess future.onSuccess(new OnSuccess() { public void onSuccess(String result) { diff --git a/akka-docs/java/code/akka/docs/jrouting/CustomRouterDocTestBase.java b/akka-docs/java/code/akka/docs/jrouting/CustomRouterDocTestBase.java index 2a2e5c7f22..a20a351f06 100644 --- a/akka-docs/java/code/akka/docs/jrouting/CustomRouterDocTestBase.java +++ b/akka-docs/java/code/akka/docs/jrouting/CustomRouterDocTestBase.java @@ -17,6 +17,7 @@ import akka.util.Duration; import akka.util.Timeout; import akka.dispatch.Await; import akka.dispatch.Future; +import akka.dispatch.Dispatchers; import akka.testkit.AkkaSpec; import com.typesafe.config.ConfigFactory; import static akka.pattern.Patterns.ask; @@ -38,6 +39,19 @@ public class CustomRouterDocTestBase { public void tearDown() { system.shutdown(); } + + public static class MyActor extends UntypedActor { + @Override public void onReceive(Object o) {} + } + + @Test + public void demonstrateDispatchers() { + //#dispatchers + final ActorRef router = system.actorOf(new Props(MyActor.class) + .withRouter(new RoundRobinRouter(5).withDispatcher("head")) // “head” router runs on "head" dispatcher + .withDispatcher("workers")); // MyActor “workers” run on "workers" dispatcher + //#dispatchers + } //#crTest @Test @@ -105,6 +119,10 @@ public class CustomRouterDocTestBase { //#crRouter public static class VoteCountRouter extends CustomRouterConfig { + + @Override public String routerDispatcher() { + return Dispatchers.DefaultDispatcherId(); + } //#crRoute @Override diff --git a/akka-docs/java/code/akka/docs/serialization/SerializationDocTestBase.java b/akka-docs/java/code/akka/docs/serialization/SerializationDocTestBase.java index 3db385ca1c..fa0f5fcce3 100644 --- a/akka-docs/java/code/akka/docs/serialization/SerializationDocTestBase.java +++ b/akka-docs/java/code/akka/docs/serialization/SerializationDocTestBase.java @@ -3,6 +3,7 @@ */ package akka.docs.serialization; +import akka.japi.Option; import akka.serialization.JSerializer; import akka.serialization.Serialization; import akka.serialization.SerializationExtension; @@ -43,10 +44,8 @@ public class SerializationDocTestBase { // "fromBinary" deserializes the given array, // using the type hint (if any, see "includeManifest" above) - // into the optionally provided classLoader. - @Override public Object fromBinary(byte[] bytes, - Class clazz, - ClassLoader classLoader) { + @Override public Object fromBinaryJava(byte[] bytes, + Class clazz) { // Put your code that deserializes here //#... return null; diff --git a/akka-docs/java/code/akka/docs/zeromq/ZeromqDocTest.scala b/akka-docs/java/code/akka/docs/zeromq/ZeromqDocTest.scala new file mode 100644 index 0000000000..a9747959e3 --- /dev/null +++ b/akka-docs/java/code/akka/docs/zeromq/ZeromqDocTest.scala @@ -0,0 +1,8 @@ +/** + * Copyright (C) 2009-2012 Typesafe Inc. + */ +package akka.docs.zeromq + +import org.scalatest.junit.JUnitSuite + +class ZeromqDocTest extends ZeromqDocTestBase with JUnitSuite diff --git a/akka-docs/java/code/akka/docs/zeromq/ZeromqDocTestBase.java b/akka-docs/java/code/akka/docs/zeromq/ZeromqDocTestBase.java new file mode 100644 index 0000000000..ee8252a6ad --- /dev/null +++ b/akka-docs/java/code/akka/docs/zeromq/ZeromqDocTestBase.java @@ -0,0 +1,286 @@ +/** + * Copyright (C) 2009-2012 Typesafe Inc. + */ +package akka.docs.zeromq; + +//#pub-socket +import akka.zeromq.Bind; +import akka.zeromq.ZeroMQExtension; + +//#pub-socket +//#sub-socket +import akka.zeromq.Connect; +import akka.zeromq.Listener; +import akka.zeromq.Subscribe; + +//#sub-socket +//#unsub-topic-socket +import akka.zeromq.Unsubscribe; + +//#unsub-topic-socket +//#pub-topic +import akka.zeromq.Frame; +import akka.zeromq.ZMQMessage; + +//#pub-topic + +import akka.zeromq.HighWatermark; +import akka.zeromq.SocketOption; +import akka.zeromq.ZeroMQVersion; + +//#health +import akka.actor.ActorRef; +import akka.actor.UntypedActor; +import akka.actor.Props; +import akka.event.Logging; +import akka.event.LoggingAdapter; +import akka.util.Duration; +import akka.serialization.SerializationExtension; +import akka.serialization.Serialization; +import java.io.Serializable; +import java.lang.management.ManagementFactory; +//#health + +import com.typesafe.config.ConfigFactory; + +import java.lang.management.MemoryMXBean; +import java.lang.management.MemoryUsage; +import java.lang.management.OperatingSystemMXBean; +import java.util.concurrent.TimeUnit; +import java.util.Date; +import java.text.SimpleDateFormat; + +import akka.actor.ActorSystem; +import akka.testkit.AkkaSpec; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.Assume; + +import akka.zeromq.SocketType; + +public class ZeromqDocTestBase { + + ActorSystem system; + + @Before + public void setUp() { + system = ActorSystem.create("ZeromqDocTest", + ConfigFactory.parseString("akka.loglevel=INFO").withFallback(AkkaSpec.testConf())); + } + + @After + public void tearDown() { + system.shutdown(); + } + + @Test + public void demonstrateCreateSocket() { + Assume.assumeTrue(checkZeroMQInstallation()); + + //#pub-socket + ActorRef pubSocket = ZeroMQExtension.get(system).newPubSocket(new Bind("tcp://127.0.0.1:1233")); + //#pub-socket + + //#sub-socket + ActorRef listener = system.actorOf(new Props(ListenerActor.class)); + ActorRef subSocket = ZeroMQExtension.get(system).newSubSocket(new Connect("tcp://127.0.0.1:1233"), + new Listener(listener), Subscribe.all()); + //#sub-socket + + //#sub-topic-socket + ActorRef subTopicSocket = ZeroMQExtension.get(system).newSubSocket(new Connect("tcp://127.0.0.1:1233"), + new Listener(listener), new Subscribe("foo.bar")); + //#sub-topic-socket + + //#unsub-topic-socket + subTopicSocket.tell(new Unsubscribe("foo.bar")); + //#unsub-topic-socket + + byte[] payload = new byte[0]; + //#pub-topic + pubSocket.tell(new ZMQMessage(new Frame("foo.bar"), new Frame(payload))); + //#pub-topic + + //#high-watermark + ActorRef highWatermarkSocket = ZeroMQExtension.get(system).newRouterSocket( + new SocketOption[] { new Listener(listener), new Bind("tcp://127.0.0.1:1233"), new HighWatermark(50000) }); + //#high-watermark + } + + @Test + public void demonstratePubSub() throws Exception { + Assume.assumeTrue(checkZeroMQInstallation()); + + //#health2 + + system.actorOf(new Props(HealthProbe.class), "health"); + //#health2 + + //#logger2 + + system.actorOf(new Props(Logger.class), "logger"); + //#logger2 + + //#alerter2 + + system.actorOf(new Props(HeapAlerter.class), "alerter"); + //#alerter2 + + // Let it run for a while to see some output. + // Don't do like this in real tests, this is only doc demonstration. + Thread.sleep(3000L); + } + + private boolean checkZeroMQInstallation() { + try { + ZeroMQVersion v = ZeroMQExtension.get(system).version(); + return (v.major() == 2 && v.minor() == 1); + } catch (LinkageError e) { + return false; + } + } + + //#listener-actor + public static class ListenerActor extends UntypedActor { + public void onReceive(Object message) throws Exception { + //... + } + } + + //#listener-actor + + //#health + + public static final Object TICK = "TICK"; + + public static class Heap implements Serializable { + public final long timestamp; + public final long used; + public final long max; + + public Heap(long timestamp, long used, long max) { + this.timestamp = timestamp; + this.used = used; + this.max = max; + } + } + + public static class Load implements Serializable { + public final long timestamp; + public final double loadAverage; + + public Load(long timestamp, double loadAverage) { + this.timestamp = timestamp; + this.loadAverage = loadAverage; + } + } + + public static class HealthProbe extends UntypedActor { + + ActorRef pubSocket = ZeroMQExtension.get(getContext().system()).newPubSocket(new Bind("tcp://127.0.0.1:1237")); + MemoryMXBean memory = ManagementFactory.getMemoryMXBean(); + OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean(); + Serialization ser = SerializationExtension.get(getContext().system()); + + @Override + public void preStart() { + getContext().system().scheduler() + .schedule(Duration.parse("1 second"), Duration.parse("1 second"), getSelf(), TICK); + } + + @Override + public void postRestart(Throwable reason) { + // don't call preStart, only schedule once + } + + @Override + public void onReceive(Object message) { + if (message.equals(TICK)) { + MemoryUsage currentHeap = memory.getHeapMemoryUsage(); + long timestamp = System.currentTimeMillis(); + + // use akka SerializationExtension to convert to bytes + byte[] heapPayload = ser.serializerFor(Heap.class).toBinary( + new Heap(timestamp, currentHeap.getUsed(), currentHeap.getMax())); + // the first frame is the topic, second is the message + pubSocket.tell(new ZMQMessage(new Frame("health.heap"), new Frame(heapPayload))); + + // use akka SerializationExtension to convert to bytes + byte[] loadPayload = ser.serializerFor(Load.class).toBinary(new Load(timestamp, os.getSystemLoadAverage())); + // the first frame is the topic, second is the message + pubSocket.tell(new ZMQMessage(new Frame("health.load"), new Frame(loadPayload))); + } else { + unhandled(message); + } + } + + } + + //#health + + //#logger + public static class Logger extends UntypedActor { + + ActorRef subSocket = ZeroMQExtension.get(getContext().system()).newSubSocket(new Connect("tcp://127.0.0.1:1237"), + new Listener(getSelf()), new Subscribe("health")); + Serialization ser = SerializationExtension.get(getContext().system()); + SimpleDateFormat timestampFormat = new SimpleDateFormat("HH:mm:ss.SSS"); + LoggingAdapter log = Logging.getLogger(getContext().system(), this); + + @Override + public void onReceive(Object message) { + if (message instanceof ZMQMessage) { + ZMQMessage m = (ZMQMessage) message; + // the first frame is the topic, second is the message + if (m.firstFrameAsString().equals("health.heap")) { + 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))); + } else if (m.firstFrameAsString().equals("health.load")) { + Load load = (Load) ser.serializerFor(Load.class).fromBinary(m.payload(1)); + log.info("Load average {}, at {}", load.loadAverage, timestampFormat.format(new Date(load.timestamp))); + } + } else { + unhandled(message); + } + } + + } + + //#logger + + //#alerter + public static class HeapAlerter extends UntypedActor { + + ActorRef subSocket = ZeroMQExtension.get(getContext().system()).newSubSocket(new Connect("tcp://127.0.0.1:1237"), + new Listener(getSelf()), new Subscribe("health.heap")); + Serialization ser = SerializationExtension.get(getContext().system()); + LoggingAdapter log = Logging.getLogger(getContext().system(), this); + int count = 0; + + @Override + public void onReceive(Object message) { + if (message instanceof ZMQMessage) { + ZMQMessage m = (ZMQMessage) message; + // the first frame is the topic, second is the message + if (m.firstFrameAsString().equals("health.heap")) { + Heap heap = (Heap) ser.serializerFor(Heap.class).fromBinary(m.payload(1)); + if (((double) heap.used / heap.max) > 0.9) { + count += 1; + } else { + count = 0; + } + if (count > 10) { + log.warning("Need more memory, using {} %", (100.0 * heap.used / heap.max)); + } + } + } else { + unhandled(message); + } + } + + } + //#alerter + +} diff --git a/akka-docs/java/index.rst b/akka-docs/java/index.rst index 319dbab302..9ed521e5e7 100644 --- a/akka-docs/java/index.rst +++ b/akka-docs/java/index.rst @@ -23,3 +23,4 @@ Java API transactors fsm extending-akka + zeromq diff --git a/akka-docs/java/logging.rst b/akka-docs/java/logging.rst index 01dedee0ad..fabd9f6e92 100644 --- a/akka-docs/java/logging.rst +++ b/akka-docs/java/logging.rst @@ -61,7 +61,7 @@ This config option is very good if you want to know what config settings are loa akka { # Log the complete configuration at INFO level when the actor system is started. # This is useful when you are uncertain of what configuration is used. - logConfigOnStart = on + log-config-on-start = on } If you want very detailed logging of all automatically received messages that are processed @@ -224,7 +224,7 @@ With Logback the thread name is available with ``%X{sourceThread}`` specifier wi .. note:: - + It will probably be a good idea to use the ``sourceThread`` MDC value also in non-Akka parts of the application in order to have this property consistently available in the logs. diff --git a/akka-docs/java/routing.rst b/akka-docs/java/routing.rst index a422900440..265e31a984 100644 --- a/akka-docs/java/routing.rst +++ b/akka-docs/java/routing.rst @@ -8,11 +8,6 @@ Routing (Java) .. contents:: :local: -Akka-core includes some building blocks to build more complex message flow handlers, they are listed and explained below: - -Router ------- - A Router is an actor that routes incoming messages to outbound actors. The router routes the messages sent to it to its underlying actors called 'routees'. @@ -249,6 +244,16 @@ This is an example of how to programatically create a resizable router: *It is also worth pointing out that if you define the ``router`` in the configuration file then this value will be used instead of any programmatically sent parameters.* +.. note:: + + Resizing is triggered by sending messages to the actor pool, but it is not + completed synchronously; instead a message is sent to the “head” + :class:`Router` to perform the size change. Thus you cannot rely on resizing + to instantaneously create new workers when all others are busy, because the + message just sent will be queued to the mailbox of a busy actor. To remedy + this, configure the pool to use a balancing dispatcher, see `Configuring + Dispatchers`_ for more information. + Custom Router ^^^^^^^^^^^^^ @@ -312,3 +317,23 @@ A router with dynamically resizable number of routees is implemented by providin in ``resizer`` method of the ``RouterConfig``. See ``akka.routing.DefaultResizer`` for inspiration of how to write your own resize strategy. +Configuring Dispatchers +^^^^^^^^^^^^^^^^^^^^^^^ + +The dispatcher for created children of the router will be taken from +:class:`Props` as described in :ref:`dispatchers-java`. For a dynamic pool it +makes sense to configure the :class:`BalancingDispatcher` if the precise +routing is not so important (i.e. no consistent hashing or round-robin is +required); this enables newly created routees to pick up work immediately by +stealing it from their siblings. + +The “head” router, of couse, cannot run on the same balancing dispatcher, +because it does not process the same messages, hence this special actor does +not use the dispatcher configured in :class:`Props`, but takes the +``routerDispatcher`` from the :class:`RouterConfig` instead, which defaults to +the actor system’s default dispatcher. All standard routers allow setting this +property in their constructor or factory method, custom routers have to +implement the method in a suitable way. + +.. includecode:: code/akka/docs/jrouting/CustomRouterDocTestBase.java#dispatchers + diff --git a/akka-docs/java/serialization.rst b/akka-docs/java/serialization.rst index e44d69f162..e4815d0ea2 100644 --- a/akka-docs/java/serialization.rst +++ b/akka-docs/java/serialization.rst @@ -103,3 +103,15 @@ which is done by extending ``akka.serialization.JSerializer``, like this: Then you only need to fill in the blanks, bind it to a name in your :ref:`configuration` and then list which classes that should be serialized using it. + +A Word About Java Serialization +=============================== + +When using Java serialization without employing the :class:`JavaSerializer` for +the task, you must make sure to supply a valid :class:`ExtendedActorSystem` in +the dynamic variable ``JavaSerializer.currentSystem``. This is used when +reading in the representation of an :class:`ActorRef` for turning the string +representation into a real reference. :class:`DynamicVariable` is a +thread-local variable, so be sure to have it set while deserializing anything +which might contain actor references. + diff --git a/akka-docs/java/zeromq.rst b/akka-docs/java/zeromq.rst new file mode 100644 index 0000000000..e24dd28796 --- /dev/null +++ b/akka-docs/java/zeromq.rst @@ -0,0 +1,98 @@ + +.. _zeromq-java: + +############### + ZeroMQ (Java) +############### + +.. sidebar:: Contents + + .. contents:: :local: + +Akka provides a ZeroMQ module which abstracts a ZeroMQ connection and therefore allows interaction between Akka actors to take place over ZeroMQ connections. The messages can be of a proprietary format or they can be defined using Protobuf. The socket actor is fault-tolerant by default and when you use the newSocket method to create new sockets it will properly reinitialize the socket. + +ZeroMQ is very opinionated when it comes to multi-threading so configuration option `akka.zeromq.socket-dispatcher` always needs to be configured to a PinnedDispatcher, because the actual ZeroMQ socket can only be accessed by the thread that created it. + +The ZeroMQ module for Akka is written against an API introduced in JZMQ, which uses JNI to interact with the native ZeroMQ library. Instead of using JZMQ, the module uses ZeroMQ binding for Scala that uses the native ZeroMQ library through JNA. In other words, the only native library that this module requires is the native ZeroMQ library. +The benefit of the scala library is that you don't need to compile and manage native dependencies at the cost of some runtime performance. The scala-bindings are compatible with the JNI bindings so they are a drop-in replacement, in case you really need to get that extra bit of performance out. + +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. +Sockets are always created using the ``akka.zeromq.ZeroMQExtension``, for example: + +.. includecode:: code/akka/docs/zeromq/ZeromqDocTestBase.java#pub-socket + +Above examples will create a ZeroMQ Publisher socket that is Bound to the port 1233 on localhost. + +Similarly you can create a subscription socket, with a listener, that subscribes to all messages from the publisher using: + +.. includecode:: code/akka/docs/zeromq/ZeromqDocTestBase.java#sub-socket + +.. includecode:: code/akka/docs/zeromq/ZeromqDocTestBase.java#listener-actor + +The following sub-sections describe the supported connection patterns and how they can be used in an Akka environment. However, for a comprehensive discussion of connection patterns, please refer to `ZeroMQ -- The Guide `_. + +Publisher-subscriber connection +------------------------------- + +In a publisher-subscriber (pub-sub) connection, the publisher accepts one or more subscribers. Each subscriber shall +subscribe to one or more topics, whereas the publisher publishes messages to a set of topics. Also, a subscriber can +subscribe to all available topics. In an Akka environment, pub-sub connections shall be used when an actor sends messages +to one or more actors that do not interact with the actor that sent the message. + +When you're using zeromq pub/sub you should be aware that it needs multicast - check your cloud - to work properly and that the filtering of events for topics happens client side, so all events are always broadcasted to every subscriber. + +An actor is subscribed to a topic as follows: + +.. includecode:: code/akka/docs/zeromq/ZeromqDocTestBase.java#sub-topic-socket + +It is a prefix match so it is subscribed to all topics starting with ``foo.bar``. Note that if the given string is empty or +``Subscribe.all()`` is used, the actor is subscribed to all topics. + +To unsubscribe from a topic you do the following: + +.. includecode:: code/akka/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. + +.. includecode:: code/akka/docs/zeromq/ZeromqDocTestBase.java#pub-topic + +Pub-Sub in Action +^^^^^^^^^^^^^^^^^ + +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 +and ``Load`` events on the ``"health.load"`` topic. + +.. includecode:: code/akka/docs/zeromq/ZeromqDocTestBase.java#health + +.. includecode:: code/akka/docs/zeromq/ZeromqDocTestBase.java#health2 + +Let's add one subscriber that logs the information. It subscribes to all topics starting with ``"health"``, i.e. both ``Heap`` and +``Load`` events. + +.. includecode:: code/akka/docs/zeromq/ZeromqDocTestBase.java#logger + +.. includecode:: code/akka/docs/zeromq/ZeromqDocTestBase.java#logger2 + +Another subscriber keep track of used heap and warns if too much heap is used. It only subscribes to ``Heap`` events. + +.. includecode:: code/akka/docs/zeromq/ZeromqDocTestBase.java#alerter + +.. includecode:: code/akka/docs/zeromq/ZeromqDocTestBase.java#alerter2 + +Router-Dealer connection +------------------------ + +While Pub/Sub is nice the real advantage of zeromq is that it is a "lego-box" for reliable messaging. And because there are so many integrations the multi-language support is fantastic. +When you're using ZeroMQ to integrate many systems you'll probably need to build your own ZeroMQ devices. This is where the router and dealer socket types come in handy. +With those socket types you can build your own reliable pub sub broker that uses TCP/IP and does publisher side filtering of events. + +To create a Router socket that has a high watermark configured, you would do: + +.. includecode:: code/akka/docs/zeromq/ZeromqDocTestBase.java#high-watermark + +The akka-zeromq module accepts most if not all the available configuration options for a zeromq socket. diff --git a/akka-docs/modules/code/akka/docs/actor/mailbox/DurableMailboxDocSpec.scala b/akka-docs/modules/code/akka/docs/actor/mailbox/DurableMailboxDocSpec.scala index 827b4200d8..2f67c607ed 100644 --- a/akka-docs/modules/code/akka/docs/actor/mailbox/DurableMailboxDocSpec.scala +++ b/akka-docs/modules/code/akka/docs/actor/mailbox/DurableMailboxDocSpec.scala @@ -23,7 +23,7 @@ object DurableMailboxDocSpec { val config = """ //#dispatcher-config my-dispatcher { - mailboxType = akka.actor.mailbox.FileBasedMailboxType + mailbox-type = akka.actor.mailbox.FileBasedMailboxType } //#dispatcher-config """ diff --git a/akka-docs/modules/durable-mailbox.rst b/akka-docs/modules/durable-mailbox.rst index dc83522705..e8df16c97f 100644 --- a/akka-docs/modules/durable-mailbox.rst +++ b/akka-docs/modules/durable-mailbox.rst @@ -99,7 +99,7 @@ You configure durable mailboxes through the dispatcher, as described in Config:: my-dispatcher { - mailboxType = akka.actor.mailbox.FileBasedMailboxType + mailbox-type = akka.actor.mailbox.FileBasedMailboxType } You can also configure and tune the file-based durable mailbox. This is done in @@ -124,7 +124,7 @@ You configure durable mailboxes through the dispatcher, as described in Config:: my-dispatcher { - mailboxType = akka.actor.mailbox.RedisBasedMailboxType + mailbox-type = akka.actor.mailbox.RedisBasedMailboxType } You also need to configure the IP and port for the Redis server. This is done in @@ -150,7 +150,7 @@ You configure durable mailboxes through the dispatcher, as described in Config:: my-dispatcher { - mailboxType = akka.actor.mailbox.ZooKeeperBasedMailboxType + mailbox-type = akka.actor.mailbox.ZooKeeperBasedMailboxType } You also need to configure ZooKeeper server addresses, timeouts, etc. This is @@ -173,7 +173,7 @@ You configure durable mailboxes through the dispatcher, as described in Config:: my-dispatcher { - mailboxType = akka.actor.mailbox.BeanstalkBasedMailboxType + mailbox-type = akka.actor.mailbox.BeanstalkBasedMailboxType } You also need to configure the IP, and port, and so on, for the Beanstalk @@ -202,7 +202,7 @@ You configure durable mailboxes through the dispatcher, as described in Config:: my-dispatcher { - mailboxType = akka.actor.mailbox.MongoBasedMailboxType + mailbox-type = akka.actor.mailbox.MongoBasedMailboxType } You will need to configure the URI for the MongoDB server, using the URI Format specified in the diff --git a/akka-docs/project/migration-guide-1.3.x-2.0.x.rst b/akka-docs/project/migration-guide-1.3.x-2.0.x.rst index 8a46d5f654..853381ca0d 100644 --- a/akka-docs/project/migration-guide-1.3.x-2.0.x.rst +++ b/akka-docs/project/migration-guide-1.3.x-2.0.x.rst @@ -326,7 +326,7 @@ v1.3:: v2.0:: -Dconfig.file= - -Dakka.logConfigOnStart=on + -Dakka.log-config-on-start=on Several configuration properties have been changed, such as: diff --git a/akka-docs/scala/code/akka/docs/dispatcher/DispatcherDocSpec.scala b/akka-docs/scala/code/akka/docs/dispatcher/DispatcherDocSpec.scala index cd57fbeddc..4de32c3b57 100644 --- a/akka-docs/scala/code/akka/docs/dispatcher/DispatcherDocSpec.scala +++ b/akka-docs/scala/code/akka/docs/dispatcher/DispatcherDocSpec.scala @@ -94,13 +94,13 @@ object DispatcherDocSpec { //#prio-dispatcher-config prio-dispatcher { - mailboxType = "akka.docs.dispatcher.DispatcherDocSpec$PrioMailbox" + mailbox-type = "akka.docs.dispatcher.DispatcherDocSpec$PrioMailbox" } //#prio-dispatcher-config //#prio-dispatcher-config-java prio-dispatcher-java { - mailboxType = "akka.docs.dispatcher.DispatcherDocTestBase$PrioMailbox" + mailbox-type = "akka.docs.dispatcher.DispatcherDocTestBase$PrioMailbox" } //#prio-dispatcher-config-java """ diff --git a/akka-docs/scala/code/akka/docs/routing/RouterDocSpec.scala b/akka-docs/scala/code/akka/docs/routing/RouterDocSpec.scala new file mode 100644 index 0000000000..229c66f13e --- /dev/null +++ b/akka-docs/scala/code/akka/docs/routing/RouterDocSpec.scala @@ -0,0 +1,29 @@ +/** + * Copyright (C) 2009-2012 Typesafe Inc. + */ +package akka.docs.routing + +import RouterDocSpec.MyActor +import akka.actor.{ Props, Actor } +import akka.testkit.AkkaSpec +import akka.routing.RoundRobinRouter + +object RouterDocSpec { + class MyActor extends Actor { + def receive = { + case _ ⇒ + } + } +} + +class RouterDocSpec extends AkkaSpec { + + import RouterDocSpec._ + + //#dispatchers + val router = system.actorOf(Props[MyActor] + .withRouter(RoundRobinRouter(5, routerDispatcher = "router")) // “head” will run on "router" dispatcher + .withDispatcher("workers")) // MyActor workers will run on "workers" dispatcher + //#dispatchers + +} \ No newline at end of file diff --git a/akka-docs/scala/code/akka/docs/serialization/SerializationDocSpec.scala b/akka-docs/scala/code/akka/docs/serialization/SerializationDocSpec.scala index ce40adce3e..38054594cd 100644 --- a/akka-docs/scala/code/akka/docs/serialization/SerializationDocSpec.scala +++ b/akka-docs/scala/code/akka/docs/serialization/SerializationDocSpec.scala @@ -35,8 +35,7 @@ class MyOwnSerializer extends Serializer { // using the type hint (if any, see "includeManifest" above) // into the optionally provided classLoader. def fromBinary(bytes: Array[Byte], - clazz: Option[Class[_]], - classLoader: Option[ClassLoader] = None): AnyRef = { + clazz: Option[Class[_]]): AnyRef = { // Put your code that deserializes here //#... null @@ -143,9 +142,7 @@ class SerializationDocSpec extends AkkaSpec { val bytes = serializer.toBinary(original) // Turn it back into an object - val back = serializer.fromBinary(bytes, - manifest = None, - classLoader = None) + val back = serializer.fromBinary(bytes, manifest = None) // Voilá! back must equal(original) diff --git a/akka-docs/scala/code/akka/docs/zeromq/ZeromqDocSpec.scala b/akka-docs/scala/code/akka/docs/zeromq/ZeromqDocSpec.scala new file mode 100644 index 0000000000..e5e3325dd5 --- /dev/null +++ b/akka-docs/scala/code/akka/docs/zeromq/ZeromqDocSpec.scala @@ -0,0 +1,195 @@ +/** + * Copyright (C) 2009-2012 Typesafe Inc. + */ +package akka.docs.zeromq + +import akka.actor.Actor +import akka.actor.Props +import akka.util.duration._ +import akka.testkit._ +import akka.zeromq.ZeroMQVersion +import akka.zeromq.ZeroMQExtension +import java.text.SimpleDateFormat +import java.util.Date +import akka.zeromq.SocketType +import akka.zeromq.Bind + +object ZeromqDocSpec { + + //#health + import akka.zeromq._ + import akka.actor.Actor + import akka.actor.Props + import akka.actor.ActorLogging + import akka.serialization.SerializationExtension + import java.lang.management.ManagementFactory + + case object Tick + case class Heap(timestamp: Long, used: Long, max: Long) + case class Load(timestamp: Long, loadAverage: Double) + + class HealthProbe extends Actor { + + val pubSocket = context.system.newSocket(SocketType.Pub, Bind("tcp://127.0.0.1:1235")) + val memory = ManagementFactory.getMemoryMXBean + val os = ManagementFactory.getOperatingSystemMXBean + val ser = SerializationExtension(context.system) + + override def preStart() { + context.system.scheduler.schedule(1 second, 1 second, self, Tick) + } + + override def postRestart(reason: Throwable) { + // don't call preStart, only schedule once + } + + def receive: Receive = { + case Tick ⇒ + val currentHeap = memory.getHeapMemoryUsage + val timestamp = System.currentTimeMillis + + // use akka SerializationExtension to convert to bytes + val heapPayload = ser.serialize(Heap(timestamp, currentHeap.getUsed, currentHeap.getMax)).fold(throw _, identity) + // the first frame is the topic, second is the message + pubSocket ! ZMQMessage(Seq(Frame("health.heap"), Frame(heapPayload))) + + // use akka SerializationExtension to convert to bytes + val loadPayload = ser.serialize(Load(timestamp, os.getSystemLoadAverage)).fold(throw _, identity) + // the first frame is the topic, second is the message + pubSocket ! ZMQMessage(Seq(Frame("health.load"), Frame(loadPayload))) + } + } + //#health + + //#logger + class Logger extends Actor with ActorLogging { + + context.system.newSocket(SocketType.Sub, Listener(self), Connect("tcp://127.0.0.1:1235"), Subscribe("health")) + val ser = SerializationExtension(context.system) + val timestampFormat = new SimpleDateFormat("HH:mm:ss.SSS") + + def receive = { + // the first frame is the topic, second is the message + case m: ZMQMessage if m.firstFrameAsString == "health.heap" ⇒ + ser.deserialize(m.payload(1), classOf[Heap]) match { + case Right(Heap(timestamp, used, max)) ⇒ + log.info("Used heap {} bytes, at {}", used, timestampFormat.format(new Date(timestamp))) + case Left(e) ⇒ throw e + } + + case m: ZMQMessage if m.firstFrameAsString == "health.load" ⇒ + ser.deserialize(m.payload(1), classOf[Load]) match { + case Right(Load(timestamp, loadAverage)) ⇒ + log.info("Load average {}, at {}", loadAverage, timestampFormat.format(new Date(timestamp))) + case Left(e) ⇒ throw e + } + } + } + //#logger + + //#alerter + class HeapAlerter extends Actor with ActorLogging { + + context.system.newSocket(SocketType.Sub, Listener(self), Connect("tcp://127.0.0.1:1235"), Subscribe("health.heap")) + val ser = SerializationExtension(context.system) + var count = 0 + + def receive = { + // the first frame is the topic, second is the message + case m: ZMQMessage if m.firstFrameAsString == "health.heap" ⇒ + ser.deserialize(m.payload(1), classOf[Heap]) match { + case Right(Heap(timestamp, used, max)) ⇒ + if ((used.toDouble / max) > 0.9) count += 1 + else count = 0 + if (count > 10) log.warning("Need more memory, using {} %", (100.0 * used / max)) + case Left(e) ⇒ throw e + } + } + } + //#alerter + +} + +class ZeromqDocSpec extends AkkaSpec("akka.loglevel=INFO") { + import ZeromqDocSpec._ + + "demonstrate how to create socket" in { + checkZeroMQInstallation() + + //#pub-socket + import akka.zeromq.ZeroMQExtension + val pubSocket = ZeroMQExtension(system).newSocket(SocketType.Pub, Bind("tcp://127.0.0.1:1234")) + //#pub-socket + + //#pub-socket2 + import akka.zeromq._ + val pubSocket2 = system.newSocket(SocketType.Pub, Bind("tcp://127.0.0.1:1234")) + //#pub-socket2 + + //#sub-socket + import akka.zeromq._ + val listener = system.actorOf(Props(new Actor { + def receive: Receive = { + case Connecting ⇒ //... + case m: ZMQMessage ⇒ //... + case _ ⇒ //... + } + })) + val subSocket = system.newSocket(SocketType.Sub, Listener(listener), Connect("tcp://127.0.0.1:1234"), SubscribeAll) + //#sub-socket + + //#sub-topic-socket + val subTopicSocket = system.newSocket(SocketType.Sub, Listener(listener), Connect("tcp://127.0.0.1:1234"), Subscribe("foo.bar")) + //#sub-topic-socket + + //#unsub-topic-socket + subTopicSocket ! Unsubscribe("foo.bar") + //#unsub-topic-socket + + val payload = Array.empty[Byte] + //#pub-topic + pubSocket ! ZMQMessage(Seq(Frame("foo.bar"), Frame(payload))) + //#pub-topic + + //#high-watermark + val highWatermarkSocket = system.newSocket( + SocketType.Router, + Listener(listener), + Bind("tcp://127.0.0.1:1234"), + HighWatermark(50000)) + //#high-watermark + } + + "demonstrate pub-sub" in { + checkZeroMQInstallation() + + //#health + + system.actorOf(Props[HealthProbe], name = "health") + //#health + + //#logger + + system.actorOf(Props[Logger], name = "logger") + //#logger + + //#alerter + + system.actorOf(Props[HeapAlerter], name = "alerter") + //#alerter + + // Let it run for a while to see some output. + // Don't do like this in real tests, this is only doc demonstration. + 3.seconds.sleep() + + } + + def checkZeroMQInstallation() = try { + ZeroMQExtension(system).version match { + case ZeroMQVersion(2, 1, _) ⇒ Unit + case version ⇒ pending + } + } catch { + case e: LinkageError ⇒ pending + } +} diff --git a/akka-docs/scala/logging.rst b/akka-docs/scala/logging.rst index 19630cda18..d8cbb948c1 100644 --- a/akka-docs/scala/logging.rst +++ b/akka-docs/scala/logging.rst @@ -58,7 +58,7 @@ This config option is very good if you want to know what config settings are loa akka { # Log the complete configuration at INFO level when the actor system is started. # This is useful when you are uncertain of what configuration is used. - logConfigOnStart = on + log-config-on-start = on } If you want very detailed logging of all user-level messages that are processed @@ -257,7 +257,7 @@ With Logback the thread name is available with ``%X{sourceThread}`` specifier wi .. note:: - + It will probably be a good idea to use the ``sourceThread`` MDC value also in non-Akka parts of the application in order to have this property consistently available in the logs. diff --git a/akka-docs/scala/routing.rst b/akka-docs/scala/routing.rst index f67841df2c..161ab88db9 100644 --- a/akka-docs/scala/routing.rst +++ b/akka-docs/scala/routing.rst @@ -8,11 +8,6 @@ Routing (Scala) .. contents:: :local: -Akka-core includes some building blocks to build more complex message flow handlers, they are listed and explained below: - -Router ------- - A Router is an actor that routes incoming messages to outbound actors. The router routes the messages sent to it to its underlying actors called 'routees'. @@ -250,6 +245,16 @@ This is an example of how to programatically create a resizable router: *It is also worth pointing out that if you define the ``router`` in the configuration file then this value will be used instead of any programmatically sent parameters.* +.. note:: + + Resizing is triggered by sending messages to the actor pool, but it is not + completed synchronously; instead a message is sent to the “head” + :class:`Router` to perform the size change. Thus you cannot rely on resizing + to instantaneously create new workers when all others are busy, because the + message just sent will be queued to the mailbox of a busy actor. To remedy + this, configure the pool to use a balancing dispatcher, see `Configuring + Dispatchers`_ for more information. + Custom Router ^^^^^^^^^^^^^ @@ -311,3 +316,23 @@ A router with dynamically resizable number of routees is implemented by providin in ``resizer`` method of the ``RouterConfig``. See ``akka.routing.DefaultResizer`` for inspiration of how to write your own resize strategy. +Configuring Dispatchers +^^^^^^^^^^^^^^^^^^^^^^^ + +The dispatcher for created children of the router will be taken from +:class:`Props` as described in :ref:`dispatchers-scala`. For a dynamic pool it +makes sense to configure the :class:`BalancingDispatcher` if the precise +routing is not so important (i.e. no consistent hashing or round-robin is +required); this enables newly created routees to pick up work immediately by +stealing it from their siblings. + +The “head” router, of couse, cannot run on the same balancing dispatcher, +because it does not process the same messages, hence this special actor does +not use the dispatcher configured in :class:`Props`, but takes the +``routerDispatcher`` from the :class:`RouterConfig` instead, which defaults to +the actor system’s default dispatcher. All standard routers allow setting this +property in their constructor or factory method, custom routers have to +implement the method in a suitable way. + +.. includecode:: code/akka/docs/routing/RouterDocSpec.scala#dispatchers + diff --git a/akka-docs/scala/serialization.rst b/akka-docs/scala/serialization.rst index 4735548140..9bdfa2d2c8 100644 --- a/akka-docs/scala/serialization.rst +++ b/akka-docs/scala/serialization.rst @@ -101,3 +101,15 @@ First you need to create a class definition of your ``Serializer`` like so: Then you only need to fill in the blanks, bind it to a name in your :ref:`configuration` and then list which classes that should be serialized using it. + +A Word About Java Serialization +=============================== + +When using Java serialization without employing the :class:`JavaSerializer` for +the task, you must make sure to supply a valid :class:`ExtendedActorSystem` in +the dynamic variable ``JavaSerializer.currentSystem``. This is used when +reading in the representation of an :class:`ActorRef` for turning the string +representation into a real reference. :class:`DynamicVariable` is a +thread-local variable, so be sure to have it set while deserializing anything +which might contain actor references. + diff --git a/akka-docs/scala/zeromq.rst b/akka-docs/scala/zeromq.rst index e080979910..fa1160ee6d 100644 --- a/akka-docs/scala/zeromq.rst +++ b/akka-docs/scala/zeromq.rst @@ -1,8 +1,10 @@ -.. _zeromq-module: +.. _zeromq-scala: + +################ + ZeroMQ (Scala) +################ -ZeroMQ -====== .. sidebar:: Contents @@ -12,83 +14,76 @@ Akka provides a ZeroMQ module which abstracts a ZeroMQ connection and therefore ZeroMQ is very opinionated when it comes to multi-threading so configuration option `akka.zeromq.socket-dispatcher` always needs to be configured to a PinnedDispatcher, because the actual ZeroMQ socket can only be accessed by the thread that created it. -The ZeroMQ module for Akka is written against an API introduced in JZMQ, which uses JNI to interact with the native ZeroMQ library. Instead of using JZMQ, the module uses ZeroMQ binding for Scala that uses the native ZeroMQ library through JNA. In other words, the only native library that this module requires is the native ZeroMQ library. +The ZeroMQ module for Akka is written against an API introduced in JZMQ, which uses JNI to interact with the native ZeroMQ library. Instead of using JZMQ, the module uses ZeroMQ binding for Scala that uses the native ZeroMQ library through JNA. In other words, the only native library that this module requires is the native ZeroMQ library. The benefit of the scala library is that you don't need to compile and manage native dependencies at the cost of some runtime performance. The scala-bindings are compatible with the JNI bindings so they are a drop-in replacement, in case you really need to get that extra bit of performance out. 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. Sockets are always created using ``akka.zeromq.ZeroMQ.newSocket``, for example: +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: -.. code-block:: scala +.. includecode:: code/akka/docs/zeromq/ZeromqDocSpec.scala#pub-socket - import akka.zeromq._ - val socket = system.zeromq.newSocket(SocketType.Pub, Bind("tcp://127.0.0.1:1234")) +or by importing the ``akka.zeromq._`` package to make newSocket method available on system, via an implicit conversion. -will create a ZeroMQ Publisher socket that is Bound to the port 1234 on localhost. -Importing the akka.zeromq._ package ensures that the implicit zeromq method is available. -Similarly you can create a subscription socket, that subscribes to all messages from the publisher using: +.. includecode:: code/akka/docs/zeromq/ZeromqDocSpec.scala#pub-socket2 -.. code-block:: scala - val socket = system.zeromq.newSocket(SocketType.Sub, Connect("tcp://127.0.0.1:1234"), SubscribeAll) +Above examples will create a ZeroMQ Publisher socket that is Bound to the port 1234 on localhost. -Also, a socket may be created with a listener that handles received messages as well as notifications: +Similarly you can create a subscription socket, with a listener, that subscribes to all messages from the publisher using: -.. code-block:: scala - - val listener = system.actorOf(Props(new Actor { - def receive: Receive = { - case Connecting => ... - case _ => ... - } - })) - val socket = system.zeromq.newSocket(SocketType.Router, Listener(listener), Connect("tcp://localhost:1234")) +.. includecode:: code/akka/docs/zeromq/ZeromqDocSpec.scala#sub-socket The following sub-sections describe the supported connection patterns and how they can be used in an Akka environment. However, for a comprehensive discussion of connection patterns, please refer to `ZeroMQ -- The Guide `_. Publisher-subscriber connection -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +------------------------------- -In a publisher-subscriber (pub-sub) connection, the publisher accepts one or more subscribers. Each subscriber shall subscribe to one or more topics, whereas the publisher publishes messages to a set of topics. Also, a subscriber can subscribe to all available topics. +In a publisher-subscriber (pub-sub) connection, the publisher accepts one or more subscribers. Each subscriber shall +subscribe to one or more topics, whereas the publisher publishes messages to a set of topics. Also, a subscriber can +subscribe to all available topics. In an Akka environment, pub-sub connections shall be used when an actor sends messages +to one or more actors that do not interact with the actor that sent the message. When you're using zeromq pub/sub you should be aware that it needs multicast - check your cloud - to work properly and that the filtering of events for topics happens client side, so all events are always broadcasted to every subscriber. An actor is subscribed to a topic as follows: -.. code-block:: scala +.. includecode:: code/akka/docs/zeromq/ZeromqDocSpec.scala#sub-topic-socket - val socket = system.zeromq.newSocket(SocketType.Sub, Listener(listener), Connect("tcp://localhost:1234"), Subscribe("the-topic")) +It is a prefix match so it is subscribed to all topics starting with ``foo.bar``. Note that if the given string is empty or +``SubscribeAll`` is used, the actor is subscribed to all topics. -Note that if the given string is empty (see below), the actor is subscribed to all topics. To unsubscribe from a topic you do the following: +To unsubscribe from a topic you do the following: -.. code-block:: scala +.. includecode:: code/akka/docs/zeromq/ZeromqDocSpec.scala#unsub-topic-socket - socket ! Unsubscribe("SomeTopic1") +To publish messages to a topic you must use two Frames with the topic in the first frame. -In an Akka environment, pub-sub connections shall be used when an actor sends messages to one or more actors that do not interact with the actor that sent the message. The following piece of code creates a publisher actor, binds the socket, and sends a message to be published: +.. includecode:: code/akka/docs/zeromq/ZeromqDocSpec.scala#pub-topic -.. code-block:: scala +Pub-Sub in Action +^^^^^^^^^^^^^^^^^ - import akka.zeromq._ - val socket = system.zeromq.newSocket(SocketType.Pub, Bind("tcp://127.0.0.1:1234")) - socket ! Send("hello".getBytes) +The following example illustrates one publisher with two subscribers. -In the following code, the subscriber is configured to receive messages for all topics: +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. -.. code-block:: scala +.. includecode:: code/akka/docs/zeromq/ZeromqDocSpec.scala#health - import akka.zeromq._ - val listener = system.actorOf(Props(new Actor { - def receive: Receive = { - case Connecting => ... - case _ => ... - } - })) - val socket = system.zeromq.newSocket(SocketType.Sub, Listener(listener), Connect("tcp://127.0.0.1:1234"), SubscribeAll) +Let's add one subscriber that logs the information. It subscribes to all topics starting with ``"health"``, i.e. both ``Heap`` and +``Load`` events. + +.. includecode:: code/akka/docs/zeromq/ZeromqDocSpec.scala#logger + +Another subscriber keep track of used heap and warns if too much heap is used. It only subscribes to ``Heap`` events. + +.. includecode:: code/akka/docs/zeromq/ZeromqDocSpec.scala#alerter Router-Dealer connection -^^^^^^^^^^^^^^^^^^^^^^^^ +------------------------ While Pub/Sub is nice the real advantage of zeromq is that it is a "lego-box" for reliable messaging. And because there are so many integrations the multi-language support is fantastic. When you're using ZeroMQ to integrate many systems you'll probably need to build your own ZeroMQ devices. This is where the router and dealer socket types come in handy. @@ -96,19 +91,6 @@ With those socket types you can build your own reliable pub sub broker that uses To create a Router socket that has a high watermark configured, you would do: -.. code-block:: scala - - import akka.zeromq._ - val listener = system.actorOf(Props(new Actor { - def receive: Receive = { - case Connecting => ... - case _ => ... - } - })) - val socket = system.zeromq.newSocket( - SocketType.Router, - Listener(listener), - Bind("tcp://127.0.0.1:1234"), - HWM(50000)) +.. includecode:: code/akka/docs/zeromq/ZeromqDocSpec.scala#high-watermark -The akka-zeromq module accepts most if not all the available configuration options for a zeromq socket. \ No newline at end of file +The akka-zeromq module accepts most if not all the available configuration options for a zeromq socket. diff --git a/akka-durable-mailboxes/akka-beanstalk-mailbox/src/main/scala/akka/actor/mailbox/BeanstalkBasedMailbox.scala b/akka-durable-mailboxes/akka-beanstalk-mailbox/src/main/scala/akka/actor/mailbox/BeanstalkBasedMailbox.scala index cd2ca2d0f0..489d97d176 100644 --- a/akka-durable-mailboxes/akka-beanstalk-mailbox/src/main/scala/akka/actor/mailbox/BeanstalkBasedMailbox.scala +++ b/akka-durable-mailboxes/akka-beanstalk-mailbox/src/main/scala/akka/actor/mailbox/BeanstalkBasedMailbox.scala @@ -22,7 +22,7 @@ class BeanstalkBasedMailboxType(config: Config) extends MailboxType { /** * @author Jonas Bonér */ -class BeanstalkBasedMailbox(val owner: ActorContext) extends DurableMailbox(owner) with DurableMessageSerialization { +class BeanstalkBasedMailbox(_owner: ActorContext) extends DurableMailbox(_owner) with DurableMessageSerialization { private val settings = BeanstalkBasedMailboxExtension(owner.system) private val messageSubmitDelaySeconds = settings.MessageSubmitDelay.toSeconds.toInt diff --git a/akka-durable-mailboxes/akka-beanstalk-mailbox/src/test/scala/akka/actor/mailbox/BeanstalkBasedMailboxSpec.scala b/akka-durable-mailboxes/akka-beanstalk-mailbox/src/test/scala/akka/actor/mailbox/BeanstalkBasedMailboxSpec.scala index f7ed2e71ac..4eb370ab63 100644 --- a/akka-durable-mailboxes/akka-beanstalk-mailbox/src/test/scala/akka/actor/mailbox/BeanstalkBasedMailboxSpec.scala +++ b/akka-durable-mailboxes/akka-beanstalk-mailbox/src/test/scala/akka/actor/mailbox/BeanstalkBasedMailboxSpec.scala @@ -3,7 +3,7 @@ package akka.actor.mailbox object BeanstalkBasedMailboxSpec { val config = """ Beanstalkd-dispatcher { - mailboxType = akka.actor.mailbox.BeanstalkBasedMailboxType + mailbox-type = akka.actor.mailbox.BeanstalkBasedMailboxType throughput = 1 } """ diff --git a/akka-durable-mailboxes/akka-file-mailbox/src/main/scala/akka/actor/mailbox/FiledBasedMailbox.scala b/akka-durable-mailboxes/akka-file-mailbox/src/main/scala/akka/actor/mailbox/FiledBasedMailbox.scala index 8da17a4cc9..ccdbdc4145 100644 --- a/akka-durable-mailboxes/akka-file-mailbox/src/main/scala/akka/actor/mailbox/FiledBasedMailbox.scala +++ b/akka-durable-mailboxes/akka-file-mailbox/src/main/scala/akka/actor/mailbox/FiledBasedMailbox.scala @@ -17,7 +17,7 @@ class FileBasedMailboxType(config: Config) extends MailboxType { override def create(owner: ActorContext) = new FileBasedMailbox(owner) } -class FileBasedMailbox(val owner: ActorContext) extends DurableMailbox(owner) with DurableMessageSerialization { +class FileBasedMailbox(_owner: ActorContext) extends DurableMailbox(_owner) with DurableMessageSerialization { val log = Logging(system, "FileBasedMailbox") diff --git a/akka-durable-mailboxes/akka-file-mailbox/src/test/scala/akka/actor/mailbox/FileBasedMailboxSpec.scala b/akka-durable-mailboxes/akka-file-mailbox/src/test/scala/akka/actor/mailbox/FileBasedMailboxSpec.scala index 3f202ddc5a..274bc36cc1 100644 --- a/akka-durable-mailboxes/akka-file-mailbox/src/test/scala/akka/actor/mailbox/FileBasedMailboxSpec.scala +++ b/akka-durable-mailboxes/akka-file-mailbox/src/test/scala/akka/actor/mailbox/FileBasedMailboxSpec.scala @@ -5,7 +5,7 @@ import org.apache.commons.io.FileUtils object FileBasedMailboxSpec { val config = """ File-dispatcher { - mailboxType = akka.actor.mailbox.FileBasedMailboxType + mailbox-type = akka.actor.mailbox.FileBasedMailboxType throughput = 1 } """ diff --git a/akka-durable-mailboxes/akka-mailboxes-common/src/main/scala/akka/actor/mailbox/DurableMailbox.scala b/akka-durable-mailboxes/akka-mailboxes-common/src/main/scala/akka/actor/mailbox/DurableMailbox.scala index 5e319dafac..69f7fb50c1 100644 --- a/akka-durable-mailboxes/akka-mailboxes-common/src/main/scala/akka/actor/mailbox/DurableMailbox.scala +++ b/akka-durable-mailboxes/akka-mailboxes-common/src/main/scala/akka/actor/mailbox/DurableMailbox.scala @@ -12,7 +12,7 @@ private[akka] object DurableExecutableMailboxConfig { val Name = "[\\.\\/\\$\\s]".r } -abstract class DurableMailbox(owner: ActorContext) extends CustomMailbox(owner) with DefaultSystemMessageQueue { +abstract class DurableMailbox(val owner: ActorContext) extends CustomMailbox(owner) with DefaultSystemMessageQueue { import DurableExecutableMailboxConfig._ def system: ExtendedActorSystem = owner.system.asInstanceOf[ExtendedActorSystem] @@ -22,15 +22,13 @@ abstract class DurableMailbox(owner: ActorContext) extends CustomMailbox(owner) } -trait DurableMessageSerialization { - - def owner: ActorContext +trait DurableMessageSerialization { this: DurableMailbox ⇒ def serialize(durableMessage: Envelope): Array[Byte] = { def serializeActorRef(ref: ActorRef): ActorRefProtocol = ActorRefProtocol.newBuilder.setPath(ref.path.toString).build - val message = MessageSerializer.serialize(owner.system, durableMessage.message.asInstanceOf[AnyRef]) + val message = MessageSerializer.serialize(system, durableMessage.message.asInstanceOf[AnyRef]) val builder = RemoteMessageProtocol.newBuilder .setMessage(message) .setRecipient(serializeActorRef(owner.self)) @@ -41,13 +39,13 @@ trait DurableMessageSerialization { def deserialize(bytes: Array[Byte]): Envelope = { - def deserializeActorRef(refProtocol: ActorRefProtocol): ActorRef = owner.system.actorFor(refProtocol.getPath) + def deserializeActorRef(refProtocol: ActorRefProtocol): ActorRef = system.actorFor(refProtocol.getPath) val durableMessage = RemoteMessageProtocol.parseFrom(bytes) - val message = MessageSerializer.deserialize(owner.system, durableMessage.getMessage, getClass.getClassLoader) + val message = MessageSerializer.deserialize(system, durableMessage.getMessage) val sender = deserializeActorRef(durableMessage.getSender) - new Envelope(message, sender)(owner.system) + new Envelope(message, sender)(system) } } diff --git a/akka-durable-mailboxes/akka-mongo-mailbox/src/main/scala/akka/actor/mailbox/BSONSerialization.scala b/akka-durable-mailboxes/akka-mongo-mailbox/src/main/scala/akka/actor/mailbox/BSONSerialization.scala index cc36286e36..5aa314eb55 100644 --- a/akka-durable-mailboxes/akka-mongo-mailbox/src/main/scala/akka/actor/mailbox/BSONSerialization.scala +++ b/akka-durable-mailboxes/akka-mongo-mailbox/src/main/scala/akka/actor/mailbox/BSONSerialization.scala @@ -65,7 +65,7 @@ class BSONSerializableMailbox(system: ExtendedActorSystem) extends SerializableB val doc = deserializer.decodeAndFetch(in).asInstanceOf[BSONDocument] system.log.debug("Deserializing a durable message from MongoDB: {}", doc) val msgData = MessageProtocol.parseFrom(doc.as[org.bson.types.Binary]("message").getData) - val msg = MessageSerializer.deserialize(system, msgData, system.internalClassLoader) + val msg = MessageSerializer.deserialize(system, msgData) val ownerPath = doc.as[String]("ownerPath") val senderPath = doc.as[String]("senderPath") val sender = system.actorFor(senderPath) diff --git a/akka-durable-mailboxes/akka-mongo-mailbox/src/main/scala/akka/actor/mailbox/MongoBasedMailbox.scala b/akka-durable-mailboxes/akka-mongo-mailbox/src/main/scala/akka/actor/mailbox/MongoBasedMailbox.scala index fed643c7d1..d17a1221a8 100644 --- a/akka-durable-mailboxes/akka-mongo-mailbox/src/main/scala/akka/actor/mailbox/MongoBasedMailbox.scala +++ b/akka-durable-mailboxes/akka-mongo-mailbox/src/main/scala/akka/actor/mailbox/MongoBasedMailbox.scala @@ -32,7 +32,7 @@ class MongoBasedMailboxType(config: Config) extends MailboxType { * * @author Brendan W. McAdams */ -class MongoBasedMailbox(val owner: ActorContext) extends DurableMailbox(owner) { +class MongoBasedMailbox(_owner: ActorContext) extends DurableMailbox(_owner) { // this implicit object provides the context for reading/writing things as MongoDurableMessage implicit val mailboxBSONSer = new BSONSerializableMailbox(system) implicit val safeWrite = WriteConcern.Safe // TODO - Replica Safe when appropriate! diff --git a/akka-durable-mailboxes/akka-mongo-mailbox/src/test/scala/akka/actor/mailbox/MongoBasedMailboxSpec.scala b/akka-durable-mailboxes/akka-mongo-mailbox/src/test/scala/akka/actor/mailbox/MongoBasedMailboxSpec.scala index 16fcde321e..7001d8de99 100644 --- a/akka-durable-mailboxes/akka-mongo-mailbox/src/test/scala/akka/actor/mailbox/MongoBasedMailboxSpec.scala +++ b/akka-durable-mailboxes/akka-mongo-mailbox/src/test/scala/akka/actor/mailbox/MongoBasedMailboxSpec.scala @@ -12,7 +12,7 @@ import akka.dispatch.MessageDispatcher object MongoBasedMailboxSpec { val config = """ mongodb-dispatcher { - mailboxType = akka.actor.mailbox.MongoBasedMailboxType + mailbox-type = akka.actor.mailbox.MongoBasedMailboxType throughput = 1 } """ diff --git a/akka-durable-mailboxes/akka-redis-mailbox/src/main/scala/akka/actor/mailbox/RedisBasedMailbox.scala b/akka-durable-mailboxes/akka-redis-mailbox/src/main/scala/akka/actor/mailbox/RedisBasedMailbox.scala index 5d4e09e65e..b6cf3febc6 100644 --- a/akka-durable-mailboxes/akka-redis-mailbox/src/main/scala/akka/actor/mailbox/RedisBasedMailbox.scala +++ b/akka-durable-mailboxes/akka-redis-mailbox/src/main/scala/akka/actor/mailbox/RedisBasedMailbox.scala @@ -19,7 +19,7 @@ class RedisBasedMailboxType(config: Config) extends MailboxType { override def create(owner: ActorContext) = new RedisBasedMailbox(owner) } -class RedisBasedMailbox(val owner: ActorContext) extends DurableMailbox(owner) with DurableMessageSerialization { +class RedisBasedMailbox(_owner: ActorContext) extends DurableMailbox(_owner) with DurableMessageSerialization { private val settings = RedisBasedMailboxExtension(owner.system) diff --git a/akka-durable-mailboxes/akka-redis-mailbox/src/test/scala/akka/actor/mailbox/RedisBasedMailboxSpec.scala b/akka-durable-mailboxes/akka-redis-mailbox/src/test/scala/akka/actor/mailbox/RedisBasedMailboxSpec.scala index 15bad81d2f..6e78d6b74a 100644 --- a/akka-durable-mailboxes/akka-redis-mailbox/src/test/scala/akka/actor/mailbox/RedisBasedMailboxSpec.scala +++ b/akka-durable-mailboxes/akka-redis-mailbox/src/test/scala/akka/actor/mailbox/RedisBasedMailboxSpec.scala @@ -3,7 +3,7 @@ package akka.actor.mailbox object RedisBasedMailboxSpec { val config = """ Redis-dispatcher { - mailboxType = akka.actor.mailbox.RedisBasedMailboxType + mailbox-type = akka.actor.mailbox.RedisBasedMailboxType throughput = 1 } """ diff --git a/akka-durable-mailboxes/akka-zookeeper-mailbox/src/main/scala/akka/actor/mailbox/ZooKeeperBasedMailbox.scala b/akka-durable-mailboxes/akka-zookeeper-mailbox/src/main/scala/akka/actor/mailbox/ZooKeeperBasedMailbox.scala index 81e4c378eb..90fd381af1 100644 --- a/akka-durable-mailboxes/akka-zookeeper-mailbox/src/main/scala/akka/actor/mailbox/ZooKeeperBasedMailbox.scala +++ b/akka-durable-mailboxes/akka-zookeeper-mailbox/src/main/scala/akka/actor/mailbox/ZooKeeperBasedMailbox.scala @@ -20,7 +20,7 @@ class ZooKeeperBasedMailboxType(config: Config) extends MailboxType { override def create(owner: ActorContext) = new ZooKeeperBasedMailbox(owner) } -class ZooKeeperBasedMailbox(val owner: ActorContext) extends DurableMailbox(owner) with DurableMessageSerialization { +class ZooKeeperBasedMailbox(_owner: ActorContext) extends DurableMailbox(_owner) with DurableMessageSerialization { private val settings = ZooKeeperBasedMailboxExtension(owner.system) val queueNode = "/queues" diff --git a/akka-durable-mailboxes/akka-zookeeper-mailbox/src/test/scala/akka/actor/mailbox/ZooKeeperBasedMailboxSpec.scala b/akka-durable-mailboxes/akka-zookeeper-mailbox/src/test/scala/akka/actor/mailbox/ZooKeeperBasedMailboxSpec.scala index 4febbafe6f..9264fbccce 100644 --- a/akka-durable-mailboxes/akka-zookeeper-mailbox/src/test/scala/akka/actor/mailbox/ZooKeeperBasedMailboxSpec.scala +++ b/akka-durable-mailboxes/akka-zookeeper-mailbox/src/test/scala/akka/actor/mailbox/ZooKeeperBasedMailboxSpec.scala @@ -9,7 +9,7 @@ import akka.actor.ActorRef object ZooKeeperBasedMailboxSpec { val config = """ ZooKeeper-dispatcher { - mailboxType = akka.actor.mailbox.ZooKeeperBasedMailboxType + mailbox-type = akka.actor.mailbox.ZooKeeperBasedMailboxType throughput = 1 } """ diff --git a/akka-remote/src/main/scala/akka/remote/MessageSerializer.scala b/akka-remote/src/main/scala/akka/remote/MessageSerializer.scala index 5301f2bdd0..65777d49ca 100644 --- a/akka-remote/src/main/scala/akka/remote/MessageSerializer.scala +++ b/akka-remote/src/main/scala/akka/remote/MessageSerializer.scala @@ -6,32 +6,25 @@ package akka.remote import akka.remote.RemoteProtocol._ import com.google.protobuf.ByteString -import akka.actor.ActorSystem +import akka.actor.ExtendedActorSystem import akka.serialization.SerializationExtension -import akka.util.ReflectiveAccess object MessageSerializer { - def deserialize(system: ActorSystem, messageProtocol: MessageProtocol, classLoader: ClassLoader): AnyRef = { - val clazz = if (messageProtocol.hasMessageManifest) { - Option(ReflectiveAccess.getClassFor[AnyRef]( - messageProtocol.getMessageManifest.toStringUtf8, - classLoader) match { - case Left(e) ⇒ throw e - case Right(r) ⇒ r - }) - } else None - SerializationExtension(system).deserialize( - messageProtocol.getMessage.toByteArray, - messageProtocol.getSerializerId, - clazz, - classLoader) match { + def deserialize(system: ExtendedActorSystem, messageProtocol: MessageProtocol): AnyRef = { + val clazz = + if (messageProtocol.hasMessageManifest) { + system.dynamicAccess.getClassFor[AnyRef](messageProtocol.getMessageManifest.toStringUtf8) + .fold(throw _, Some(_)) + } else None + SerializationExtension(system) + .deserialize(messageProtocol.getMessage.toByteArray, messageProtocol.getSerializerId, clazz) match { case Left(e) ⇒ throw e case Right(r) ⇒ r } } - def serialize(system: ActorSystem, message: AnyRef): MessageProtocol = { + def serialize(system: ExtendedActorSystem, message: AnyRef): MessageProtocol = { val s = SerializationExtension(system) val serializer = s.findSerializerFor(message) val builder = MessageProtocol.newBuilder diff --git a/akka-remote/src/main/scala/akka/remote/RemoteActorRefProvider.scala b/akka-remote/src/main/scala/akka/remote/RemoteActorRefProvider.scala index 872be5aa41..89c35115ef 100644 --- a/akka-remote/src/main/scala/akka/remote/RemoteActorRefProvider.scala +++ b/akka-remote/src/main/scala/akka/remote/RemoteActorRefProvider.scala @@ -12,7 +12,6 @@ import akka.event.EventStream import akka.config.ConfigurationException import java.util.concurrent.{ TimeoutException } import com.typesafe.config.Config -import akka.util.ReflectiveAccess import akka.serialization.Serialization import akka.serialization.SerializationExtension @@ -28,11 +27,11 @@ class RemoteActorRefProvider( val settings: ActorSystem.Settings, val eventStream: EventStream, val scheduler: Scheduler, - val classloader: ClassLoader) extends ActorRefProvider { + val dynamicAccess: DynamicAccess) extends ActorRefProvider { val remoteSettings = new RemoteSettings(settings.config, systemName) - val deployer = new RemoteDeployer(settings, classloader) + val deployer = new RemoteDeployer(settings, dynamicAccess) private val local = new LocalActorRefProvider(systemName, settings, eventStream, scheduler, deployer) @@ -84,7 +83,7 @@ class RemoteActorRefProvider( classOf[ActorSystemImpl] -> system, classOf[RemoteActorRefProvider] -> this) - ReflectiveAccess.createInstance[RemoteTransport](fqn, args, system.internalClassLoader) match { + system.dynamicAccess.createInstanceFor[RemoteTransport](fqn, args) match { case Left(problem) ⇒ throw new RemoteTransportException("Could not load remote transport layer " + fqn, problem) case Right(remote) ⇒ remote } diff --git a/akka-remote/src/main/scala/akka/remote/RemoteDeployer.scala b/akka-remote/src/main/scala/akka/remote/RemoteDeployer.scala index 9686d295ad..1e1068cd5f 100644 --- a/akka-remote/src/main/scala/akka/remote/RemoteDeployer.scala +++ b/akka-remote/src/main/scala/akka/remote/RemoteDeployer.scala @@ -12,7 +12,7 @@ case class RemoteScope(node: Address) extends Scope { def withFallback(other: Scope): Scope = this } -class RemoteDeployer(_settings: ActorSystem.Settings, _classloader: ClassLoader) extends Deployer(_settings, _classloader) { +class RemoteDeployer(_settings: ActorSystem.Settings, _pm: DynamicAccess) extends Deployer(_settings, _pm) { override protected def parseConfig(path: String, config: Config): Option[Deploy] = { import scala.collection.JavaConverters._ diff --git a/akka-remote/src/main/scala/akka/remote/RemoteTransport.scala b/akka-remote/src/main/scala/akka/remote/RemoteTransport.scala index b40992d12a..256451bc0a 100644 --- a/akka-remote/src/main/scala/akka/remote/RemoteTransport.scala +++ b/akka-remote/src/main/scala/akka/remote/RemoteTransport.scala @@ -219,7 +219,7 @@ class RemoteMessage(input: RemoteMessageProtocol, system: ActorSystemImpl) { lazy val recipient: InternalActorRef = system.provider.actorFor(system.provider.rootGuardian, originalReceiver) - lazy val payload: AnyRef = MessageSerializer.deserialize(system, input.getMessage, getClass.getClassLoader) + lazy val payload: AnyRef = MessageSerializer.deserialize(system, input.getMessage) override def toString = "RemoteMessage: " + payload + " to " + recipient + "<+{" + originalReceiver + "} from " + sender } diff --git a/akka-remote/src/main/scala/akka/remote/netty/Settings.scala b/akka-remote/src/main/scala/akka/remote/netty/Settings.scala index 0db6cabf18..daa91a3014 100644 --- a/akka-remote/src/main/scala/akka/remote/netty/Settings.scala +++ b/akka-remote/src/main/scala/akka/remote/netty/Settings.scala @@ -40,7 +40,7 @@ class NettySettings(config: Config, val systemName: String) { case value ⇒ value } - @deprecated("WARNING: This should only be used by professionals.") + @deprecated("WARNING: This should only be used by professionals.", "2.0") val PortSelector = getInt("port") val ConnectionTimeout = Duration(getMilliseconds("connection-timeout"), MILLISECONDS) diff --git a/akka-remote/src/main/scala/akka/routing/RemoteRouterConfig.scala b/akka-remote/src/main/scala/akka/routing/RemoteRouterConfig.scala index 21f5c400b0..3b1791db8e 100644 --- a/akka-remote/src/main/scala/akka/routing/RemoteRouterConfig.scala +++ b/akka-remote/src/main/scala/akka/routing/RemoteRouterConfig.scala @@ -30,6 +30,8 @@ case class RemoteRouterConfig(local: RouterConfig, nodes: Iterable[String]) exte override def createActor(): Router = local.createActor() + override def routerDispatcher: String = local.routerDispatcher + override def resizer: Option[Resizer] = local.resizer override def withFallback(other: RouterConfig): RouterConfig = other match { diff --git a/akka-remote/src/main/scala/akka/serialization/ProtobufSerializer.scala b/akka-remote/src/main/scala/akka/serialization/ProtobufSerializer.scala index f733e8188f..813a22fba4 100644 --- a/akka-remote/src/main/scala/akka/serialization/ProtobufSerializer.scala +++ b/akka-remote/src/main/scala/akka/serialization/ProtobufSerializer.scala @@ -5,6 +5,7 @@ package akka.serialization import com.google.protobuf.Message +import akka.actor.DynamicAccess /** * This Serializer serializes `com.google.protobuf.Message`s @@ -19,7 +20,7 @@ class ProtobufSerializer extends Serializer { case _ ⇒ throw new IllegalArgumentException("Can't serialize a non-protobuf message using protobuf [" + obj + "]") } - def fromBinary(bytes: Array[Byte], clazz: Option[Class[_]], classLoader: Option[ClassLoader] = None): AnyRef = + def fromBinary(bytes: Array[Byte], clazz: Option[Class[_]]): AnyRef = clazz match { case None ⇒ throw new IllegalArgumentException("Need a protobuf message class to be able to serialize bytes using protobuf") case Some(c) ⇒ c.getDeclaredMethod("parseFrom", ARRAY_OF_BYTE_ARRAY: _*).invoke(null, bytes).asInstanceOf[Message] diff --git a/akka-testkit/src/main/scala/akka/testkit/TestActorRef.scala b/akka-testkit/src/main/scala/akka/testkit/TestActorRef.scala index d42cfcf165..8769fdda51 100644 --- a/akka-testkit/src/main/scala/akka/testkit/TestActorRef.scala +++ b/akka-testkit/src/main/scala/akka/testkit/TestActorRef.scala @@ -5,7 +5,7 @@ package akka.testkit import akka.actor._ -import akka.util.{ ReflectiveAccess, Duration } +import akka.util.Duration import java.util.concurrent.atomic.AtomicLong import scala.collection.immutable.Stack import akka.dispatch._ @@ -121,8 +121,7 @@ object TestActorRef { def apply[T <: Actor](implicit m: Manifest[T], system: ActorSystem): TestActorRef[T] = apply[T](randomName) def apply[T <: Actor](name: String)(implicit m: Manifest[T], system: ActorSystem): TestActorRef[T] = apply[T](Props({ - import ReflectiveAccess.{ createInstance, noParams, noArgs } - createInstance[T](m.erasure, noParams, noArgs) match { + system.asInstanceOf[ExtendedActorSystem].dynamicAccess.createInstanceFor[T](m.erasure, Seq()) match { case Right(value) ⇒ value case Left(exception) ⇒ throw new ActorInitializationException(null, "Could not instantiate Actor" + diff --git a/akka-zeromq/src/main/scala/akka/zeromq/SocketOption.scala b/akka-zeromq/src/main/scala/akka/zeromq/SocketOption.scala index 9cded84da1..d3b824bca1 100644 --- a/akka-zeromq/src/main/scala/akka/zeromq/SocketOption.scala +++ b/akka-zeromq/src/main/scala/akka/zeromq/SocketOption.scala @@ -174,9 +174,12 @@ private[zeromq] case object Close extends Request * * @param payload the topic to subscribe to */ -case class Subscribe(payload: Seq[Byte]) extends PubSubOption +case class Subscribe(payload: Seq[Byte]) extends PubSubOption { + def this(topic: String) = this(topic.getBytes("UTF-8")) +} object Subscribe { - def apply(topic: String): Subscribe = new Subscribe(topic.getBytes) + def apply(topic: String): Subscribe = new Subscribe(topic) + val all = Subscribe(Seq.empty) } /** @@ -188,9 +191,11 @@ object Subscribe { * * @param payload */ -case class Unsubscribe(payload: Seq[Byte]) extends PubSubOption +case class Unsubscribe(payload: Seq[Byte]) extends PubSubOption { + def this(topic: String) = this(topic.getBytes("UTF-8")) +} object Unsubscribe { - def apply(topic: String): Unsubscribe = Unsubscribe(topic.getBytes) + def apply(topic: String): Unsubscribe = new Unsubscribe(topic) } /** @@ -204,7 +209,21 @@ case class Send(frames: Seq[Frame]) extends Request * @param frames */ case class ZMQMessage(frames: Seq[Frame]) { - def firstFrameAsString = new String(frames.head.payload.toArray) + + def this(frame: Frame) = this(Seq(frame)) + def this(frame1: Frame, frame2: Frame) = this(Seq(frame1, frame2)) + def this(frameArray: Array[Frame]) = this(frameArray.toSeq) + + /** + * Convert the bytes in the first frame to a String, using specified charset. + */ + def firstFrameAsString(charsetName: String): String = new String(frames.head.payload.toArray, charsetName) + /** + * Convert the bytes in the first frame to a String, using "UTF-8" charset. + */ + def firstFrameAsString: String = firstFrameAsString("UTF-8") + + def payload(frameIndex: Int): Array[Byte] = frames(frameIndex).payload.toArray } object ZMQMessage { def apply(bytes: Array[Byte]): ZMQMessage = ZMQMessage(Seq(Frame(bytes))) diff --git a/akka-zeromq/src/main/scala/akka/zeromq/ZMQMessageDeserializer.scala b/akka-zeromq/src/main/scala/akka/zeromq/ZMQMessageDeserializer.scala index 470c98617a..1776f21211 100644 --- a/akka-zeromq/src/main/scala/akka/zeromq/ZMQMessageDeserializer.scala +++ b/akka-zeromq/src/main/scala/akka/zeromq/ZMQMessageDeserializer.scala @@ -3,11 +3,18 @@ */ package akka.zeromq +object Frame { + def apply(text: String): Frame = new Frame(text) +} + /** * A single message frame of a zeromq message * @param payload */ -case class Frame(payload: Seq[Byte]) +case class Frame(payload: Seq[Byte]) { + def this(bytes: Array[Byte]) = this(bytes.toSeq) + def this(text: String) = this(text.getBytes("UTF-8")) +} /** * Deserializes ZeroMQ messages into an immutable sequence of frames diff --git a/akka-zeromq/src/main/scala/akka/zeromq/ZeroMQExtension.scala b/akka-zeromq/src/main/scala/akka/zeromq/ZeroMQExtension.scala index 20dbb0724a..7ae178291f 100644 --- a/akka-zeromq/src/main/scala/akka/zeromq/ZeroMQExtension.scala +++ b/akka-zeromq/src/main/scala/akka/zeromq/ZeroMQExtension.scala @@ -22,6 +22,7 @@ case class ZeroMQVersion(major: Int, minor: Int, patch: Int) { * The [[akka.actor.ExtensionId]] and [[akka.actor.ExtensionIdProvider]] for the ZeroMQ module */ object ZeroMQExtension extends ExtensionId[ZeroMQExtension] with ExtensionIdProvider { + override def get(system: ActorSystem): ZeroMQExtension = super.get(system) def lookup() = this def createExtension(system: ExtendedActorSystem) = new ZeroMQExtension(system) @@ -141,92 +142,94 @@ class ZeroMQExtension(system: ActorSystem) extends Extension { } /** - * Java API helper - * Factory method to create the actor representing the ZeroMQ Publisher socket. + * Java API factory method to create the actor representing the ZeroMQ Publisher socket. * You can pass in as many configuration options as you want and the order of the configuration options doesn't matter * They are matched on type and the first one found wins. * - * @param socketParameters a varargs list of [[akka.zeromq.SocketOption]] to configure the socke + * @param socketParameters array of [[akka.zeromq.SocketOption]] to configure the socket * @return the [[akka.actor.ActorRef]] */ - def newPubSocket(socketParameters: SocketOption*): ActorRef = newSocket((SocketType.Pub +: socketParameters): _*) + def newPubSocket(socketParameters: Array[SocketOption]): ActorRef = newSocket((SocketType.Pub +: socketParameters): _*) /** - * Java API helper - * Factory method to create the actor representing the ZeroMQ Subscriber socket. - * You can pass in as many configuration options as you want and the order of the configuration options doesn't matter - * They are matched on type and the first one found wins. - * - * @param socketParameters a varargs list of [[akka.zeromq.SocketOption]] to configure the socke - * @return the [[akka.actor.ActorRef]] + * Convenience for creating a publisher socket. */ - def newSubSocket(socketParameters: SocketOption*): ActorRef = newSocket((SocketType.Sub +: socketParameters): _*) + def newPubSocket(bind: Bind): ActorRef = newSocket(SocketType.Pub, bind) /** - * Java API helper - * Factory method to create the actor representing the ZeroMQ Dealer socket. + * Java API factory method to create the actor representing the ZeroMQ Subscriber socket. * You can pass in as many configuration options as you want and the order of the configuration options doesn't matter * They are matched on type and the first one found wins. * - * @param socketParameters a varargs list of [[akka.zeromq.SocketOption]] to configure the socke + * @param socketParameters array of [[akka.zeromq.SocketOption]] to configure the socket * @return the [[akka.actor.ActorRef]] */ - def newDealerSocket(socketParameters: SocketOption*): ActorRef = newSocket((SocketType.Dealer +: socketParameters): _*) + def newSubSocket(socketParameters: Array[SocketOption]): ActorRef = newSocket((SocketType.Sub +: socketParameters): _*) /** - * Java API helper - * Factory method to create the actor representing the ZeroMQ Router socket. - * You can pass in as many configuration options as you want and the order of the configuration options doesn't matter - * They are matched on type and the first one found wins. - * - * @param socketParameters a varargs list of [[akka.zeromq.SocketOption]] to configure the socke - * @return the [[akka.actor.ActorRef]] + * Convenience for creating a subscriber socket. */ - def newRouterSocket(socketParameters: SocketOption*): ActorRef = newSocket((SocketType.Router +: socketParameters): _*) + def newSubSocket(connect: Connect, listener: Listener, subscribe: Subscribe): ActorRef = newSocket(SocketType.Sub, connect, listener, subscribe) /** - * Java API helper - * Factory method to create the actor representing the ZeroMQ Push socket. + * Java API factory method to create the actor representing the ZeroMQ Dealer socket. * You can pass in as many configuration options as you want and the order of the configuration options doesn't matter * They are matched on type and the first one found wins. * - * @param socketParameters a varargs list of [[akka.zeromq.SocketOption]] to configure the socke + * @param socketParameters array of [[akka.zeromq.SocketOption]] to configure the socket * @return the [[akka.actor.ActorRef]] */ - def newPushSocket(socketParameters: SocketOption*): ActorRef = newSocket((SocketType.Push +: socketParameters): _*) + def newDealerSocket(socketParameters: Array[SocketOption]): ActorRef = newSocket((SocketType.Dealer +: socketParameters): _*) /** - * Java API helper - * Factory method to create the actor representing the ZeroMQ Pull socket. + * Java API factory method to create the actor representing the ZeroMQ Router socket. * You can pass in as many configuration options as you want and the order of the configuration options doesn't matter * They are matched on type and the first one found wins. * - * @param socketParameters a varargs list of [[akka.zeromq.SocketOption]] to configure the socke + * @param socketParameters array of [[akka.zeromq.SocketOption]] to configure the socket * @return the [[akka.actor.ActorRef]] */ - def newPullSocket(socketParameters: SocketOption*): ActorRef = newSocket((SocketType.Pull +: socketParameters): _*) + def newRouterSocket(socketParameters: Array[SocketOption]): ActorRef = newSocket((SocketType.Router +: socketParameters): _*) /** - * Java API helper - * Factory method to create the actor representing the ZeroMQ Req socket. + * Java API factory method to create the actor representing the ZeroMQ Push socket. * You can pass in as many configuration options as you want and the order of the configuration options doesn't matter * They are matched on type and the first one found wins. * - * @param socketParameters a varargs list of [[akka.zeromq.SocketOption]] to configure the socke + * @param socketParameters array of [[akka.zeromq.SocketOption]] to configure the socket * @return the [[akka.actor.ActorRef]] */ - def newReqSocket(socketParameters: SocketOption*): ActorRef = newSocket((SocketType.Req +: socketParameters): _*) + def newPushSocket(socketParameters: Array[SocketOption]): ActorRef = newSocket((SocketType.Push +: socketParameters): _*) /** - * Java API helper - * Factory method to create the actor representing the ZeroMQ Rep socket. + * Java API factory method to create the actor representing the ZeroMQ Pull socket. * You can pass in as many configuration options as you want and the order of the configuration options doesn't matter * They are matched on type and the first one found wins. * - * @param socketParameters a varargs list of [[akka.zeromq.SocketOption]] to configure the socke + * @param socketParameters array of [[akka.zeromq.SocketOption]] to configure the socket * @return the [[akka.actor.ActorRef]] */ - def newRepSocket(socketParameters: SocketOption*): ActorRef = newSocket((SocketType.Rep +: socketParameters): _*) + def newPullSocket(socketParameters: Array[SocketOption]): ActorRef = newSocket((SocketType.Pull +: socketParameters): _*) + + /** + * Java API factory method to create the actor representing the ZeroMQ Req socket. + * You can pass in as many configuration options as you want and the order of the configuration options doesn't matter + * They are matched on type and the first one found wins. + * + * @param socketParameters array of [[akka.zeromq.SocketOption]] to configure the socket + * @return the [[akka.actor.ActorRef]] + */ + def newReqSocket(socketParameters: Array[SocketOption]): ActorRef = newSocket((SocketType.Req +: socketParameters): _*) + + /** + * Java API factory method to create the actor representing the ZeroMQ Rep socket. + * You can pass in as many configuration options as you want and the order of the configuration options doesn't matter + * They are matched on type and the first one found wins. + * + * @param socketParameters array of [[akka.zeromq.SocketOption]] to configure the socke + * @return the [[akka.actor.ActorRef]] + */ + def newRepSocket(socketParameters: Array[SocketOption]): ActorRef = newSocket((SocketType.Rep +: socketParameters): _*) private val zeromqGuardian: ActorRef = { verifyZeroMQVersion diff --git a/akka-zeromq/src/test/scala/akka/zeromq/ConcurrentSocketActorSpec.scala b/akka-zeromq/src/test/scala/akka/zeromq/ConcurrentSocketActorSpec.scala index 7c498bd653..fe5b85b9dc 100644 --- a/akka-zeromq/src/test/scala/akka/zeromq/ConcurrentSocketActorSpec.scala +++ b/akka-zeromq/src/test/scala/akka/zeromq/ConcurrentSocketActorSpec.scala @@ -24,7 +24,8 @@ class ConcurrentSocketActorSpec "ConcurrentSocketActor" should { "support pub-sub connections" in { checkZeroMQInstallation - val (publisherProbe, subscriberProbe) = (TestProbe(), TestProbe()) + val publisherProbe = TestProbe() + val subscriberProbe = TestProbe() val context = Context() val publisher = newPublisher(context, publisherProbe.ref) val subscriber = newSubscriber(context, subscriberProbe.ref) @@ -68,7 +69,7 @@ class ConcurrentSocketActorSpec zmq.newSocket(SocketType.Pub, context, Listener(listener), Bind(endpoint)) } def newSubscriber(context: Context, listener: ActorRef) = { - zmq.newSocket(SocketType.Sub, context, Listener(listener), Connect(endpoint), Subscribe(Seq.empty)) + zmq.newSocket(SocketType.Sub, context, Listener(listener), Connect(endpoint), SubscribeAll) } def newMessageGenerator(actorRef: ActorRef) = { system.actorOf(Props(new MessageGeneratorActor(actorRef))) @@ -110,7 +111,7 @@ class ConcurrentSocketActorSpec protected def receive = { case _ ⇒ val payload = "%s".format(messageNumber) - messageNumber = messageNumber + 1 + messageNumber += 1 actorRef ! ZMQMessage(payload.getBytes) } } diff --git a/project/AkkaBuild.scala b/project/AkkaBuild.scala index 306aa38e8f..7ad4be2a9f 100644 --- a/project/AkkaBuild.scala +++ b/project/AkkaBuild.scala @@ -320,7 +320,8 @@ object AkkaBuild extends Build { lazy val docs = Project( id = "akka-docs", base = file("akka-docs"), - dependencies = Seq(actor, testkit % "test->test", remote, cluster, slf4j, agent, transactor, fileMailbox, mongoMailbox, redisMailbox, beanstalkMailbox, zookeeperMailbox), + dependencies = Seq(actor, testkit % "test->test", remote, cluster, slf4j, agent, transactor, + fileMailbox, mongoMailbox, redisMailbox, beanstalkMailbox, zookeeperMailbox, zeroMQ), settings = defaultSettings ++ Seq( unmanagedSourceDirectories in Test <<= baseDirectory { _ ** "code" get }, libraryDependencies ++= Dependencies.docs,