Akka / Java getContext()。带有参数吗? [英] Akka/Java getContext().become with parameter?

查看:164
本文介绍了Akka / Java getContext()。带有参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Akka / Scala中,可以将参数传递给自定义接收函数,因此可以通过参数传递整个演员状态,而无需使用可变变量。

In Akka/Scala one is able to pass parameters to the custom receive function, so it is possible to pass the whole actor state through params, without using mutable variables.

context.become(myCustomReceive(param1, param2))

但是在Java Api中,您只能传递将接收到的消息作为唯一参数的Procedure

But in Java Api you can pass only Procedure which gets the received message as the only param

getContext().become( new Procedure<Object> {
  public void apply(Object param) throws Exception
  {
    // ... 
  }
}

是否有一种干净的方法可以在Java中执行相同的技巧?

Is there a clean way to do the same trick in Java?

推荐答案

我会这样做

class ProcedureWithParams<T> extends Procedure<T> {
   Object param1;
   Object param2;

   ProcedureWithParams(Object param1, Object param2) {
      this.param1 = param1;
      this.param2 = param2;
   }

   public void apply(Object param) throws Exception {
    //access para1 and param2 here
   }

}

getContext().become( new ProcedureWithParams(param1, param2))

这篇关于Akka / Java getContext()。带有参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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