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

发布于 2024-11-03 21:12:46 字数 2396 浏览 0 评论 0原文

我尝试按照 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);
    }
}

我的服务 接口

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

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.

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)

Here is my code for the 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);
    }
}

My interface for the service:

public interface BaseService {

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

And a test class:

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

}

In the example they write:

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

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

How can I implement an TypedActor with a custom constructor?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心意如水 2024-11-10 21:12:46

据我所知,您的代码是正确的。
要使用非默认构造函数在 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(*)"/>

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.

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.

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(*)"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文