Search in sources :

Example 1 with ReceivedMessage

use of com.google.pubsub.v1.ReceivedMessage in project google-cloud-java by GoogleCloudPlatform.

the class MessageDispatcher method processReceivedMessages.

public void processReceivedMessages(List<ReceivedMessage> messages, Runnable doneCallback) {
    if (messages.isEmpty()) {
        doneCallback.run();
        return;
    }
    messagesWaiter.incrementPendingMessages(messages.size());
    OutstandingMessagesBatch outstandingBatch = new OutstandingMessagesBatch(doneCallback);
    final ArrayList<AckHandler> ackHandlers = new ArrayList<>(messages.size());
    for (ReceivedMessage message : messages) {
        AckHandler ackHandler = new AckHandler(message.getAckId(), message.getMessage().getSerializedSize());
        ackHandlers.add(ackHandler);
        outstandingBatch.addMessage(message, ackHandler);
    }
    Instant expiration = Instant.ofEpochMilli(clock.millisTime()).plusSeconds(messageDeadlineSeconds);
    synchronized (outstandingAckHandlers) {
        outstandingAckHandlers.add(new ExtensionJob(Instant.ofEpochMilli(clock.millisTime()), expiration, INITIAL_ACK_DEADLINE_EXTENSION_SECONDS, ackHandlers));
    }
    setupNextAckDeadlineExtensionAlarm(expiration);
    synchronized (outstandingMessageBatches) {
        outstandingMessageBatches.add(outstandingBatch);
    }
    processOutstandingBatches();
}
Also used : Instant(org.threeten.bp.Instant) ArrayList(java.util.ArrayList) ReceivedMessage(com.google.pubsub.v1.ReceivedMessage)

Example 2 with ReceivedMessage

use of com.google.pubsub.v1.ReceivedMessage in project spring-cloud-gcp by spring-cloud.

the class PubSubTemplate method pull.

/**
 * Pulls messages synchronously, on demand, using the pull request in argument.
 *
 * <p>This method acknowledges all received messages.
 * @param pullRequest pull request containing the subscription name
 * @return the list of {@link PubsubMessage} containing the headers and payload
 */
private List<PubsubMessage> pull(PullRequest pullRequest, RetrySettings retrySettings) {
    Assert.notNull(pullRequest, "The pull request cannot be null.");
    try {
        SubscriberStub subscriber = this.subscriberFactory.createSubscriberStub(retrySettings);
        Assert.notNull(subscriber, "A SubscriberStub is needed to execute the pull request.");
        PullResponse pullResponse = subscriber.pullCallable().call(pullRequest);
        // Ack received messages.
        if (pullResponse.getReceivedMessagesCount() > 0) {
            List<String> ackIds = pullResponse.getReceivedMessagesList().stream().map(ReceivedMessage::getAckId).collect(Collectors.toList());
            AcknowledgeRequest acknowledgeRequest = AcknowledgeRequest.newBuilder().setSubscription(pullRequest.getSubscription()).addAllAckIds(ackIds).build();
            subscriber.acknowledgeCallable().call(acknowledgeRequest);
        }
        return pullResponse.getReceivedMessagesList().stream().map(ReceivedMessage::getMessage).collect(Collectors.toList());
    } catch (Exception ioe) {
        throw new PubSubException("Error pulling messages from subscription " + pullRequest.getSubscription() + ".", ioe);
    }
}
Also used : PullResponse(com.google.pubsub.v1.PullResponse) AcknowledgeRequest(com.google.pubsub.v1.AcknowledgeRequest) SubscriberStub(com.google.cloud.pubsub.v1.stub.SubscriberStub) ByteString(com.google.protobuf.ByteString)

Example 3 with ReceivedMessage

use of com.google.pubsub.v1.ReceivedMessage in project flink by apache.

the class PubSubSource method processMessage.

private void processMessage(SourceContext<OUT> sourceContext, List<ReceivedMessage> messages, PubSubCollector collector) throws Exception {
    rateLimiter.acquire(messages.size());
    synchronized (sourceContext.getCheckpointLock()) {
        for (ReceivedMessage message : messages) {
            acknowledgeOnCheckpoint.addAcknowledgeId(message.getAckId());
            PubsubMessage pubsubMessage = message.getMessage();
            deserializationSchema.deserialize(pubsubMessage, collector);
            if (collector.isEndOfStreamSignalled()) {
                cancel();
                return;
            }
        }
    }
}
Also used : ReceivedMessage(com.google.pubsub.v1.ReceivedMessage) PubsubMessage(com.google.pubsub.v1.PubsubMessage)

Example 4 with ReceivedMessage

use of com.google.pubsub.v1.ReceivedMessage in project beam by apache.

the class PubsubGrpcClientTest method pullOneMessage.

@Test
public void pullOneMessage() throws IOException {
    initializeClient(null, null);
    String expectedSubscription = SUBSCRIPTION.getPath();
    final PullRequest expectedRequest = PullRequest.newBuilder().setSubscription(expectedSubscription).setReturnImmediately(true).setMaxMessages(10).build();
    Timestamp timestamp = Timestamp.newBuilder().setSeconds(PUB_TIME_MS / 1000).setNanos((int) (PUB_TIME_MS % 1000) * 1000 * 1000).build();
    PubsubMessage expectedPubsubMessage = PubsubMessage.newBuilder().setMessageId(MESSAGE_ID).setData(ByteString.copyFrom(DATA.getBytes(StandardCharsets.UTF_8))).setPublishTime(timestamp).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> actualMessages = client.pull(REQ_TIME_MS, SUBSCRIPTION, 10, true);
        assertEquals(1, actualMessages.size());
        IncomingMessage actualMessage = actualMessages.get(0);
        assertEquals(ACK_ID, actualMessage.ackId());
        assertEquals(DATA, actualMessage.message().getData().toStringUtf8());
        assertEquals(MESSAGE_ID, actualMessage.recordId());
        assertEquals(REQ_TIME_MS, actualMessage.requestTimeMsSinceEpoch());
        assertEquals(PUB_TIME_MS, actualMessage.timestampMsSinceEpoch());
        assertEquals(expectedRequest, Iterables.getOnlyElement(requestsReceived));
    } finally {
        server.shutdownNow();
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) SubscriberImplBase(com.google.pubsub.v1.SubscriberGrpc.SubscriberImplBase) Server(io.grpc.Server) PullRequest(com.google.pubsub.v1.PullRequest) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) ReceivedMessage(com.google.pubsub.v1.ReceivedMessage) Timestamp(com.google.protobuf.Timestamp) PubsubMessage(com.google.pubsub.v1.PubsubMessage) PullResponse(com.google.pubsub.v1.PullResponse) IncomingMessage(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.IncomingMessage) Test(org.junit.Test)

Example 5 with ReceivedMessage

use of com.google.pubsub.v1.ReceivedMessage in project beam by apache.

the class PubsubGrpcClientTest method pullOneMessageUsingAttributes.

@Test
public void pullOneMessageUsingAttributes() throws IOException {
    initializeClient(TIMESTAMP_ATTRIBUTE, ID_ATTRIBUTE);
    String expectedSubscription = SUBSCRIPTION.getPath();
    final PullRequest expectedRequest = PullRequest.newBuilder().setSubscription(expectedSubscription).setReturnImmediately(true).setMaxMessages(10).build();
    Timestamp timestamp = Timestamp.newBuilder().setSeconds(PUB_TIME_MS / 1000).setNanos((int) (PUB_TIME_MS % 1000) * 1000 * 1000).build();
    PubsubMessage expectedPubsubMessage = PubsubMessage.newBuilder().setMessageId(MESSAGE_ID).setData(ByteString.copyFrom(DATA.getBytes(StandardCharsets.UTF_8))).setPublishTime(timestamp).putAllAttributes(ATTRIBUTES).putAllAttributes(ImmutableMap.of(TIMESTAMP_ATTRIBUTE, String.valueOf(MESSAGE_TIME_MS), 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> actualMessages = client.pull(REQ_TIME_MS, SUBSCRIPTION, 10, true);
        assertEquals(1, actualMessages.size());
        IncomingMessage actualMessage = actualMessages.get(0);
        assertEquals(ACK_ID, actualMessage.ackId());
        assertEquals(DATA, actualMessage.message().getData().toStringUtf8());
        assertEquals(RECORD_ID, actualMessage.recordId());
        assertEquals(REQ_TIME_MS, actualMessage.requestTimeMsSinceEpoch());
        assertEquals(MESSAGE_TIME_MS, actualMessage.timestampMsSinceEpoch());
        assertEquals(expectedRequest, Iterables.getOnlyElement(requestsReceived));
    } finally {
        server.shutdownNow();
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) SubscriberImplBase(com.google.pubsub.v1.SubscriberGrpc.SubscriberImplBase) Server(io.grpc.Server) PullRequest(com.google.pubsub.v1.PullRequest) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) ReceivedMessage(com.google.pubsub.v1.ReceivedMessage) Timestamp(com.google.protobuf.Timestamp) PubsubMessage(com.google.pubsub.v1.PubsubMessage) PullResponse(com.google.pubsub.v1.PullResponse) IncomingMessage(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.IncomingMessage) Test(org.junit.Test)

Aggregations

ReceivedMessage (com.google.pubsub.v1.ReceivedMessage)9 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)5 ByteString (com.google.protobuf.ByteString)4 PubsubMessage (com.google.pubsub.v1.PubsubMessage)4 PullRequest (com.google.pubsub.v1.PullRequest)4 PullResponse (com.google.pubsub.v1.PullResponse)4 Timestamp (com.google.protobuf.Timestamp)3 Publisher (com.google.cloud.pubsub.v1.Publisher)2 SubscriberStub (com.google.cloud.pubsub.v1.stub.SubscriberStub)2 AcknowledgeRequest (com.google.pubsub.v1.AcknowledgeRequest)2 SubscriberImplBase (com.google.pubsub.v1.SubscriberGrpc.SubscriberImplBase)2 Server (io.grpc.Server)2 StreamObserver (io.grpc.stub.StreamObserver)2 IncomingMessage (org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.IncomingMessage)2 SimpleStringSchema (org.apache.flink.api.common.serialization.SimpleStringSchema)2 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)2 GrpcSubscriberStub (com.google.cloud.pubsub.v1.stub.GrpcSubscriberStub)1 SubscriberStubSettings (com.google.cloud.pubsub.v1.stub.SubscriberStubSettings)1 StringUtils (org.apache.commons.lang3.StringUtils)1