Created
January 10, 2014 02:04
-
-
Save ubourdon/8345778 to your computer and use it in GitHub Desktop.
Problem with Akka Actors that is when you use it, you must use ActorRef Scalatype. It means Akka Actors were not typed.
We can "solve" this problem using simpla "companion class" api.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AClassUsingActor(myClass: MyClass) { | |
def useActor = myClass.ref ! message | |
} | |
object CreateActorClass { | |
val system = ActorSystem("system") | |
val classUsingActor = new AClassUsingActor(new MyClass(system)) | |
classUsingActor.useActor | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor._ | |
// the class for use specific type instead of ActorRef | |
class MyClass(val ref: ActorRef) { | |
def this(system: ActorSystem) = this(system.actorOf(MyClass.props, MyClass.name)) | |
} | |
// companion object of Actor for define name & props | |
object MyClass { | |
val name = "myClass" | |
def props = Props[MyClassActor] | |
} | |
// the Actor | |
class MyClassActor extends Actor with ActorLogging { | |
def receive = ??? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment