流返回错误的类型
我试图理解反应性风格。但是在这个示例上陷入困境。
public class ScriptServiceImpl implements ScriptService{
private static Logger log = LoggerFactory.getLogger(ScriptServiceImpl.class);
private final ScriptEngineManager manager = new ScriptEngineManager();
private final ScriptEngine engine = manager.getEngineByName("JavaScript");
@Override
public Flux<MyFunctionResult> evaluate(MyFunction myFunction, Integer iterations){
Flux<MyFunctionResult> flux = Flux.empty();
flux.mergeWith(
Flux.range(1,iterations)
.map(counter -> {
engine.put("parametr", counter);
try {
long start = System.currentTimeMillis();
String functionResult = engine.eval(myFunction.getSource()).toString();
long timer = System.currentTimeMillis() - start;
return Mono.just(new MyFunctionResult(timer, functionResult, myFunction.getNumber(), counter));
} catch (ScriptException ex) {
return Mono.error(ex);
}
})
);
return flux;
}
}
我想返回
,但获取myFunctionResult
的fluxflux
of object> object
in in
flux.mergewith
部分。我在做什么错?
I'm trying to understand reactive style. But stuck on this example.
public class ScriptServiceImpl implements ScriptService{
private static Logger log = LoggerFactory.getLogger(ScriptServiceImpl.class);
private final ScriptEngineManager manager = new ScriptEngineManager();
private final ScriptEngine engine = manager.getEngineByName("JavaScript");
@Override
public Flux<MyFunctionResult> evaluate(MyFunction myFunction, Integer iterations){
Flux<MyFunctionResult> flux = Flux.empty();
flux.mergeWith(
Flux.range(1,iterations)
.map(counter -> {
engine.put("parametr", counter);
try {
long start = System.currentTimeMillis();
String functionResult = engine.eval(myFunction.getSource()).toString();
long timer = System.currentTimeMillis() - start;
return Mono.just(new MyFunctionResult(timer, functionResult, myFunction.getNumber(), counter));
} catch (ScriptException ex) {
return Mono.error(ex);
}
})
);
return flux;
}
}
I want to return Flux
of MyFunctionResult
but get Flux
of Object
in Flux.mergeWith
section. What am i doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有多个问题,
myFunctionResult
纳入mono
。地图
期望无反应的返回类型。结果,而不是mono.Error
您应该将检查的异常包装到未检查的RuntimeTimeException
中。flux.mergewith
而不是flux
的结果。但是通常,在此示例中,您不需要Mergewith
您的代码可以被转换为
另外,不确定
Engine.eval.eval
,但如果这是阻止代码它并在单独的调度程序上运行打电话?There are multiple issues here
MyFunctionResult
intoMono
.map
expects none-reactive return type. As result, instead ofMono.error
you should just wrap checked exception into uncheckedRuntimeException
.flux.mergeWith
and notflux
. But in general for this example you don't needmergeWith
Your code could be converted into
In addition, not sure about
engine.eval
but in case this is blocking code consider wrapping it and run on a separate scheduler How Do I Wrap a Synchronous, Blocking Call?