java 的typed,如何在Java应用程序中使用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?

解决方案

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:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值