A constructor for a dynamically loaded actor?

102 views
Skip to first unread message

John Antypas

unread,
Jan 26, 2013, 7:50:49 PM1/26/13
to akka...@googlegroups.com
I'm actually starting to learn Akka -- some of us are a bit slow I suppose.... :-)

I finally figured out how I might load an actor stored in a jar, something like this....

def loadByClass(name : String) .... code = {
     val myClazz = theClassLoader.load(name)   // Using a load, get the class itself
     val myNewActor = context.system.actorOf(Props(myClazz).asInstanceOf(Actor)))
     ....
}

I'm trying to load these actors at runtime, and instantiate them into what will eventually be a pipeline pattern.    What'd really like then is a way to pass a set of parameters as the constructor -- the forward and back stages of the pipeline.   I'd like to do something like this:

def loadByClass(name : String, prev : ActorRef, next: ActorRef) .... code = {
     val myClazz = theClassLoader.load(name)   // Using a load, get the class itself
     val myNewActor = context.system.actorOf(Props(myClazz).asInstanceOf(ActorClass)(prev, next)))
     ....
}

Can I do this?   The alternatives are:

1. Actually call a method in the class to set parameters -- but the actor should be actor and I have no idea at runtime when, or for that matter, where given remote actors.   So that's probably a decidedly bad idea.
2. Send this actor a blocking message from its "partner" actors to set up the chain... that seems slightly better.

Any better way?

        

Patrik Nordwall

unread,
Jan 28, 2013, 5:23:10 AM1/28/13
to akka...@googlegroups.com
You might as well find this method useful:
system.asInstanceOf[ExtendedActorSystem].dynamicAccess.createInstanceFor
/Patrik


On Mon, Jan 28, 2013 at 2:03 AM, rey abe <r...@reyabe.com> wrote:

For passing arguments to a constructor of a dynamically loaded actor class, you can use Props.withCreator(c:Creator[Actor])
Just define your custom Creator, in your case something like:

class MyCreator(name:String, prev:ActorRef, next:ActorRef) extends Creator[Actor] {
  val myClazz = theClassLoader.load(name)
  override def create() = {
    val constructor = myClazz.getConstructor( classOf[ActorRef], classOf[ActorRef] )
    constructor.newInstance( prev, next ).asInstanceOf[Actor]
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://akka.io/faq/
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
 
 



--

Patrik Nordwall
Typesafe The software stack for applications that scale
Twitter: @patriknw

Roland Kuhn

unread,
Jan 28, 2013, 5:25:09 AM1/28/13
to akka...@googlegroups.com
Hi John,

if you need to pass in both “prev” and “next” then you obviously cannot do it using constructor arguments (since prev’s next is yourself). I’d recommend instantiating all of them using the Props(clazz) variant and then when you have all the ActorRefs you send each of them a message with their prev/next pointers.

BTW: no need for .asInstanceOf[Actor], just context.actorOf(Props(classOf[FooActor]), "myname")

Regards,

Roland

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://akka.io/faq/
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.

To post to this group, send email to akka...@googlegroups.com.



Dr. Roland Kuhn
Akka Tech Lead
Typesafe – The software stack for applications that scale.
twitter: @rolandkuhn

√iktor Ҡlang

unread,
Jan 28, 2013, 5:27:43 AM1/28/13
to Akka User List
We should probably create a DynamicAccessExtension instead and not encourage blindcasting.

Cheers,


To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.



--
Viktor Klang
Director of Engineering

Typesafe - The software stack for applications that scale
Twitter: @viktorklang

Jonas Bonér

unread,
Jan 28, 2013, 5:31:21 AM1/28/13
to akka...@googlegroups.com
On Mon, Jan 28, 2013 at 11:27 AM, √iktor Ҡlang <viktor...@gmail.com> wrote:
> We should probably create a DynamicAccessExtension instead and not encourage
> blindcasting.

+1 Feels bad to recommend it as an idiomatic way.
Jonas Bonér
Phone: +46 733 777 123
Home: http://jonasboner.com
Twitter: @jboner
Reply all
Reply to author
Forward
0 new messages