use of com.google.pubsub.v1.PullResponse in project beam by apache.
the class PubsubGrpcClient method pull.
@Override
public List<IncomingMessage> pull(long requestTimeMsSinceEpoch, SubscriptionPath subscription, int batchSize, boolean returnImmediately) throws IOException {
PullRequest request = PullRequest.newBuilder().setSubscription(subscription.getPath()).setReturnImmediately(returnImmediately).setMaxMessages(batchSize).build();
PullResponse response = subscriberStub().pull(request);
if (response.getReceivedMessagesCount() == 0) {
return ImmutableList.of();
}
List<IncomingMessage> incomingMessages = new ArrayList<>(response.getReceivedMessagesCount());
for (ReceivedMessage message : response.getReceivedMessagesList()) {
PubsubMessage pubsubMessage = message.getMessage();
@Nullable Map<String, String> attributes = pubsubMessage.getAttributes();
// Payload.
byte[] elementBytes = pubsubMessage.getData().toByteArray();
// Timestamp.
String pubsubTimestampString = null;
Timestamp timestampProto = pubsubMessage.getPublishTime();
if (timestampProto != null) {
pubsubTimestampString = String.valueOf(timestampProto.getSeconds() + timestampProto.getNanos() / 1000L);
}
long timestampMsSinceEpoch = extractTimestamp(timestampAttribute, pubsubTimestampString, attributes);
// Ack id.
String ackId = message.getAckId();
checkState(!Strings.isNullOrEmpty(ackId));
// Record id, if any.
@Nullable String recordId = null;
if (idAttribute != null && attributes != null) {
recordId = attributes.get(idAttribute);
}
if (Strings.isNullOrEmpty(recordId)) {
// Fall back to the Pubsub provided message id.
recordId = pubsubMessage.getMessageId();
}
incomingMessages.add(new IncomingMessage(elementBytes, attributes, timestampMsSinceEpoch, requestTimeMsSinceEpoch, ackId, recordId));
}
return incomingMessages;
}
use of com.google.pubsub.v1.PullResponse in project beam by apache.
the class PubsubGrpcClientTest method pullOneMessage.
@Test
public void pullOneMessage() throws IOException {
String expectedSubscription = SUBSCRIPTION.getPath();
final PullRequest expectedRequest = PullRequest.newBuilder().setSubscription(expectedSubscription).setReturnImmediately(true).setMaxMessages(10).build();
Timestamp timestamp = Timestamp.newBuilder().setSeconds(PUB_TIME / 1000).setNanos((int) (PUB_TIME % 1000) * 1000).build();
PubsubMessage expectedPubsubMessage = PubsubMessage.newBuilder().setMessageId(MESSAGE_ID).setData(ByteString.copyFrom(DATA.getBytes())).setPublishTime(timestamp).putAllAttributes(ATTRIBUTES).putAllAttributes(ImmutableMap.of(TIMESTAMP_ATTRIBUTE, String.valueOf(MESSAGE_TIME), ID_ATTRIBUTE, RECORD_ID)).build();
ReceivedMessage expectedReceivedMessage = ReceivedMessage.newBuilder().setMessage(expectedPubsubMessage).setAckId(ACK_ID).build();
final PullResponse response = PullResponse.newBuilder().addAllReceivedMessages(ImmutableList.of(expectedReceivedMessage)).build();
final List<PullRequest> requestsReceived = new ArrayList<>();
SubscriberImplBase subscriberImplBase = new SubscriberImplBase() {
@Override
public void pull(PullRequest request, StreamObserver<PullResponse> responseObserver) {
requestsReceived.add(request);
responseObserver.onNext(response);
responseObserver.onCompleted();
}
};
Server server = InProcessServerBuilder.forName(channelName).addService(subscriberImplBase).build().start();
try {
List<IncomingMessage> acutalMessages = client.pull(REQ_TIME, SUBSCRIPTION, 10, true);
assertEquals(1, acutalMessages.size());
IncomingMessage actualMessage = acutalMessages.get(0);
assertEquals(ACK_ID, actualMessage.ackId);
assertEquals(DATA, new String(actualMessage.elementBytes));
assertEquals(RECORD_ID, actualMessage.recordId);
assertEquals(REQ_TIME, actualMessage.requestTimeMsSinceEpoch);
assertEquals(MESSAGE_TIME, actualMessage.timestampMsSinceEpoch);
assertEquals(expectedRequest, Iterables.getOnlyElement(requestsReceived));
} finally {
server.shutdownNow();
}
}
use of com.google.pubsub.v1.PullResponse in project google-cloud-java by GoogleCloudPlatform.
the class PollingSubscriberConnection method pullMessages.
private void pullMessages(final Duration backoff) {
ListenableFuture<PullResponse> pullResult = stub.withDeadlineAfter(DEFAULT_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS).pull(PullRequest.newBuilder().setSubscription(subscription).setMaxMessages(maxDesiredPulledMessages).setReturnImmediately(true).build());
Futures.addCallback(pullResult, new FutureCallback<PullResponse>() {
@Override
public void onSuccess(PullResponse pullResponse) {
if (pullResponse.getReceivedMessagesCount() == 0) {
// No messages in response, possibly caught up in backlog, we backoff to avoid
// slamming the server.
pollingExecutor.schedule(new Runnable() {
@Override
public void run() {
Duration newBackoff = backoff.multipliedBy(2);
if (newBackoff.compareTo(MAX_BACKOFF) > 0) {
newBackoff = MAX_BACKOFF;
}
pullMessages(newBackoff);
}
}, backoff.toMillis(), TimeUnit.MILLISECONDS);
return;
}
messageDispatcher.processReceivedMessages(pullResponse.getReceivedMessagesList(), new Runnable() {
@Override
public void run() {
pullMessages(INITIAL_BACKOFF);
}
});
}
@Override
public void onFailure(Throwable cause) {
if (!isAlive()) {
// we don't care about subscription failures when we're no longer running.
logger.log(Level.FINE, "pull failure after service no longer running", cause);
return;
}
if (StatusUtil.isRetryable(cause)) {
logger.log(Level.WARNING, "Failed to pull messages (recoverable): ", cause);
pollingExecutor.schedule(new Runnable() {
@Override
public void run() {
Duration newBackoff = backoff.multipliedBy(2);
if (newBackoff.compareTo(MAX_BACKOFF) > 0) {
newBackoff = MAX_BACKOFF;
}
pullMessages(newBackoff);
}
}, backoff.toMillis(), TimeUnit.MILLISECONDS);
} else {
messageDispatcher.stop();
notifyFailed(cause);
}
}
}, pollingExecutor);
}
use of com.google.pubsub.v1.PullResponse in project google-cloud-java by GoogleCloudPlatform.
the class SubscriptionAdminClientTest method pullTest.
@Test
@SuppressWarnings("all")
public void pullTest() {
PullResponse expectedResponse = PullResponse.newBuilder().build();
mockSubscriber.addResponse(expectedResponse);
SubscriptionName subscription = SubscriptionName.create("[PROJECT]", "[SUBSCRIPTION]");
boolean returnImmediately = false;
int maxMessages = 496131527;
PullResponse actualResponse = client.pull(subscription, returnImmediately, maxMessages);
Assert.assertEquals(expectedResponse, actualResponse);
List<GeneratedMessageV3> actualRequests = mockSubscriber.getRequests();
Assert.assertEquals(1, actualRequests.size());
PullRequest actualRequest = (PullRequest) actualRequests.get(0);
Assert.assertEquals(subscription, actualRequest.getSubscriptionAsSubscriptionName());
Assert.assertEquals(returnImmediately, actualRequest.getReturnImmediately());
Assert.assertEquals(maxMessages, actualRequest.getMaxMessages());
}
Aggregations