如何使用给定的班级名称创建Akka Actor [英] How to create an Akka Actor given the class name

查看:114
本文介绍了如何使用给定的班级名称创建Akka Actor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用类名来创建Akka actor,如图所示。
我尝试了许多system.actorOf(new Props(theProcessor.getClass),name = Test),
的变体,但我无法使它正常工作。从类加载器创建Actor的任何想法

I would like to create an Akka actor by using the class name, as shown. I've tried many variations of system.actorOf(new Props(theProcessor.getClass), name = "Test"), but I just cannot get this to work. Any idea in creating an Actor from the class loader

package com.test

import akka.actor.{Props, Actor, ActorRef, ActorSystem}

object Main {
  def main(args: Array[String]) {
   ActorFromString("Test")
  }
}

object ActorFromString {
  implicit val system = ActorSystem("Test")
  def apply(name: String): ActorRef = {
    val className = "com.test." + name + "Processor"
    val theProcessor: Actor = Class.forName(className).newInstance().asInstanceOf[Actor]
    system.actorOf(new Props(theProcessor.getClass), name = "Test")
  }
}

class TestProcessor extends Actor {
  def receive = {
    case data => println("processing data")
  }
}

Exception in thread "main" akka.actor.ActorInitializationException:
You cannot create an instance of [com.test.TestProcessor] explicitly using the constructor (new).
You have to use one of the factory methods to create a new actor. Either use:
'val actor = context.actorOf(Props[MyActor])'        (to create a supervised child actor from    within an actor), or
'val actor = system.actorOf(Props(new MyActor(..)))' (to create a top level actor from the   ActorSystem)
at akka.actor.ActorInitializationException$.apply(Actor.scala:166)
at akka.actor.Actor$class.$init$(Actor.scala:377)
at com.test.TestProcessor.<init>(ActorFromString.scala:20)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at  sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at java.lang.Class.newInstance0(Class.java:374)
at java.lang.Class.newInstance(Class.java:327)
at com.test.ActorFromString$.apply(ActorFromString.scala:15)
at com.test.Main$.main(ActorFromString.scala:7)
at com.test.Main.main(ActorFromString.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Process finished with exit code 143


推荐答案

听起来像您想使用与ActorSystem使用的ClassLoader相同的ClassLoader,
,因此可以使用自定义的Akka扩展程序轻松解决此问题(警告,但实际上并未进行编译,但是您会领会到它的要旨)

Sounds like you want to use the same ClassLoader as the one that is used by your ActorSystem, so this is easily solved with a custom Akka Extension (Warning, not actually compiled but you'll get the gist of it)

import akka.actor.{ Extension, ExtensionId, ExtensionIdProvider, ExtendedActorSystem, DynamicAccess }
// This will be instantiated once per ActorSystem instance
class Reflection(access: DynamicAccess) extends Extension {
  // Loads the Class with the provided Fully Qualified Class Name using the given DynamicAccess
  // Throws exception if it fails to load
  def actorClassFor(fqcn: String) = access.getClassFor[Actor](fqcn).get
}
// This is how we access the Reflection extension, you can view it as an ActorSystemLocal
object Reflect extends ExtensionId[Reflection] with ExtensionIdProvider {
  override def lookup = Reflect
  override def createExtension(system: ExtendedActorSystem) = new Reflection(system.dynamicAccess)
}
// Load the extension if not already loaded, return the instance for this ActorSystem and call the actorClassFor
system.actorOf(Props(Reflect(system).actorClassFor("com.test." + name + "Processor"))

这篇关于如何使用给定的班级名称创建Akka Actor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆