Search in sources :

Example 1 with PubsubMessage

use of com.google.api.services.pubsub.model.PubsubMessage in project camel by apache.

the class GooglePubsubProducer method sendMessages.

private void sendMessages(List<Exchange> exchanges) throws Exception {
    GooglePubsubEndpoint endpoint = (GooglePubsubEndpoint) getEndpoint();
    String topicName = String.format("projects/%s/topics/%s", endpoint.getProjectId(), endpoint.getDestinationName());
    List<PubsubMessage> messages = new ArrayList<>();
    for (Exchange exchange : exchanges) {
        PubsubMessage message = new PubsubMessage();
        Object body = exchange.getIn().getBody();
        if (body instanceof String) {
            message.encodeData(((String) body).getBytes("UTF-8"));
        } else if (body instanceof byte[]) {
            message.encodeData((byte[]) body);
        } else {
            message.encodeData(serialize(body));
        }
        Object attributes = exchange.getIn().getHeader(GooglePubsubConstants.ATTRIBUTES);
        if (attributes != null && attributes instanceof Map && ((Map) attributes).size() > 0) {
            message.setAttributes((Map) attributes);
        }
        messages.add(message);
    }
    PublishRequest publishRequest = new PublishRequest().setMessages(messages);
    PublishResponse response = endpoint.getPubsub().projects().topics().publish(topicName, publishRequest).execute();
    List<String> sentMessageIds = response.getMessageIds();
    int i = 0;
    for (Exchange entry : exchanges) {
        entry.getIn().setHeader(GooglePubsubConstants.MESSAGE_ID, sentMessageIds.get(i));
        i++;
    }
}
Also used : PublishResponse(com.google.api.services.pubsub.model.PublishResponse) ArrayList(java.util.ArrayList) PublishRequest(com.google.api.services.pubsub.model.PublishRequest) PubsubMessage(com.google.api.services.pubsub.model.PubsubMessage) Exchange(org.apache.camel.Exchange) Map(java.util.Map)

Example 2 with PubsubMessage

use of com.google.api.services.pubsub.model.PubsubMessage in project beam by apache.

the class Injector method publishData.

/**
   * Publish 'numMessages' arbitrary events from live users with the provided delay, to a
   * PubSub topic.
   */
public static void publishData(int numMessages, int delayInMillis) throws IOException {
    List<PubsubMessage> pubsubMessages = new ArrayList<>();
    for (int i = 0; i < Math.max(1, numMessages); i++) {
        Long currTime = System.currentTimeMillis();
        String message = generateEvent(currTime, delayInMillis);
        PubsubMessage pubsubMessage = new PubsubMessage().encodeData(message.getBytes("UTF-8"));
        pubsubMessage.setAttributes(ImmutableMap.of(TIMESTAMP_ATTRIBUTE, Long.toString((currTime - delayInMillis) / 1000 * 1000)));
        if (delayInMillis != 0) {
            System.out.println(pubsubMessage.getAttributes());
            System.out.println("late data for: " + message);
        }
        pubsubMessages.add(pubsubMessage);
    }
    PublishRequest publishRequest = new PublishRequest();
    publishRequest.setMessages(pubsubMessages);
    pubsub.projects().topics().publish(topic, publishRequest).execute();
}
Also used : ArrayList(java.util.ArrayList) PublishRequest(com.google.api.services.pubsub.model.PublishRequest) PubsubMessage(com.google.api.services.pubsub.model.PubsubMessage)

Example 3 with PubsubMessage

use of com.google.api.services.pubsub.model.PubsubMessage in project beam by apache.

the class PubsubJsonClient method publish.

@Override
public int publish(TopicPath topic, List<OutgoingMessage> outgoingMessages) throws IOException {
    List<PubsubMessage> pubsubMessages = new ArrayList<>(outgoingMessages.size());
    for (OutgoingMessage outgoingMessage : outgoingMessages) {
        PubsubMessage pubsubMessage = new PubsubMessage().encodeData(outgoingMessage.elementBytes);
        Map<String, String> attributes = outgoingMessage.attributes;
        if ((timestampAttribute != null || idAttribute != null) && attributes == null) {
            attributes = new TreeMap<>();
        }
        if (attributes != null) {
            pubsubMessage.setAttributes(attributes);
        }
        if (timestampAttribute != null) {
            attributes.put(timestampAttribute, String.valueOf(outgoingMessage.timestampMsSinceEpoch));
        }
        if (idAttribute != null && !Strings.isNullOrEmpty(outgoingMessage.recordId)) {
            attributes.put(idAttribute, outgoingMessage.recordId);
        }
        pubsubMessages.add(pubsubMessage);
    }
    PublishRequest request = new PublishRequest().setMessages(pubsubMessages);
    PublishResponse response = pubsub.projects().topics().publish(topic.getPath(), request).execute();
    return response.getMessageIds().size();
}
Also used : PublishResponse(com.google.api.services.pubsub.model.PublishResponse) ArrayList(java.util.ArrayList) PublishRequest(com.google.api.services.pubsub.model.PublishRequest) PubsubMessage(com.google.api.services.pubsub.model.PubsubMessage)

Example 4 with PubsubMessage

use of com.google.api.services.pubsub.model.PubsubMessage in project beam by apache.

the class PubsubJsonClientTest method pullOneMessage.

@Test
public void pullOneMessage() throws IOException {
    String expectedSubscription = SUBSCRIPTION.getPath();
    PullRequest expectedRequest = new PullRequest().setReturnImmediately(true).setMaxMessages(10);
    PubsubMessage expectedPubsubMessage = new PubsubMessage().setMessageId(MESSAGE_ID).encodeData(DATA.getBytes()).setPublishTime(String.valueOf(PUB_TIME)).setAttributes(ImmutableMap.of(TIMESTAMP_ATTRIBUTE, String.valueOf(MESSAGE_TIME), ID_ATTRIBUTE, RECORD_ID));
    ReceivedMessage expectedReceivedMessage = new ReceivedMessage().setMessage(expectedPubsubMessage).setAckId(ACK_ID);
    PullResponse expectedResponse = new PullResponse().setReceivedMessages(ImmutableList.of(expectedReceivedMessage));
    Mockito.when((Object) (mockPubsub.projects().subscriptions().pull(expectedSubscription, expectedRequest).execute())).thenReturn(expectedResponse);
    List<IncomingMessage> acutalMessages = client.pull(REQ_TIME, SUBSCRIPTION, 10, true);
    assertEquals(1, acutalMessages.size());
    IncomingMessage actualMessage = acutalMessages.get(0);
    assertEquals(ACK_ID, actualMessage.ackId);
    assertEquals(DATA, new String(actualMessage.elementBytes));
    assertEquals(RECORD_ID, actualMessage.recordId);
    assertEquals(REQ_TIME, actualMessage.requestTimeMsSinceEpoch);
    assertEquals(MESSAGE_TIME, actualMessage.timestampMsSinceEpoch);
}
Also used : PullResponse(com.google.api.services.pubsub.model.PullResponse) IncomingMessage(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.IncomingMessage) PullRequest(com.google.api.services.pubsub.model.PullRequest) ReceivedMessage(com.google.api.services.pubsub.model.ReceivedMessage) PubsubMessage(com.google.api.services.pubsub.model.PubsubMessage) Test(org.junit.Test)

Example 5 with PubsubMessage

use of com.google.api.services.pubsub.model.PubsubMessage in project beam by apache.

the class PubsubJsonClientTest method publishOneMessage.

@Test
public void publishOneMessage() throws IOException {
    String expectedTopic = TOPIC.getPath();
    PubsubMessage expectedPubsubMessage = new PubsubMessage().encodeData(DATA.getBytes()).setAttributes(ImmutableMap.<String, String>builder().put(TIMESTAMP_ATTRIBUTE, String.valueOf(MESSAGE_TIME)).put(ID_ATTRIBUTE, RECORD_ID).put("k", "v").build());
    PublishRequest expectedRequest = new PublishRequest().setMessages(ImmutableList.of(expectedPubsubMessage));
    PublishResponse expectedResponse = new PublishResponse().setMessageIds(ImmutableList.of(MESSAGE_ID));
    Mockito.when((Object) (mockPubsub.projects().topics().publish(expectedTopic, expectedRequest).execute())).thenReturn(expectedResponse);
    Map<String, String> attrs = new HashMap<>();
    attrs.put("k", "v");
    OutgoingMessage actualMessage = new OutgoingMessage(DATA.getBytes(), attrs, MESSAGE_TIME, RECORD_ID);
    int n = client.publish(TOPIC, ImmutableList.of(actualMessage));
    assertEquals(1, n);
}
Also used : PublishResponse(com.google.api.services.pubsub.model.PublishResponse) OutgoingMessage(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage) HashMap(java.util.HashMap) PublishRequest(com.google.api.services.pubsub.model.PublishRequest) PubsubMessage(com.google.api.services.pubsub.model.PubsubMessage) Test(org.junit.Test)

Aggregations

PubsubMessage (com.google.api.services.pubsub.model.PubsubMessage)7 PublishRequest (com.google.api.services.pubsub.model.PublishRequest)5 ArrayList (java.util.ArrayList)5 PublishResponse (com.google.api.services.pubsub.model.PublishResponse)3 PullRequest (com.google.api.services.pubsub.model.PullRequest)2 PullResponse (com.google.api.services.pubsub.model.PullResponse)2 ReceivedMessage (com.google.api.services.pubsub.model.ReceivedMessage)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Nullable (javax.annotation.Nullable)1 IncomingMessage (org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.IncomingMessage)1 OutgoingMessage (org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage)1 Exchange (org.apache.camel.Exchange)1