Search in sources :

Example 1 with SubscriptionHandle

use of com.hedera.hashgraph.sdk.SubscriptionHandle in project hedera-mirror-node by hashgraph.

the class MirrorNodeClient method subscribeToTopicAndRetrieveMessages.

public SubscriptionResponse subscribeToTopicAndRetrieveMessages(SDKClient sdkClient, TopicMessageQuery topicMessageQuery, int numMessages, long latency) throws Throwable {
    latency = latency <= 0 ? acceptanceTestProperties.getMessageTimeout().toSeconds() : latency;
    log.debug("Subscribing to topic, expecting {} within {} seconds.", numMessages, latency);
    CountDownLatch messageLatch = new CountDownLatch(numMessages);
    SubscriptionResponse subscriptionResponse = new SubscriptionResponse();
    Stopwatch stopwatch = Stopwatch.createStarted();
    SubscriptionHandle subscription = topicMessageQuery.subscribe(sdkClient.getClient(), resp -> {
        // add expected messages only to messages list
        if (subscriptionResponse.getMirrorHCSResponses().size() < numMessages) {
            subscriptionResponse.handleConsensusTopicResponse(resp);
        }
        messageLatch.countDown();
    });
    subscriptionResponse.setSubscription(subscription);
    if (!messageLatch.await(latency, TimeUnit.SECONDS)) {
        stopwatch.stop();
        log.error("{} messages were expected within {} s. {} not yet received after {}", numMessages, latency, messageLatch.getCount(), stopwatch);
    } else {
        stopwatch.stop();
        log.info("Success, received {} out of {} messages received in {}.", numMessages - messageLatch.getCount(), numMessages, stopwatch);
    }
    subscriptionResponse.setElapsedTime(stopwatch);
    if (subscriptionResponse.errorEncountered()) {
        throw subscriptionResponse.getResponseError();
    }
    return subscriptionResponse;
}
Also used : Stopwatch(com.google.common.base.Stopwatch) SubscriptionHandle(com.hedera.hashgraph.sdk.SubscriptionHandle) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with SubscriptionHandle

use of com.hedera.hashgraph.sdk.SubscriptionHandle in project hedera-mirror-node by hashgraph.

the class MirrorNodeClient method subscribeToTopic.

public SubscriptionResponse subscribeToTopic(SDKClient sdkClient, TopicMessageQuery topicMessageQuery) throws Throwable {
    log.debug("Subscribing to topic.");
    SubscriptionResponse subscriptionResponse = new SubscriptionResponse();
    SubscriptionHandle subscription = topicMessageQuery.subscribe(sdkClient.getClient(), subscriptionResponse::handleConsensusTopicResponse);
    subscriptionResponse.setSubscription(subscription);
    // allow time for connection to be made and error to be caught
    Thread.sleep(5000, 0);
    return subscriptionResponse;
}
Also used : SubscriptionHandle(com.hedera.hashgraph.sdk.SubscriptionHandle)

Example 3 with SubscriptionHandle

use of com.hedera.hashgraph.sdk.SubscriptionHandle in project hedera-mirror-node by hashgraph.

the class GrpcClientSDK method subscribeToClient.

private Flux<SubscribeResponse> subscribeToClient(Client client, GrpcSubscription subscription) {
    Sinks.Many<TopicMessage> sink = Sinks.many().multicast().directBestEffort();
    TopicMessageQuery topicMessageQuery = subscription.getTopicMessageQuery();
    topicMessageQuery.setCompletionHandler(sink::tryEmitComplete);
    topicMessageQuery.setErrorHandler((throwable, topicMessage) -> sink.tryEmitError(throwable));
    // Disable since we use our own retry logic to capture errors
    topicMessageQuery.setMaxAttempts(0);
    SubscriptionHandle subscriptionHandle = topicMessageQuery.subscribe(client, sink::tryEmitNext);
    return sink.asFlux().publishOn(Schedulers.parallel()).doFinally(s -> subscriptionHandle.unsubscribe()).doOnComplete(subscription::onComplete).doOnError(subscription::onError).doOnNext(subscription::onNext).map(t -> toResponse(subscription, t));
}
Also used : Sinks(reactor.core.publisher.Sinks) TopicMessage(com.hedera.hashgraph.sdk.TopicMessage) SubscriptionHandle(com.hedera.hashgraph.sdk.SubscriptionHandle) TopicMessageQuery(com.hedera.hashgraph.sdk.TopicMessageQuery)

Example 4 with SubscriptionHandle

use of com.hedera.hashgraph.sdk.SubscriptionHandle in project hedera-mirror-node by hashgraph.

the class MirrorNodeClient method subscribeToTopicAndRetrieveMessages.

public SubscriptionResponse subscribeToTopicAndRetrieveMessages(TopicMessageQuery topicMessageQuery, int numMessages, long latency) throws Throwable {
    latency = latency <= 0 ? sdkClient.getMessageTimeoutSeconds() : latency;
    log.debug("Subscribing to topic, expecting {} within {} seconds.", numMessages, latency);
    CountDownLatch messageLatch = new CountDownLatch(numMessages);
    SubscriptionResponse subscriptionResponse = new SubscriptionResponse();
    Stopwatch stopwatch = Stopwatch.createStarted();
    SubscriptionHandle subscription = topicMessageQuery.subscribe(client, resp -> {
        // add expected messages only to messages list
        if (subscriptionResponse.getMirrorHCSResponses().size() < numMessages) {
            subscriptionResponse.handleConsensusTopicResponse(resp);
        }
        messageLatch.countDown();
    });
    subscriptionResponse.setSubscription(subscription);
    if (!messageLatch.await(latency, TimeUnit.SECONDS)) {
        stopwatch.stop();
        log.error("{} messages were expected within {} s. {} not yet received after {}", numMessages, latency, messageLatch.getCount(), stopwatch);
    } else {
        stopwatch.stop();
        log.info("Success, received {} out of {} messages received in {}.", numMessages - messageLatch.getCount(), numMessages, stopwatch);
    }
    subscriptionResponse.setElapsedTime(stopwatch);
    if (subscriptionResponse.errorEncountered()) {
        throw subscriptionResponse.getResponseError();
    }
    return subscriptionResponse;
}
Also used : Stopwatch(com.google.common.base.Stopwatch) SubscriptionHandle(com.hedera.hashgraph.sdk.SubscriptionHandle) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 5 with SubscriptionHandle

use of com.hedera.hashgraph.sdk.SubscriptionHandle in project hedera-mirror-node by hashgraph.

the class MirrorNodeClient method subscribeToTopic.

public SubscriptionResponse subscribeToTopic(TopicMessageQuery topicMessageQuery) throws Throwable {
    log.debug("Subscribing to topic.");
    SubscriptionResponse subscriptionResponse = new SubscriptionResponse();
    SubscriptionHandle subscription = topicMessageQuery.subscribe(client, subscriptionResponse::handleConsensusTopicResponse);
    subscriptionResponse.setSubscription(subscription);
    // allow time for connection to be made and error to be caught
    Thread.sleep(5000, 0);
    return subscriptionResponse;
}
Also used : SubscriptionHandle(com.hedera.hashgraph.sdk.SubscriptionHandle)

Aggregations

SubscriptionHandle (com.hedera.hashgraph.sdk.SubscriptionHandle)5 Stopwatch (com.google.common.base.Stopwatch)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 TopicMessage (com.hedera.hashgraph.sdk.TopicMessage)1 TopicMessageQuery (com.hedera.hashgraph.sdk.TopicMessageQuery)1 Sinks (reactor.core.publisher.Sinks)1