如何在Java应用程序中使用TypedActor? [英] How can I use a TypedActor in a Java application?

查看:282
本文介绍了如何在Java应用程序中使用TypedActor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 TypedActor =nofollow>键入的Actor(Java)。但我在苦苦挣扎。我添加了 akka-actor-1.1-M1.jar akka-typed-actor-1.1-M1.jar scala-library.jar 但这还不够。我在Eclipse中遇到错误,所以我还将 aspectwerkz-2.0.jar aspectwerkz-core-2.0.jar 添加到我的构建路径。

I try to implement a TypedActor in Java following the examples on Typed Actors (Java). But I'm struggling. I have added akka-actor-1.1-M1.jar, akka-typed-actor-1.1-M1.jar, scala-library.jar but it wasn't enough. I got errors in Eclipse so I also added aspectwerkz-2.0.jar and aspectwerkz-core-2.0.jar to my Build Path.

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

I try to use a TypedActor with custom constructor.

但现在我收到错误编译:

But now I get an error at compilation:

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);
    }
}

我的界面用于服务:

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);
    }

}

在他们写的例子中: / p>

In the example they write:

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

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

But I don't think that classOf[Service] is Java, it's looks more like Scala.

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

How can I implement an TypedActor with a custom constructor?

推荐答案

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

Your code is correct, as far as I can see. To instantiate a TypedActor in Java with a non default constructor, you should use:

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

确实,官方文档包含拼写错误。

Indeed, the official doc contains a typo.

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

Can you try to use aspectwerkz-2.2.3? I have tried your code and it does work for me. The only difference is the version of aspectwerkz I'm using.

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

Also, please note that these are the dependencies for 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(*)"/>

这篇关于如何在Java应用程序中使用TypedActor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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