javavert.xvertx-eventbus

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


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<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()) {
                            //succeded
                        }
                    } catch (Exception ex) {
                        log.error(ex);
                    }
                    trashStatusHandler.complete();
                });
            });

Solution

  • 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()) {
                                //succeded
                            }
                            // Recursion 
                            if (c.incrementAndGet() < files.size()) {
                                sendFile(files, c);
                            }
                        } catch (Exception ex) {
                            log.error(ex);
                        }
                    });
    }