Skip to content

Commit

Permalink
Add a serialization format to actor selections. Fixes #3748
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhopkins committed Feb 3, 2014
1 parent 7b25bd7 commit 6ab828b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,14 @@ class ActorSelectionSpec extends AkkaSpec("akka.loglevel=DEBUG") with DefaultTim
s"ActorSelection[Anchor(akka://ActorSelectionSpec/user/c2/c21#${c21.path.uid}), Path(/../*/hello)]")
}

"have a stringly serializable path" in {
system.actorSelection(system / "c2").toSerializationFormat should be("akka://ActorSelectionSpec/user/c2")
system.actorSelection(system / "c2" / "c21").toSerializationFormat should be("akka://ActorSelectionSpec/user/c2/c21")
ActorSelection(c2, "/").toSerializationFormat should be("akka://ActorSelectionSpec/user/c2")
ActorSelection(c2, "../*/hello").toSerializationFormat should be("akka://ActorSelectionSpec/user/c2/../*/hello")
ActorSelection(c2, "/../*/hello").toSerializationFormat should be("akka://ActorSelectionSpec/user/c2/../*/hello")
}

"send ActorSelection targeted to missing actor to deadLetters" in {
val p = TestProbe()
system.eventStream.subscribe(p.ref, classOf[DeadLetter])
Expand Down
21 changes: 21 additions & 0 deletions akka-actor/src/main/scala/akka/actor/ActorSelection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,27 @@ abstract class ActorSelection extends Serializable {
*/
def pathString: String = path.mkString("/", "/", "")

/**
* String representation of the actor selection suitable for storage and recreation.
* The output is similar to the URI fragment returned by [[akka.actor.ActorPath.toSerializationFormat]].
* @return URI fragment
*/
def toSerializationFormat: String = {
val anchorPath = anchor match {
case a: ActorRefWithCell anchor.path.toStringWithAddress(a.provider.getDefaultAddress)
case _ anchor.path.toString
}

val builder = new java.lang.StringBuilder()
builder.append(anchorPath)
val lastChar = builder.charAt(builder.length - 1)
if (path.nonEmpty && lastChar != '/')
builder.append(path.mkString("/", "/", ""))
else if (path.nonEmpty)
builder.append(path.mkString("/"))
builder.toString
}

override def equals(obj: Any): Boolean = obj match {
case s: ActorSelection this.anchor == s.anchor && this.path == s.path
case _ false
Expand Down
4 changes: 4 additions & 0 deletions akka-remote/src/test/scala/akka/remote/RemotingSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ class RemotingSpec extends AkkaSpec(RemotingSpec.cfg) with ImplicitSender with D
system.actorSelection(child.path / "*" / "grandgrandchild") ! Identify("idReq13")
expectMsg(ActorIdentity("idReq13", Some(grandgrandchild)))

val sel1 = system.actorSelection("/user/looker2/child/grandchild/grandgrandchild")
system.actorSelection(sel1.toSerializationFormat) ! Identify("idReq18")
expectMsg(ActorIdentity("idReq18", Some(grandgrandchild)))

child ! Identify("idReq14")
expectMsg(ActorIdentity("idReq14", Some(child)))
watch(child)
Expand Down

0 comments on commit 6ab828b

Please sign in to comment.