2

我尝试按照Typed Actors (Java)TypedActor上的示例在 Java中实现 a 。但我在挣扎。我已添加, ,但还不够。我在 Eclipse 中遇到错误,所以我还添加了和到我的构建路径。akka-actor-1.1-M1.jarakka-typed-actor-1.1-M1.jarscala-library.jaraspectwerkz-2.0.jaraspectwerkz-core-2.0.jar

我尝试使用带有自定义构造函数的 TypedActor。

但是现在我在编译时遇到错误:

Exception in thread "main" java.lang.NoSuchMethodError: org.codehaus.aspectwerkz.proxy.Proxy.newInstance([Ljava/lang/Class;[Ljava/lang/Object;ZZ)Ljava/lang/Object;
    at akka.actor.TypedActor$.newInstance(TypedActor.scala:596)
    at akka.actor.TypedActor$.newInstance(TypedActor.scala:634)
    at akka.actor.TypedActor.newInstance(TypedActor.scala)
    at com.example.actor.ActorTest.main(ActorTest.java:12)

这是我的代码BaseActor

import akka.actor.TypedActor;

public class BaseActor extends TypedActor implements BaseService {

    private String str;
    private int num;

    public BaseActor(String str, int num) {
    this.str = str;
    this.num = num;
        System.out.println("booted");
    }

    public void testData(String str, int num) {
        System.out.println(this.str + " " + this.num);
        System.out.println(str + " " + num);
    }
}

interface的服务:

public interface BaseService {

    public void testData(String str, int num);
}

和一个测试类:

import akka.actor.TypedActor;
import akka.actor.TypedActorFactory;

public class ActorTest {

    public static void main(String[] args) {

        BaseService service = TypedActor.newInstance(BaseService.class,
                                                     new TypedActorFactory() {
            public TypedActor create() {
                return new BaseActor("someString", 12);
            }
        });

        service.testData("Hello", 6);
    }

}

在示例中,他们写道:

Service service = TypedActor.newInstance(classOf[Service], 
                                         new TypedActorFactory() {
  public TypedActor create() {
    return new ServiceWithConstructorArgsImpl("someString", 500L));
});

但我不认为那classOf[Service]是 Java,它看起来更像 Scala。

如何TypedActor使用自定义构造函数实现一个?

4

1 回答 1

3

据我所知,您的代码是正确的。要使用非默认构造函数在 Java 中实例化 TypedActor,您应该使用:

BaseService service = TypedActor.newInstance(BaseService.class,
                                                     new TypedActorFactory() {
            public TypedActor create() {
                return new BaseActor("someString", 12);
            }
        });

事实上,官方文档包含一个错字。

你可以尝试使用aspectwerkz-2.2.3吗?我试过你的代码,它对我有用。唯一的区别是我使用的 aspectwerkz 版本。

另外,请注意这些是 akka-typed-actor 1.1-M1 的依赖项:

<dependency org="org.codehaus.aspectwerkz" name="aspectwerkz" rev="2.2.3" force="true" conf="compile->compile(*),master(*);runtime->runtime(*)"/>
<dependency org="aopalliance" name="aopalliance" rev="1.0" force="true" conf="compile->compile(*),master(*);runtime->runtime(*)"/>
<dependency org="org.guiceyfruit" name="guice-all" rev="2.0" force="true" conf="compile->compile(*),master(*);runtime->runtime(*)"/>
<dependency org="se.scalablesolutions.akka" name="akka-stm" rev="1.1-M1" force="true" conf="compile->compile(*),master(*);runtime->runtime(*)"/>
<dependency org="org.scala-lang" name="scala-library" rev="2.9.0.RC1" force="true" conf="compile->compile(*),master(*);runtime->runtime(*)"/>
于 2011-04-29T09:28:27.867 回答