Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CompletableFuture to wait until AsyncReader is fully initialised #196

Open
Eistern opened this issue Oct 29, 2023 · 0 comments
Open

Comments

@Eistern
Copy link

Eistern commented Oct 29, 2023

Currently, when working with the AsyncReader there is no convenient way to block the main thread until it is not needed

Example:

package org.example;

import tech.ydb.topic.TopicClient;
import tech.ydb.topic.read.AsyncReader;
import tech.ydb.topic.read.Message;
import tech.ydb.topic.read.PartitionSession;
import tech.ydb.topic.read.events.AbstractReadEventHandler;
import tech.ydb.topic.read.events.DataReceivedEvent;
import tech.ydb.topic.read.events.ReaderClosedEvent;
import tech.ydb.topic.settings.ReadEventHandlersSettings;
import tech.ydb.topic.settings.ReaderSettings;
import tech.ydb.topic.settings.TopicReadSettings;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import static org.example.YdbConsts.*;

public class Reader {
    public static void main(String[] args) {
        TopicClient topicClient = TopicClient.newClient(createTransport())
                .build();

        AsyncReader asyncReader = topicClient.createAsyncReader(ReaderSettings.newBuilder()
                        .setConsumerName(CONSUMER_NAME)
                        .setTopics(topic())
                        .build(),
                ReadEventHandlersSettings.newBuilder()
                        .setEventHandler(new EventHandler())
                        .build()
        );

        CompletableFuture<Void> init = asyncReader.init();
        init.join();
        synchronized (Reader.class) {
            try {
                Reader.class.wait(10_000);
            } catch (InterruptedException ignored) {}
        }
    }

    private static List<TopicReadSettings> topic() {
        List<TopicReadSettings> result = new ArrayList<>();
        for (TopicDesc topic : TOPICS) {
            TopicReadSettings topicS = TopicReadSettings.newBuilder()
                    .setPath(topic.name())
                    .build();
            result.add(topicS);
        }
        return result;
    }

    private static class EventHandler extends AbstractReadEventHandler {
        @Override
        public void onMessages(DataReceivedEvent event) {
            PartitionSession partitionSession = event.getPartitionSession();
            System.out.printf("Got message from: %s %d %d%n", partitionSession.getPath(), partitionSession.getPartitionId(), partitionSession.getId());
            for (Message message : event.getMessages()) {
                String s = new String(message.getData());
                System.out.printf("Message %s%n", s);
            }

            synchronized (this) {
                try {
                    this.wait(10_000);
                } catch (InterruptedException ignored) {}
            }

            event.commit();
        }

        @Override
        public void onReaderClosed(ReaderClosedEvent event) {
            System.out.println("Closed");
        }
    }
}

This example won't work without an additional wait after joining on a init future due to all other threads being marked as a daemon


Main request: Provide some means to actually wait for the reader to be properly opened (even without assigned partition session)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant