Search in sources :

Example 41 with PubsubMessage

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

the class GoogleCloudPubSubFlusherTest method getFirstPublishedMessage.

private PubsubMessage getFirstPublishedMessage() {
    final Publisher publisher = mockPublisher.orElseThrow(IllegalStateException::new);
    final ArgumentCaptor<PubsubMessage> argumentCaptor = ArgumentCaptor.forClass(PubsubMessage.class);
    verify(publisher).publish(argumentCaptor.capture());
    return argumentCaptor.getValue();
}
Also used : Publisher(com.google.cloud.pubsub.v1.Publisher) PubsubMessage(com.google.pubsub.v1.PubsubMessage)

Example 42 with PubsubMessage

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

the class GoogleCloudPubSubFlusherTest method testMessagesHavePartyIdAttribute.

@Test
public void testMessagesHavePartyIdAttribute() {
    processSingleMessage();
    final PubsubMessage deliveredMessage = getFirstPublishedMessage();
    assertEquals(partyId.orElseThrow(IllegalStateException::new).toString(), deliveredMessage.getAttributesOrThrow("partyIdentifier"));
}
Also used : PubsubMessage(com.google.pubsub.v1.PubsubMessage) Test(org.junit.Test)

Example 43 with PubsubMessage

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

the class SubscriberExample method main.

/**
 * Receive messages over a subscription.
 */
public static void main(String... args) throws Exception {
    // set subscriber id, eg. my-sub
    String subscriptionId = args[0];
    ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(PROJECT_ID, subscriptionId);
    Subscriber subscriber = null;
    try {
        // create a subscriber bound to the asynchronous message receiver
        subscriber = Subscriber.newBuilder(subscriptionName, new MessageReceiverExample()).build();
        subscriber.startAsync().awaitRunning();
        // Continue to listen to messages
        while (true) {
            PubsubMessage message = messages.take();
            System.out.println("Message Id: " + message.getMessageId());
            System.out.println("Data: " + message.getData().toStringUtf8());
        }
    } finally {
        if (subscriber != null) {
            subscriber.stopAsync();
        }
    }
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) Subscriber(com.google.cloud.pubsub.v1.Subscriber) PubsubMessage(com.google.pubsub.v1.PubsubMessage)

Example 44 with PubsubMessage

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

the class PubSubPublishTest method servletPublishesPayloadMessage.

@Test
public void servletPublishesPayloadMessage() throws Exception {
    assertNotNull(System.getenv("PUBSUB_TOPIC"));
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getParameter("payload")).thenReturn("test-message");
    HttpServletResponse response = mock(HttpServletResponse.class);
    Publisher publisher = mock(Publisher.class);
    PubsubMessage message = PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8("test-message")).build();
    when(publisher.publish(eq(message))).thenReturn(SettableApiFuture.create());
    PubSubPublish pubSubPublish = new PubSubPublish(publisher);
    // verify content of published test message
    pubSubPublish.doPost(request, response);
    verify(publisher, times(1)).publish(eq(message));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Publisher(com.google.cloud.pubsub.v1.Publisher) PubsubMessage(com.google.pubsub.v1.PubsubMessage) Test(org.junit.Test)

Example 45 with PubsubMessage

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

the class PubSubPublish method doPost.

@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    Publisher publisher = this.publisher;
    // construct a pubsub message from the payload
    final String payload = req.getParameter("payload");
    Message message = new Message(null);
    message.setData(payload);
    PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8(payload)).putAttributes("sourceLang", req.getParameter("sourceLang")).putAttributes("targetLang", req.getParameter("targetLang")).build();
    String topicId = System.getenv("PUBSUB_TOPIC");
    // create a publisher on the topic
    if (publisher == null) {
        this.publisher = publisher = Publisher.newBuilder(ProjectTopicName.newBuilder().setProject(ServiceOptions.getDefaultProjectId()).setTopic(topicId).build()).build();
    }
    publisher.publish(pubsubMessage);
    // redirect to home page
    resp.sendRedirect("/");
}
Also used : PubsubMessage(com.google.pubsub.v1.PubsubMessage) Publisher(com.google.cloud.pubsub.v1.Publisher) ByteString(com.google.protobuf.ByteString) PubsubMessage(com.google.pubsub.v1.PubsubMessage)

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