简体   繁体   English

如何在Java中实现Function1(其compose和andThen方法)?

[英]How to implement a Function1 in Java (its compose and andThen methods)?

I am working with Akka Java API's, in one of the actors I want to receive a callback and process it on completion. 我正在使用Akka Java API,在其中一个参与者中,我想接收回调并在完成后对其进行处理。

I want to achieve something like: 我想实现以下目标:

Future future = Patterns.ask(actorRefMap.get(order.getInstrument()), order, 500);
future.onComplete(getSender().tell(String.format("{} order processed for instrument {} with price {}", order.getOrderType(), order.getInstrument(), order.getPrice()), getSelf()), getContext().dispatcher());

With my current code I am getting error wrong first argument, Found 'void' required 'scala.Function1'. 在我当前的代码中,我遇到了错误的第一个参数错误,找到“ void”需要“ scala.Function1”。 How do we implement the scala.Function1 in Java? 我们如何在Java中实现scala.Function1

You need to pass it as a function: 您需要将其作为函数传递:

Future future = Patterns.ask(actorRefMap.get(order.getInstrument()), order, 500);
future.onComplete(() -> getSender().tell(String.format("{} order processed for instrument {} with price {}", order.getOrderType(), order.getInstrument(), order.getPrice()), getSelf()), getContext().dispatcher());

... the essential part is: ...必不可少的部分是:

future.onComplete(() -> ...)

instead of 代替

future.onComplete(...)

And if it requires scala.Function1 instead of java.util.Function , make sure you import the Java DSL ( akka.actor.typed.javadsl.AskPattern ), not the Scala DSL ... 如果需要scala.Function1而不是java.util.Function ,请确保导入Java DSL( akka.actor.typed.javadsl.AskPattern ),而不是Scala DSL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM