首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Mutiny反应式编程中调用长时间运行的阻塞空返回方法?

如何在Mutiny反应式编程中调用长时间运行的阻塞空返回方法?
EN

Stack Overflow用户
提问于 2021-03-14 23:31:10
回答 2查看 735关注 0票数 2

我在Mutiny的Uni上有一个异步和同步方法调用链,有些方法是一个返回类型为void的长时间运行的进程。

在不阻塞下游的情况下调用它们的正确方式是什么?

下面是简单的类比代码。

代码语言:javascript
复制
class Root {

    public static void main(String[] args) {

        final Response response = getResponsePayload(); // Gets the the Payload from upstream service
        Uni.createFrom().item(response)
            .onItem().invoke(() -> System.out.println("Process Started"))
            .onItem().call(res -> {
            longRunningMethodAsync(res);    // long running blocking method, I want to run on a worker thread
            return Uni.createFrom().voidItem(); // This line I created, because of the ppipeline will be broken if the Uni is not returned from here
        })
            .onItem().transform(item -> item.hello + " mutiny")
            .onItem().transform(String::toUpperCase)
            .subscribe().with(
            item -> System.out.println(">> " + item));  // This is printed to the console
    }



    // Boilerplate method - I created to invoke/call the actual method actual method - `longRunningMethod`, this method basically an adapter
    // This is the best apprach I could come up, but I'm looking for better thatn this as I'm not conviced I'm doing it right
    private static UniSubscribe<Void> longRunningMethodAsync(final Response response) {

        
        return Uni.createFrom().voidItem().invoke(() -> longRunningMethod(response))
            .runSubscriptionOn(Infrastructure.getDefaultExecutor()).subscribe();
    }


    // Important - this is the method I want to run asynchronously independently of main *event-loop* thread.
    private static void longRunningMethod(final Response response) {
  
        System.out.println("Long running process started"); // Doesn't get printed, which means this is never called at all, not even in the blocked manner by the main even-loop thread
    }




   // Not as importatnt, I provded this in case if you like to run on your local box
    private static Response getResponsePayload() {
        return new Response();
    }

    private static class Response {
        public final String hello = "hello";
    }
}
EN

回答 2

Stack Overflow用户

发布于 2022-01-07 09:36:57

尝试:

代码语言:javascript
复制
private static UniSubscribe<Void> longRunningMethodAsync(final Response response) {
            
   return Uni.createFrom()
  .voidItem()
  .invoke(() -> longRunningMethod(response))
  .runSubscriptionOn(Infrastructure.getDefaultExecutor())
  .subscribeAsCompletionStage().join();
}
票数 1
EN

Stack Overflow用户

发布于 2021-03-17 15:19:04

通常,使用runSubscriptionOn并传递特定的executor:

代码语言:javascript
复制
longRunningMethodAsync
   .runSubscriptionOn(executor);

请注意,它会将并发限制为executor中可用线程的数量。

参考资料:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66626298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档