技术控

    今日:5| 主题:49136
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Using Akka With Java

[复制链接]
終究還是敷衍 发表于 2016-10-4 19:08:35
100 3

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
In the past few days, I've been working on a project with Akka using Java. It was an amazing experience, so I'm going to discuss how to use Akka in Java and write a test case.
  If we look at the    Akka's documentation, there is a class named     UntypedActorto create an actor. But here, we're going to discuss the     AbstractActor, which seems pretty concise.  
  First, add the following dependency for Akka and the test case:
  1. "com.typesafe.akka" %% "akka-slf4j" % "2.4.8"
复制代码
Then, to create an actor, we first we need to create Props:
  1. public static Props props() {
  2.     return Props.create(HappyBdayActor.class);
  3. }
复制代码
Then, we write the responsibility of an actor as:
  1. class HappyBdayActor extends AbstractActor {
  2.     public static Props props() {
  3.         return Props.create(HappyBdayActor.class);
  4.     }
  5.     @Override
  6.     public PartialFunction<Object, BoxedUnit> receive() {
  7.       //responsiblity of this actor can be here
  8.     }
  9. }
复制代码
Now we need to find out the message passed to this actor and react accordingly. We can do this as:
  1. @Override
  2. public PartialFunction<Object, BoxedUnit> receive() {
  3.        return ReceiveBuilder
  4.             .match(String.class, message -> {
  5.                 logger.info("Message received from someone : {}"+ message);
  6.             })
  7.             .matchAny(message -> logger.error("Some unknown things happened : {}", message))
  8.             .build();
  9. }
复制代码
Now everybody will need a Supervisor. As we know, a Supervisor is an Actor that supervises other actors. Here's how we'll implement it.
  1. private final SupervisorStrategy strategy = new OneForOneStrategy(false,
  2.         match(InterruptedException.class, e ->
  3.                 SupervisorStrategy.resume()
  4.         ).
  5.                 match(Throwable.class, e ->
  6.                         SupervisorStrategy.restart()
  7.                 ).build());
  8. @Override
  9. public SupervisorStrategy supervisorStrategy() {
  10.     return strategy;
  11. }
复制代码
Now we have an architecture for an Akka actor.
  After that, I decided to introduce a Google Guice injection. To proceed with this, we need to add the following dependency:
  1. “com.google.inject” % “guice” % “4.1.0”
复制代码
We will inject our service to the actor, so we create a simple service:
  1. @Singleton
  2. public class MessageHandlerService {
  3.     static final String BEAN_NAME = "messageHandlerService";
  4.     public final String substr(String message , int index){
  5.         return "Happy bday "+message.substring(index);
  6.     }
  7. }
复制代码
Now create an another class to define injections:
  1. public class Config extends AbstractModule {
  2.     @Override
  3.     protected final void configure() {
  4.         bind(ActorSystem.class).toInstance(ActorSystem.apply());
  5.         bind(MessageHandlerService.class).annotatedWith(Names.named(MessageHandlerService.BEAN_NAME)).
  6.                 to(MessageHandlerService.class);
  7.     }
  8.     @Singleton
  9.     @Provides
  10.     @Named(value = Supervisor.BEAN_NAME)
  11.     public final ActorRef supervisorRef(final ActorSystem system) {
  12.         return system.actorOf(Supervisor.props());
  13.     }
  14.     @Provides
  15.     @Inject
  16.     @Named(value = HappyBdayActor.BEAN_NAME)
  17.     public final ActorRef HappyBdayActorRef(@Named(Supervisor.BEAN_NAME) final ActorRef supervisor,
  18.                                             @Named(MessageHandlerService.BEAN_NAME) final MessageHandlerService messageHandlerService) throws Exception {
  19.         CompletionStage<Object> eventFuture = ask(supervisor, HappyBdayActor.props(messageHandlerService),
  20.                 Timeout.apply(50, TimeUnit.MILLISECONDS));
  21.         return (ActorRef) eventFuture.toCompletableFuture().get(60, TimeUnit.MILLISECONDS);
  22.     }
  23. }
复制代码
And also modify the actor as:
  1. final LoggingAdapter logger = Logging.getLogger(context().system(), this);
  2. public static final String BEAN_NAME = "HappyBdayActorRef";
  3. private final MessageHandlerService messageHandlerService;
  4. HappyBdayActor(MessageHandlerService messageHandlerServie) {
  5.     this.messageHandlerService = messageHandlerServie;
  6. }
  7. public static Props props(MessageHandlerService messageHandlerService) {
  8.     return Props.create(HappyBdayActor.class, messageHandlerService);
  9. }
复制代码
Now we can write our launcher to start the actor’s execution as:
  1. public class Launcher {
  2.     private ActorRef happyBdayActorRef;
  3.     @Inject
  4.     public Launcher(@Named(HappyBdayActor.BEAN_NAME) ActorRef happyBdayActorRef) {
  5.         this.happyBdayActorRef = happyBdayActorRef;
  6.     }
  7.     public ActorRef getHappyBdayActorRef() {
  8.         return happyBdayActorRef;
  9.     }
  10.     public static void main(String[] args) {
  11.         Injector injector = Guice.createInjector(new Config());
  12.         Launcher app = injector.getInstance(Launcher.class);
  13.         app.getHappyBdayActorRef().tell("Hello",ActorRef.noSender());
  14.     }
  15. }
复制代码
Now, we developers are always curious about how to write unit test cases. Add the following sbt dependency:
  1. public static Props props() {
  2.     return Props.create(HappyBdayActor.class);
  3. }0
复制代码
And now, our unit test case:
  1. public static Props props() {
  2.     return Props.create(HappyBdayActor.class);
  3. }1
复制代码
For the complete code,    click here.
友荐云推荐




上一篇:scrollbarStyle详解
下一篇:基于 Koa平台Node.js开发的KoaHub.js获取/设置会话功能代码
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

小生活大甜蜜 发表于 2016-10-4 20:05:21
再美好也经不住遗忘,再悲伤也抵不过时间。
回复 支持 反对

使用道具 举报

qgb126 发表于 2016-10-8 10:31:12
萌萌哒
回复 支持 反对

使用道具 举报

hamlili 发表于 2016-10-11 17:57:14
听終究還是敷衍一席话,省我十本书!
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表