use of com.google.pubsub.v1.StreamingPullResponse in project curiostack by curioswitch.
the class Subscriber method onNext.
@Override
public void onNext(StreamingPullResponse value) {
if (ctx == null) {
ctx = RequestContext.current();
}
streamReconnectBackoff = INITIAL_CHANNEL_RECONNECT_BACKOFF;
receivedMessages.increment(value.getReceivedMessagesCount());
AtomicInteger pendingAcks = new AtomicInteger(value.getReceivedMessagesCount());
for (ReceivedMessage message : value.getReceivedMessagesList()) {
TraceContextOrSamplingFlags contextOrFlags = traceExtractor.extract(message.getMessage());
// Add an artificial span modeling the time spent within Pub/Sub until getting here.
Span span = contextOrFlags.context() != null ? tracer.joinSpan(contextOrFlags.context()) : // entire stream.
tracer.newTrace();
span.kind(Kind.SERVER).name("google.pubsub.v1.Publisher.Publish").tag("subscription", options.getSubscription()).start(Timestamps.toMicros(message.getMessage().getPublishTime())).finish();
StreamObserver<StreamingPullRequest> requestObserver = this.requestObserver;
long startTimeNanos = System.nanoTime();
options.getMessageReceiver().receiveMessage(message.getMessage(), new AckReplyConsumer() {
@Override
public void ack() {
releaseAndRecord();
ackedMessages.increment();
checkNotNull(requestObserver, "onNext called before start()");
requestObserver.onNext(StreamingPullRequest.newBuilder().addAckIds(message.getAckId()).build());
}
@Override
public void nack() {
releaseAndRecord();
nackedMessages.increment();
checkNotNull(requestObserver, "onNext called before start()");
requestObserver.onNext(StreamingPullRequest.newBuilder().addModifyDeadlineAckIds(message.getAckId()).addModifyDeadlineSeconds(0).build());
}
private void releaseAndRecord() {
if (options.getUnsafeWrapBuffers() && pendingAcks.decrementAndGet() == 0) {
GrpcUnsafeBufferUtil.releaseBuffer(value, ctx);
}
messageProcessingTime.record(Duration.ofNanos(System.nanoTime() - startTimeNanos));
}
});
}
}
use of com.google.pubsub.v1.StreamingPullResponse in project google-cloud-java by GoogleCloudPlatform.
the class SubscriptionAdminClientTest method streamingPullExceptionTest.
@Test
@SuppressWarnings("all")
public void streamingPullExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
mockSubscriber.addException(exception);
SubscriptionName subscription = SubscriptionName.create("[PROJECT]", "[SUBSCRIPTION]");
int streamAckDeadlineSeconds = 1875467245;
StreamingPullRequest request = StreamingPullRequest.newBuilder().setSubscriptionWithSubscriptionName(subscription).setStreamAckDeadlineSeconds(streamAckDeadlineSeconds).build();
MockStreamObserver<StreamingPullResponse> responseObserver = new MockStreamObserver<>();
StreamingCallable<StreamingPullRequest, StreamingPullResponse> callable = client.streamingPullCallable();
ApiStreamObserver<StreamingPullRequest> requestObserver = callable.bidiStreamingCall(responseObserver);
requestObserver.onNext(request);
try {
List<StreamingPullResponse> actualResponses = responseObserver.future().get();
Assert.fail("No exception thrown");
} catch (ExecutionException e) {
Assert.assertTrue(e.getCause() instanceof StatusRuntimeException);
StatusRuntimeException statusException = (StatusRuntimeException) e.getCause();
Assert.assertEquals(Status.INVALID_ARGUMENT, statusException.getStatus());
}
}
use of com.google.pubsub.v1.StreamingPullResponse in project google-cloud-java by GoogleCloudPlatform.
the class SubscriptionAdminClientTest method streamingPullTest.
@Test
@SuppressWarnings("all")
public void streamingPullTest() throws Exception {
StreamingPullResponse expectedResponse = StreamingPullResponse.newBuilder().build();
mockSubscriber.addResponse(expectedResponse);
SubscriptionName subscription = SubscriptionName.create("[PROJECT]", "[SUBSCRIPTION]");
int streamAckDeadlineSeconds = 1875467245;
StreamingPullRequest request = StreamingPullRequest.newBuilder().setSubscriptionWithSubscriptionName(subscription).setStreamAckDeadlineSeconds(streamAckDeadlineSeconds).build();
MockStreamObserver<StreamingPullResponse> responseObserver = new MockStreamObserver<>();
StreamingCallable<StreamingPullRequest, StreamingPullResponse> callable = client.streamingPullCallable();
ApiStreamObserver<StreamingPullRequest> requestObserver = callable.bidiStreamingCall(responseObserver);
requestObserver.onNext(request);
requestObserver.onCompleted();
List<StreamingPullResponse> actualResponses = responseObserver.future().get();
Assert.assertEquals(1, actualResponses.size());
Assert.assertEquals(expectedResponse, actualResponses.get(0));
}
Aggregations