Search in sources :

Example 6 with PubsubMessage

use of com.google.api.services.pubsub.model.PubsubMessage in project DataflowJavaSDK-examples by GoogleCloudPlatform.

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 7 with PubsubMessage

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

the class PubsubJsonClient method pull.

@Override
public List<IncomingMessage> pull(long requestTimeMsSinceEpoch, SubscriptionPath subscription, int batchSize, boolean returnImmediately) throws IOException {
    PullRequest request = new PullRequest().setReturnImmediately(returnImmediately).setMaxMessages(batchSize);
    PullResponse response = pubsub.projects().subscriptions().pull(subscription.getPath(), request).execute();
    if (response.getReceivedMessages() == null || response.getReceivedMessages().size() == 0) {
        return ImmutableList.of();
    }
    List<IncomingMessage> incomingMessages = new ArrayList<>(response.getReceivedMessages().size());
    for (ReceivedMessage message : response.getReceivedMessages()) {
        PubsubMessage pubsubMessage = message.getMessage();
        @Nullable Map<String, String> attributes = pubsubMessage.getAttributes();
        // Payload.
        byte[] elementBytes = pubsubMessage.decodeData();
        // Timestamp.
        long timestampMsSinceEpoch = extractTimestamp(timestampAttribute, message.getMessage().getPublishTime(), attributes);
        // Ack id.
        String ackId = message.getAckId();
        checkState(!Strings.isNullOrEmpty(ackId));
        // Record id, if any.
        @Nullable String recordId = null;
        if (idAttribute != null && attributes != null) {
            recordId = attributes.get(idAttribute);
        }
        if (Strings.isNullOrEmpty(recordId)) {
            // Fall back to the Pubsub provided message id.
            recordId = pubsubMessage.getMessageId();
        }
        incomingMessages.add(new IncomingMessage(elementBytes, attributes, timestampMsSinceEpoch, requestTimeMsSinceEpoch, ackId, recordId));
    }
    return incomingMessages;
}
Also used : PullResponse(com.google.api.services.pubsub.model.PullResponse) PullRequest(com.google.api.services.pubsub.model.PullRequest) ArrayList(java.util.ArrayList) ReceivedMessage(com.google.api.services.pubsub.model.ReceivedMessage) PubsubMessage(com.google.api.services.pubsub.model.PubsubMessage) Nullable(javax.annotation.Nullable)

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