如何使 Vert.x EventBus.send 顺序处理请求?

huangapple go评论69阅读模式
英文:

How to make Vert.x EventBus.send process requests sequentially?

问题

I can help you translate the provided content. Here is the translation:

"我是新手使用 vert.x,想知道是否可以配置 eventbus 以使其能够稳定工作?

我的意思是需要使用 vert.x 逐个发送请求。

目前,我有这段代码,它使用事件循环原则,并等待所有处理程序完成,但我不需要那么快完成,我的想法是解放服务器,避免同时处理大量请求。在这里 eb_send() 使用默认的 EventBus.send() 方法。换句话说,我希望在请求之前执行所有请求,等待答案。

List<Future> queue = new ArrayList<>();

files.forEach(fileObj -> {
    Future<JsonObject> trashStatusHandler = Future.future();
    queue.add(trashStatusHandler);

    eb_send(segment, StorageType.getAddress(StorageType.getStorageType(fileInfo.getString("storageType"))) + ".getTrashStatus", fileInfo, reply -> {
        Entity dummy = createDummySegment();
        try {
            if (reply.succeeded()) {
                //succeeded
            }
        } catch (Exception ex) {
            log.error(ex);
        }
        trashStatusHandler.complete();
    });
});

如果需要进一步帮助,请告诉我。"

英文:

I'm new to vert.x and would like to know if its possible to configure eventbus somehow to make it work consistently?

I mean need to send requests one by one using vert.x

At the moment I got this code which uses eventloop principle and waits until all handlers finished, but I don't need this done that fast, idea is to free server from lots of requests at the same time. Here eb_send() uses default EventBus.send() method. In other words I want to execute all requests with blocking, waiting for answers before requests.

List&lt;Future&gt; queue = new ArrayList&lt;&gt;();

files.forEach(fileObj -&gt; {
                Future&lt;JsonObject&gt; trashStatusHandler = Future.future();
                queue.add(trashStatusHandler);


                eb_send(segment, StorageType.getAddress(StorageType.getStorageType(fileInfo.getString(&quot;storageType&quot;))) + &quot;.getTrashStatus&quot;, fileInfo, reply -&gt; {
                    Entity dummy = createDummySegment();
                    try {
                        if (reply.succeeded()) {
                            //succeded
                        }
                    } catch (Exception ex) {
                        log.error(ex);
                    }
                    trashStatusHandler.complete();
                });
            });

答案1

得分: 1

The basic idea is to extract this into a function, which you would invoke recursively.

public void sendFile(List<File> files, AtomicInteger c) {
    eb_send(segment, StorageType.getAddress(StorageType.getStorageType(fileInfo.getString("storageType"))) + ".getTrashStatus", fileInfo, reply -> {
        Entity dummy = createDummySegment();
        try {
            if (reply.succeeded()) {
                // succeeded
            }
            // Recursion 
            if (c.incrementAndGet() < files.size()) {
                sendFile(files, c);
            }
        } catch (Exception ex) {
            log.error(ex);
        }
    });
}
英文:

The basic idea is to extract this into a function, which you would invoke recursively.

public void sendFile(List&lt;File&gt; files, AtomicInteger c) {
    eb_send(segment, StorageType.getAddress(StorageType.getStorageType(fileInfo.getString(&quot;storageType&quot;))) + &quot;.getTrashStatus&quot;, fileInfo, reply -&gt; {
                    Entity dummy = createDummySegment();
                    try {
                        if (reply.succeeded()) {
                            //succeded
                        }
                        // Recursion 
                        if (c.incrementAndGet() &lt; files.size()) {
                            sendFile(files, c);
                        }
                    } catch (Exception ex) {
                        log.error(ex);
                    }
                });
} 

huangapple
  • 本文由 发表于 2020年8月10日 20:13:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63339967.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定