如何在Java应用程序中使用TypedActor?

时间:2011-04-29 08:43:04

标签: java actor akka typedactor

我尝试按照Typed Actors (Java)上的示例在Java中实现TypedActor。但我很挣扎。我添加了akka-actor-1.1-M1.jarakka-typed-actor-1.1-M1.jarscala-library.jar但这还不够。我在Eclipse中遇到错误,因此我还在构建路径中添加了aspectwerkz-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

1 个答案:

答案 0 :(得分: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(*)"/>