Akka/Java getContext().become with parameter?

青春壹個敷衍的年華 提交于 2020-01-04 02:15:08

问题


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

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
  {
    // ... 
  }
}

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


回答1:


I would do it like this

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


来源:https://stackoverflow.com/questions/21455020/akka-java-getcontext-become-with-parameter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!