0
votes

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();
                });
            });
1
you want to send requests one by one, or wait till they all are finished? - injecteer
Probably you mean "sequentially" (one after the other), and not "consistently" (in every case)? - Alexey Soshin
@AlexeySoshin yes that's right sequentially, sorry - Sad Santa
@injecteer one by one. sequentially =) - Sad Santa
in your code the requests are sequentially sent one by one. What was the problem again? - injecteer

1 Answers

1
votes

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);
                    }
                });
}