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