Search in sources :

Example 36 with PubsubMessage

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

the class SubscriberSnippets method createSubscriber.

private void createSubscriber() throws Exception {
    // [START pubsub_pull]
    String projectId = "my-project-id";
    String subscriptionId = "my-subscription-id";
    SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
    // Instantiate an asynchronous message receiver
    MessageReceiver receiver = new MessageReceiver() {

        @Override
        public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
            // handle incoming message, then ack/nack the received message
            System.out.println("Id : " + message.getMessageId());
            System.out.println("Data : " + message.getData().toStringUtf8());
            consumer.ack();
        }
    };
    Subscriber subscriber = null;
    try {
        // Create a subscriber for "my-subscription-id" bound to the message receiver
        subscriber = Subscriber.defaultBuilder(subscriptionName, receiver).build();
        subscriber.startAsync();
    // ...
    } finally {
        // stop receiving messages
        if (subscriber != null) {
            subscriber.stopAsync();
        }
    }
// [END pubsub_pull]
}
Also used : MessageReceiver(com.google.cloud.pubsub.spi.v1.MessageReceiver) Subscriber(com.google.cloud.pubsub.spi.v1.Subscriber) SubscriptionName(com.google.pubsub.v1.SubscriptionName) AckReplyConsumer(com.google.cloud.pubsub.spi.v1.AckReplyConsumer) PubsubMessage(com.google.pubsub.v1.PubsubMessage)

Example 37 with PubsubMessage

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

the class TopicAdminClientTest method publishTest.

@Test
@SuppressWarnings("all")
public void publishTest() {
    String messageIdsElement = "messageIdsElement-744837059";
    List<String> messageIds = Arrays.asList(messageIdsElement);
    PublishResponse expectedResponse = PublishResponse.newBuilder().addAllMessageIds(messageIds).build();
    mockPublisher.addResponse(expectedResponse);
    TopicName topic = TopicName.create("[PROJECT]", "[TOPIC]");
    ByteString data = ByteString.copyFromUtf8("-86");
    PubsubMessage messagesElement = PubsubMessage.newBuilder().setData(data).build();
    List<PubsubMessage> messages = Arrays.asList(messagesElement);
    PublishResponse actualResponse = client.publish(topic, messages);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockPublisher.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    PublishRequest actualRequest = (PublishRequest) actualRequests.get(0);
    Assert.assertEquals(topic, actualRequest.getTopicAsTopicName());
    Assert.assertEquals(messages, actualRequest.getMessagesList());
}
Also used : PublishResponse(com.google.pubsub.v1.PublishResponse) ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) PublishRequest(com.google.pubsub.v1.PublishRequest) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) PubsubMessage(com.google.pubsub.v1.PubsubMessage) TopicName(com.google.pubsub.v1.TopicName) Test(org.junit.Test)

Example 38 with PubsubMessage

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

the class TopicAdminClientTest method publishExceptionTest.

@Test
@SuppressWarnings("all")
public void publishExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
    mockPublisher.addException(exception);
    try {
        TopicName topic = TopicName.create("[PROJECT]", "[TOPIC]");
        ByteString data = ByteString.copyFromUtf8("-86");
        PubsubMessage messagesElement = PubsubMessage.newBuilder().setData(data).build();
        List<PubsubMessage> messages = Arrays.asList(messagesElement);
        client.publish(topic, messages);
        Assert.fail("No exception raised");
    } catch (ApiException e) {
        Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode());
    }
}
Also used : ByteString(com.google.protobuf.ByteString) StatusRuntimeException(io.grpc.StatusRuntimeException) PubsubMessage(com.google.pubsub.v1.PubsubMessage) TopicName(com.google.pubsub.v1.TopicName) ApiException(com.google.api.gax.grpc.ApiException) Test(org.junit.Test)

Example 39 with PubsubMessage

use of com.google.pubsub.v1.PubsubMessage in project divolte-collector by divolte.

the class GoogleCloudPubSubFlusher method sendBatch.

@Override
protected ImmutableList<PubsubMessage> sendBatch(final List<PubsubMessage> batch) throws InterruptedException {
    // For Pub/Sub we assume the following:
    // - Batching behaviour is set to flush everything ASAP.
    // - Retry behaviour will retry indefinitely, so long as it seems likely to succeed.
    // First start sending the messages.
    // (This will serialize them, determine the partition and then assign them to a per-partition buffer.)
    final int batchSize = batch.size();
    final List<ApiFuture<String>> sendResults = batch.stream().map(publisher::publish).collect(Collectors.toCollection(() -> new ArrayList<>(batchSize)));
    // At this point the messages are in flight, and we assume being flushed.
    // When they eventually complete, each message can be in one of several states:
    // - Completed.
    // - An error occurred, but a retry may succeed.
    // - A fatal error occurred.
    final ImmutableList.Builder<PubsubMessage> remaining = ImmutableList.builder();
    for (int i = 0; i < batchSize; ++i) {
        final ApiFuture<String> pendingResult = sendResults.get(i);
        try {
            final String messageId = pendingResult.get();
            if (logger.isDebugEnabled()) {
                final PubsubMessage message = batch.get(i);
                logger.debug("Finished sending event (partyId={}) to Pub/Sub: messageId = {}", message.getAttributesOrThrow(MESSAGE_ATTRIBUTE_PARTYID), messageId);
            }
        } catch (final ExecutionException e) {
            final PubsubMessage message = batch.get(i);
            // The Pub/Sub publisher internally has a retry policy, but outside that we also
            // retry indefinitely unless it's a cause that we don't understand.
            final Throwable cause = e.getCause();
            if (cause instanceof ApiException) {
                final ApiException apiException = (ApiException) cause;
                if (apiException.isRetryable()) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Transient error sending event (partyId=" + message.getAttributesOrThrow(MESSAGE_ATTRIBUTE_PARTYID) + ") to Pub/Sub; retrying.", cause);
                    }
                    remaining.add(message);
                } else {
                    logger.warn("Permanent error sending event (partyId=" + message.getAttributesOrThrow(MESSAGE_ATTRIBUTE_PARTYID) + ") to Pub/Sub; abandoning.", cause);
                }
            } else {
                logger.error("Unknown error sending event (partyId=" + message.getAttributesOrThrow(MESSAGE_ATTRIBUTE_PARTYID) + ") to Pub/Sub; abandoning.", cause);
            }
        }
    }
    return remaining.build();
}
Also used : ApiFuture(com.google.api.core.ApiFuture) ImmutableList(com.google.common.collect.ImmutableList) ByteString(com.google.protobuf.ByteString) ExecutionException(java.util.concurrent.ExecutionException) PubsubMessage(com.google.pubsub.v1.PubsubMessage) ApiException(com.google.api.gax.rpc.ApiException)

Example 40 with PubsubMessage

use of com.google.pubsub.v1.PubsubMessage in project divolte-collector by divolte.

the class GoogleCloudPubSubFlusherTest method testMessageBodyIsNakedAvroRecord.

@Test
public void testMessageBodyIsNakedAvroRecord() throws IOException {
    processSingleMessage();
    final PubsubMessage deliveredMessage = getFirstPublishedMessage();
    final ByteString body = deliveredMessage.getData();
    final DatumReader<GenericRecord> reader = new GenericDatumReader<>(MINIMAL_SCHEMA);
    final Decoder decoder = DecoderFactory.get().binaryDecoder(body.newInput(), null);
    final GenericRecord record = reader.read(null, decoder);
    assertEquals(partyId.orElseThrow(IllegalStateException::new).toString(), record.get("partyId").toString());
    assertEquals(sessionId.orElseThrow(IllegalStateException::new).toString(), record.get("sessionId").toString());
    assertEquals(0L, record.get("counter"));
}
Also used : ByteString(com.google.protobuf.ByteString) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) GenericRecord(org.apache.avro.generic.GenericRecord) Decoder(org.apache.avro.io.Decoder) PubsubMessage(com.google.pubsub.v1.PubsubMessage) Test(org.junit.Test)

Aggregations

PubsubMessage (com.google.pubsub.v1.PubsubMessage)51 ByteString (com.google.protobuf.ByteString)28 Test (org.junit.Test)26 ArrayList (java.util.ArrayList)15 Subscriber (com.google.cloud.pubsub.v1.Subscriber)12 List (java.util.List)12 AcknowledgeablePubsubMessage (org.springframework.cloud.gcp.pubsub.support.AcknowledgeablePubsubMessage)12 Publisher (com.google.cloud.pubsub.v1.Publisher)11 ConvertedAcknowledgeablePubsubMessage (org.springframework.cloud.gcp.pubsub.support.converter.ConvertedAcknowledgeablePubsubMessage)11 ProjectTopicName (com.google.pubsub.v1.ProjectTopicName)10 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)9 BasicAcknowledgeablePubsubMessage (org.springframework.cloud.gcp.pubsub.support.BasicAcknowledgeablePubsubMessage)9 ConvertedBasicAcknowledgeablePubsubMessage (org.springframework.cloud.gcp.pubsub.support.converter.ConvertedBasicAcknowledgeablePubsubMessage)9 SettableApiFuture (com.google.api.core.SettableApiFuture)8 ServiceOptions (com.google.cloud.ServiceOptions)8 DlpServiceClient (com.google.cloud.dlp.v2.DlpServiceClient)8 Action (com.google.privacy.dlp.v2.Action)8 BigQueryTable (com.google.privacy.dlp.v2.BigQueryTable)8 CreateDlpJobRequest (com.google.privacy.dlp.v2.CreateDlpJobRequest)8 DlpJob (com.google.privacy.dlp.v2.DlpJob)8