use of com.google.cloud.pubsub.v1.MessageReceiver in project beam by apache.
the class TestPubsub method waitForNMessages.
/**
* Repeatedly pull messages from {@link #subscriptionPath()}, returns after receiving {@code n}
* messages or after waiting for {@code timeoutDuration}.
*/
public List<PubsubMessage> waitForNMessages(int n, Duration timeoutDuration) throws IOException, InterruptedException {
Preconditions.checkNotNull(subscriptionPath);
BlockingQueue<com.google.pubsub.v1.PubsubMessage> receivedMessages = new LinkedBlockingDeque<>(n);
MessageReceiver receiver = (com.google.pubsub.v1.PubsubMessage message, AckReplyConsumer replyConsumer) -> {
if (receivedMessages.offer(message)) {
replyConsumer.ack();
} else {
replyConsumer.nack();
}
};
Subscriber subscriber = Subscriber.newBuilder(subscriptionPath.getPath(), receiver).setCredentialsProvider(pipelineOptions::getGcpCredential).setChannelProvider(channelProvider).setEndpoint(pubsubEndpoint).build();
subscriber.startAsync();
DateTime startTime = new DateTime();
int timeoutSeconds = timeoutDuration.toStandardSeconds().getSeconds();
while (receivedMessages.size() < n && Seconds.secondsBetween(startTime, new DateTime()).getSeconds() < timeoutSeconds) {
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
}
}
subscriber.stopAsync();
subscriber.awaitTerminated();
return receivedMessages.stream().map((message) -> new PubsubMessage(message.getData().toByteArray(), message.getAttributesMap(), message.getMessageId())).collect(Collectors.toList());
}
use of com.google.cloud.pubsub.v1.MessageReceiver in project flink by apache.
the class PubsubHelper method subscribeToSubscription.
public Subscriber subscribeToSubscription(String project, String subscription, MessageReceiver messageReceiver) {
ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(project, subscription);
Subscriber subscriber = Subscriber.newBuilder(subscriptionName, messageReceiver).setChannelProvider(channelProvider).setCredentialsProvider(EmulatorCredentialsProvider.create()).build();
subscriber.startAsync();
return subscriber;
}
Aggregations