流返回错误的类型

发布于 2025-01-22 20:49:41 字数 1413 浏览 0 评论 0原文

我试图理解反应性风格。但是在这个示例上陷入困境。

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的flux,但获取flux 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝天白云 2025-01-29 20:49:41

这里有多个问题,

  • 您不需要将myFunctionResult纳入mono地图期望无反应的返回类型。结果,而不是mono.Error您应该将检查的异常包装到未检查的RuntimeTimeException中。
  • 您需要返回flux.mergewith而不是flux的结果。但是通常,在此示例中,您不需要Mergewith

您的代码可以被转换为

return 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 new MyFunctionResult(timer, functionResult, myFunction.getNumber(), counter);
            } catch (ScriptException ex) {
                throw Exceptions.propagate(ex);
            }
        });

另外,不确定Engine.eval.eval,但如果这是阻止代码它并在单独的调度程序上运行打电话?

There are multiple issues here

  • you don't need to wrap MyFunctionResult into Mono. map expects none-reactive return type. As result, instead of Mono.error you should just wrap checked exception into unchecked RuntimeException.
  • you need to return result of the flux.mergeWith and not flux. But in general for this example you don't need mergeWith

Your code could be converted into

return 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 new MyFunctionResult(timer, functionResult, myFunction.getNumber(), counter);
            } catch (ScriptException ex) {
                throw Exceptions.propagate(ex);
            }
        });

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文