Search in sources :

Example 1 with StreamingPullResponse

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));
            }
        });
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReceivedMessage(com.google.pubsub.v1.ReceivedMessage) TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags) Span(brave.Span) StreamingPullRequest(com.google.pubsub.v1.StreamingPullRequest) AckReplyConsumer(com.google.cloud.pubsub.v1.AckReplyConsumer)

Example 2 with StreamingPullResponse

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());
    }
}
Also used : StreamingPullResponse(com.google.pubsub.v1.StreamingPullResponse) StatusRuntimeException(io.grpc.StatusRuntimeException) SubscriptionName(com.google.pubsub.v1.SubscriptionName) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) ExecutionException(java.util.concurrent.ExecutionException) StreamingPullRequest(com.google.pubsub.v1.StreamingPullRequest) Test(org.junit.Test)

Example 3 with StreamingPullResponse

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));
}
Also used : StreamingPullResponse(com.google.pubsub.v1.StreamingPullResponse) SubscriptionName(com.google.pubsub.v1.SubscriptionName) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) StreamingPullRequest(com.google.pubsub.v1.StreamingPullRequest) Test(org.junit.Test)

Aggregations

StreamingPullRequest (com.google.pubsub.v1.StreamingPullRequest)3 MockStreamObserver (com.google.api.gax.grpc.testing.MockStreamObserver)2 StreamingPullResponse (com.google.pubsub.v1.StreamingPullResponse)2 SubscriptionName (com.google.pubsub.v1.SubscriptionName)2 Test (org.junit.Test)2 Span (brave.Span)1 TraceContextOrSamplingFlags (brave.propagation.TraceContextOrSamplingFlags)1 AckReplyConsumer (com.google.cloud.pubsub.v1.AckReplyConsumer)1 ReceivedMessage (com.google.pubsub.v1.ReceivedMessage)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1