Search in sources :

Example 56 with PubsubMessage

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

the class PubSubTemplateDocumentationTests method testCreatePublishPullNextAndDelete.

@Test
public void testCreatePublishPullNextAndDelete() {
    pubSubTest((PubSubTemplate pubSubTemplate, String subscriptionName, String topicName) -> {
        // tag::publish[]
        Map<String, String> headers = Collections.singletonMap("key1", "val1");
        pubSubTemplate.publish(topicName, "message", headers).get();
        // end::publish[]
        PubsubMessage pubsubMessage = pubSubTemplate.pullNext(subscriptionName);
        assertThat(pubsubMessage.getData()).isEqualTo(ByteString.copyFromUtf8("message"));
        assertThat(pubsubMessage.getAttributesCount()).isEqualTo(1);
        assertThat(pubsubMessage.getAttributesOrThrow("key1")).isEqualTo("val1");
    });
}
Also used : ByteString(com.google.protobuf.ByteString) PubSubTemplate(org.springframework.cloud.gcp.pubsub.core.PubSubTemplate) PubsubMessage(com.google.pubsub.v1.PubsubMessage) ConvertedAcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.converter.ConvertedAcknowledgeablePubsubMessage) AcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.AcknowledgeablePubsubMessage) Test(org.junit.Test)

Example 57 with PubsubMessage

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

the class SimplePubSubMessageConverter method toPubSubMessage.

@Override
public PubsubMessage toPubSubMessage(Object payload, Map<String, String> headers) {
    ByteString convertedPayload;
    if (payload instanceof ByteString) {
        convertedPayload = (ByteString) payload;
    } else if (payload instanceof String) {
        convertedPayload = ByteString.copyFrom(((String) payload).getBytes(this.charset));
    } else if (payload instanceof ByteBuffer) {
        convertedPayload = ByteString.copyFrom((ByteBuffer) payload);
    } else if (payload instanceof byte[]) {
        convertedPayload = ByteString.copyFrom((byte[]) payload);
    } else {
        throw new PubSubMessageConversionException("Unable to convert payload of type " + payload.getClass().getName() + " to byte[] for sending to Pub/Sub.");
    }
    PubsubMessage.Builder pubsubMessageBuilder = PubsubMessage.newBuilder().setData(convertedPayload);
    if (headers != null) {
        pubsubMessageBuilder.putAllAttributes(headers);
    }
    return pubsubMessageBuilder.build();
}
Also used : ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) ByteBuffer(java.nio.ByteBuffer) PubsubMessage(com.google.pubsub.v1.PubsubMessage)

Example 58 with PubsubMessage

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

the class PubSubReactiveFactoryTests method setUpMessages.

/**
 * Replays provided messages.
 * If a synthetic message "stop" is encountered, immediately returns previously collected messages.
 * If a synthetic message "timeout" is encountered, throws an {@link DeadlineExceededException}.
 * If a synthetic message "throw" is encountered, throws an {@link RuntimeException}.
 * Fails the calling test if there are not enough messages to fulfill demand from cumulative calls to {@code pull()}.
 * @param messages messages to replay
 */
private void setUpMessages(String... messages) {
    List<String> msgList = new ArrayList<>(Arrays.asList(messages));
    when(subscriberOperations.pullAsync(eq("sub1"), any(Integer.class), any(Boolean.class))).then(invocationOnMock -> {
        List<AcknowledgeablePubsubMessage> result = new ArrayList<>();
        for (int i = 0; i < (Integer) invocationOnMock.getArgument(1); i++) {
            if (msgList.isEmpty()) {
                fail("Ran out of provided messages.");
            }
            String nextPayload = msgList.remove(0);
            switch(nextPayload) {
                case "stop":
                    return AsyncResult.forValue(result);
                case "timeout":
                    if (!result.isEmpty()) {
                        fail("Bad setup -- 'throw' should be the first event in batch");
                    }
                    return AsyncResult.forExecutionException(new DeadlineExceededException("this is a noop", null, GrpcStatusCode.of(Status.Code.DEADLINE_EXCEEDED), true));
                case "throw":
                    return AsyncResult.forExecutionException(new RuntimeException("expected exception during pull of messages"));
            }
            AcknowledgeablePubsubMessage msg = mock(AcknowledgeablePubsubMessage.class);
            PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(ByteString.copyFrom((nextPayload).getBytes())).build();
            when(msg.getPubsubMessage()).thenReturn(pubsubMessage);
            result.add(msg);
        }
        return AsyncResult.forValue(result);
    });
}
Also used : ArrayList(java.util.ArrayList) DeadlineExceededException(com.google.api.gax.rpc.DeadlineExceededException) ByteString(com.google.protobuf.ByteString) AcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.AcknowledgeablePubsubMessage) PubsubMessage(com.google.pubsub.v1.PubsubMessage) AcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.AcknowledgeablePubsubMessage)

Example 59 with PubsubMessage

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

the class JacksonPubSubMessageConverterTests method testPojo.

@Test
public void testPojo() throws JSONException {
    Contact contact = new Contact("Thomas", "Edison", 8817);
    PubsubMessage pubsubMessage = this.converter.toPubSubMessage(contact, null);
    JSONAssert.assertEquals("{\"firstName\":\"Thomas\",\"lastName\":\"Edison\",\"zip\":8817}", pubsubMessage.getData().toStringUtf8(), true);
    Object o = this.converter.fromPubSubMessage(pubsubMessage, Contact.class);
    assertThat(o).as("verify that deserialized object is equal to the original one").isEqualTo(contact);
}
Also used : PubsubMessage(com.google.pubsub.v1.PubsubMessage) Test(org.junit.Test)

Example 60 with PubsubMessage

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

the class PubSubSubscriberTemplate method pullNextAsync.

@Override
public ListenableFuture<PubsubMessage> pullNextAsync(String subscription) {
    final SettableListenableFuture<PubsubMessage> settableFuture = new SettableListenableFuture<>();
    this.pullAndAckAsync(subscription, 1, true).addCallback(messages -> {
        PubsubMessage message = messages.isEmpty() ? null : messages.get(0);
        settableFuture.set(message);
    }, settableFuture::setException);
    return settableFuture;
}
Also used : SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) BasicAcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.BasicAcknowledgeablePubsubMessage) PubsubMessage(com.google.pubsub.v1.PubsubMessage) ConvertedAcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.converter.ConvertedAcknowledgeablePubsubMessage) AcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.AcknowledgeablePubsubMessage) ConvertedBasicAcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.converter.ConvertedBasicAcknowledgeablePubsubMessage)

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