Class PubsubClient

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable
    Direct Known Subclasses:
    PubsubGrpcClient, PubsubJsonClient, PubsubTestClient

    public abstract class PubsubClient
    extends java.lang.Object
    implements java.io.Closeable
    An (abstract) helper class for talking to Pubsub via an underlying transport.
    • Constructor Detail

      • PubsubClient

        public PubsubClient()
    • Method Detail

      • parseTimestampAsMsSinceEpoch

        protected static java.lang.Long parseTimestampAsMsSinceEpoch​(java.lang.String timestamp)
        Return timestamp as ms-since-unix-epoch corresponding to timestamp. Throw IllegalArgumentException if timestamp cannot be recognized.
      • extractTimestampAttribute

        protected static long extractTimestampAttribute​(java.lang.String timestampAttribute,
                                                        @Nullable java.util.Map<java.lang.String,​java.lang.String> attributes)
        Return the timestamp (in ms since unix epoch) to use for a Pubsub message with timestampAttribute and attriutes.

        The message attributes must contain timestampAttribute, and the value of that attribute will be taken as the timestamp.

        Throws:
        java.lang.IllegalArgumentException - if the timestamp cannot be recognized as a ms-since-unix-epoch or RFC3339 time.
      • schemaPathFromId

        public static PubsubClient.SchemaPath schemaPathFromId​(java.lang.String projectId,
                                                               java.lang.String schemaId)
      • subscriptionPathFromName

        public static PubsubClient.SubscriptionPath subscriptionPathFromName​(java.lang.String projectId,
                                                                             java.lang.String subscriptionName)
      • topicPathFromName

        public static PubsubClient.TopicPath topicPathFromName​(java.lang.String projectId,
                                                               java.lang.String topicName)
      • publish

        public abstract int publish​(PubsubClient.TopicPath topic,
                                    java.util.List<PubsubClient.OutgoingMessage> outgoingMessages)
                             throws java.io.IOException
        Publish outgoingMessages to Pubsub topic. Return number of messages published.
        Throws:
        java.io.IOException
      • pull

        public abstract java.util.List<PubsubClient.IncomingMessage> pull​(long requestTimeMsSinceEpoch,
                                                                          PubsubClient.SubscriptionPath subscription,
                                                                          int batchSize,
                                                                          boolean returnImmediately)
                                                                   throws java.io.IOException
        Request the next batch of up to batchSize messages from subscription. Return the received messages, or empty collection if none were available. Does not wait for messages to arrive if returnImmediately is true. Returned messages will record their request time as requestTimeMsSinceEpoch.
        Throws:
        java.io.IOException
      • acknowledge

        public abstract void acknowledge​(PubsubClient.SubscriptionPath subscription,
                                         java.util.List<java.lang.String> ackIds)
                                  throws java.io.IOException
        Acknowldege messages from subscription with ackIds.
        Throws:
        java.io.IOException
      • modifyAckDeadline

        public abstract void modifyAckDeadline​(PubsubClient.SubscriptionPath subscription,
                                               java.util.List<java.lang.String> ackIds,
                                               int deadlineSeconds)
                                        throws java.io.IOException
        Modify the ack deadline for messages from subscription with ackIds to be deadlineSeconds from now.
        Throws:
        java.io.IOException
      • createTopic

        public abstract void createTopic​(PubsubClient.TopicPath topic)
                                  throws java.io.IOException
        Create topic.
        Throws:
        java.io.IOException
      • deleteTopic

        public abstract void deleteTopic​(PubsubClient.TopicPath topic)
                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • isTopicExists

        public abstract boolean isTopicExists​(PubsubClient.TopicPath topic)
                                       throws java.io.IOException
        Return true if topic exists.
        Throws:
        java.io.IOException
      • deleteSubscription

        public abstract void deleteSubscription​(PubsubClient.SubscriptionPath subscription)
                                         throws java.io.IOException
        Delete subscription.
        Throws:
        java.io.IOException
      • ackDeadlineSeconds

        public abstract int ackDeadlineSeconds​(PubsubClient.SubscriptionPath subscription)
                                        throws java.io.IOException
        Return the ack deadline, in seconds, for subscription.
        Throws:
        java.io.IOException
      • createSchema

        public abstract void createSchema​(PubsubClient.SchemaPath schemaPath,
                                          java.lang.String schemaContent,
                                          com.google.pubsub.v1.Schema.Type type)
                                   throws java.io.IOException
        Create Schema from Schema definition content.
        Throws:
        java.io.IOException
      • getSchema

        public abstract org.apache.beam.sdk.schemas.Schema getSchema​(PubsubClient.SchemaPath schemaPath)
                                                              throws java.io.IOException
        Return a Beam Schema from the Pub/Sub schema resource, if exists.
        Throws:
        java.io.IOException