Akka / Java getContext()。带有参数吗?

编程入门 行业动态 更新时间:2024-10-10 19:27:56
本文介绍了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()。带有参数吗?

本文发布于:2023-07-15 13:15:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1111425.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   Akka   Java   getContext

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!