Search in sources :

Example 1 with SubscriptionName

use of com.google.pubsub.v1.SubscriptionName in project google-cloud-java by GoogleCloudPlatform.

the class TopicAdminClientSnippets method listTopicSubscriptions.

/** Example of listing topics for a subscription. */
public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String topicId) throws Exception {
    // [START pubsub_list_topic_subscriptions]
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        TopicName topicName = TopicName.create(projectId, topicId);
        ListTopicSubscriptionsRequest request = ListTopicSubscriptionsRequest.newBuilder().setTopicWithTopicName(topicName).build();
        ListTopicSubscriptionsPagedResponse response = topicAdminClient.listTopicSubscriptions(request);
        Iterable<String> subscriptionNames = response.iterateAll();
        for (String subscriptionName : subscriptionNames) {
        // do something with the subscription name
        }
        return response;
    }
// [END pubsub_list_topic_subscriptions]
}
Also used : ListTopicSubscriptionsPagedResponse(com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicSubscriptionsPagedResponse) TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient) ListTopicSubscriptionsRequest(com.google.pubsub.v1.ListTopicSubscriptionsRequest) TopicName(com.google.pubsub.v1.TopicName)

Example 2 with SubscriptionName

use of com.google.pubsub.v1.SubscriptionName in project google-cloud-java by GoogleCloudPlatform.

the class SubscriptionAdminClient method deleteSubscription.

// AUTO-GENERATED DOCUMENTATION AND METHOD
/**
   * Deletes an existing subscription. All messages retained in the subscription are immediately
   * dropped. Calls to `Pull` after deletion will return `NOT_FOUND`. After a subscription is
   * deleted, a new one may be created with the same name, but the new one has no association with
   * the old subscription or its topic unless the same topic is specified.
   *
   * <p>Sample code:
   *
   * <pre><code>
   * try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
   *   SubscriptionName subscription = SubscriptionName.create("[PROJECT]", "[SUBSCRIPTION]");
   *   subscriptionAdminClient.deleteSubscription(subscription);
   * }
   * </code></pre>
   *
   * @param subscription The subscription to delete. Format is
   *     `projects/{project}/subscriptions/{sub}`.
   * @throws com.google.api.gax.grpc.ApiException if the remote call fails
   */
public final void deleteSubscription(SubscriptionName subscription) {
    DeleteSubscriptionRequest request = DeleteSubscriptionRequest.newBuilder().setSubscriptionWithSubscriptionName(subscription).build();
    deleteSubscription(request);
}
Also used : DeleteSubscriptionRequest(com.google.pubsub.v1.DeleteSubscriptionRequest)

Example 3 with SubscriptionName

use of com.google.pubsub.v1.SubscriptionName in project google-cloud-java by GoogleCloudPlatform.

the class SubscriptionAdminClient method modifyPushConfig.

// AUTO-GENERATED DOCUMENTATION AND METHOD
/**
   * Modifies the `PushConfig` for a specified subscription.
   *
   * <p>This may be used to change a push subscription to a pull one (signified by an empty
   * `PushConfig`) or vice versa, or change the endpoint URL and other attributes of a push
   * subscription. Messages will accumulate for delivery continuously through the call regardless of
   * changes to the `PushConfig`.
   *
   * <p>Sample code:
   *
   * <pre><code>
   * try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
   *   SubscriptionName subscription = SubscriptionName.create("[PROJECT]", "[SUBSCRIPTION]");
   *   PushConfig pushConfig = PushConfig.newBuilder().build();
   *   subscriptionAdminClient.modifyPushConfig(subscription, pushConfig);
   * }
   * </code></pre>
   *
   * @param subscription The name of the subscription. Format is
   *     `projects/{project}/subscriptions/{sub}`.
   * @param pushConfig The push configuration for future deliveries.
   *     <p>An empty `pushConfig` indicates that the Pub/Sub system should stop pushing messages
   *     from the given subscription and allow messages to be pulled and acknowledged - effectively
   *     pausing the subscription if `Pull` is not called.
   * @throws com.google.api.gax.grpc.ApiException if the remote call fails
   */
public final void modifyPushConfig(SubscriptionName subscription, PushConfig pushConfig) {
    ModifyPushConfigRequest request = ModifyPushConfigRequest.newBuilder().setSubscriptionWithSubscriptionName(subscription).setPushConfig(pushConfig).build();
    modifyPushConfig(request);
}
Also used : ModifyPushConfigRequest(com.google.pubsub.v1.ModifyPushConfigRequest)

Example 4 with SubscriptionName

use of com.google.pubsub.v1.SubscriptionName in project google-cloud-java by GoogleCloudPlatform.

the class SubscriptionAdminClient method modifyAckDeadline.

// AUTO-GENERATED DOCUMENTATION AND METHOD
/**
   * Modifies the ack deadline for a specific message. This method is useful to indicate that more
   * time is needed to process a message by the subscriber, or to make the message available for
   * redelivery if the processing was interrupted. Note that this does not modify the
   * subscription-level `ackDeadlineSeconds` used for subsequent messages.
   *
   * <p>Sample code:
   *
   * <pre><code>
   * try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
   *   SubscriptionName subscription = SubscriptionName.create("[PROJECT]", "[SUBSCRIPTION]");
   *   List&lt;String&gt; ackIds = new ArrayList&lt;&gt;();
   *   int ackDeadlineSeconds = 0;
   *   subscriptionAdminClient.modifyAckDeadline(subscription, ackIds, ackDeadlineSeconds);
   * }
   * </code></pre>
   *
   * @param subscription The name of the subscription. Format is
   *     `projects/{project}/subscriptions/{sub}`.
   * @param ackIds List of acknowledgment IDs.
   * @param ackDeadlineSeconds The new ack deadline with respect to the time this request was sent
   *     to the Pub/Sub system. For example, if the value is 10, the new ack deadline will expire 10
   *     seconds after the `ModifyAckDeadline` call was made. Specifying zero may immediately make
   *     the message available for another pull request. The minimum deadline you can specify is 0
   *     seconds. The maximum deadline you can specify is 600 seconds (10 minutes).
   * @throws com.google.api.gax.grpc.ApiException if the remote call fails
   */
/* package-private */
final void modifyAckDeadline(SubscriptionName subscription, List<String> ackIds, int ackDeadlineSeconds) {
    ModifyAckDeadlineRequest request = ModifyAckDeadlineRequest.newBuilder().setSubscriptionWithSubscriptionName(subscription).addAllAckIds(ackIds).setAckDeadlineSeconds(ackDeadlineSeconds).build();
    modifyAckDeadline(request);
}
Also used : ModifyAckDeadlineRequest(com.google.pubsub.v1.ModifyAckDeadlineRequest)

Example 5 with SubscriptionName

use of com.google.pubsub.v1.SubscriptionName in project google-cloud-java by GoogleCloudPlatform.

the class ITPubSubTest method testPublishSubscribe.

@Test
public void testPublishSubscribe() throws Exception {
    TopicName topicName = TopicName.create(projectId, formatForTest("testing-publish-subscribe-topic"));
    SubscriptionName subscriptionName = SubscriptionName.create(projectId, formatForTest("testing-publish-subscribe-subscription"));
    topicAdminClient.createTopic(topicName);
    subscriptionAdminClient.createSubscription(subscriptionName, topicName, PushConfig.newBuilder().build(), 10);
    PubsubMessage message = PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8("my message")).build();
    final SettableApiFuture<PubsubMessage> received = SettableApiFuture.create();
    Subscriber subscriber = Subscriber.defaultBuilder(subscriptionName, new MessageReceiver() {

        @Override
        public void receiveMessage(final PubsubMessage message, final AckReplyConsumer consumer) {
            if (received.set(message)) {
                consumer.ack();
            } else {
                consumer.nack();
            }
        }
    }).build();
    subscriber.addListener(new Subscriber.Listener() {

        public void failed(Subscriber.State from, Throwable failure) {
            received.setException(failure);
        }
    }, MoreExecutors.directExecutor());
    subscriber.startAsync();
    Publisher publisher = Publisher.defaultBuilder(topicName).build();
    publisher.publish(message).get();
    publisher.shutdown();
    assertEquals(received.get().getData(), message.getData());
    subscriber.stopAsync().awaitTerminated();
    subscriptionAdminClient.deleteSubscription(subscriptionName);
    topicAdminClient.deleteTopic(topicName);
}
Also used : Subscriber(com.google.cloud.pubsub.spi.v1.Subscriber) MessageReceiver(com.google.cloud.pubsub.spi.v1.MessageReceiver) SubscriptionName(com.google.pubsub.v1.SubscriptionName) Publisher(com.google.cloud.pubsub.spi.v1.Publisher) AckReplyConsumer(com.google.cloud.pubsub.spi.v1.AckReplyConsumer) PubsubMessage(com.google.pubsub.v1.PubsubMessage) TopicName(com.google.pubsub.v1.TopicName) Test(org.junit.Test)

Aggregations

SubscriptionName (com.google.pubsub.v1.SubscriptionName)31 Test (org.junit.Test)24 TopicName (com.google.pubsub.v1.TopicName)11 Subscription (com.google.pubsub.v1.Subscription)10 SubscriptionAdminClient (com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient)9 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)9 StatusRuntimeException (io.grpc.StatusRuntimeException)9 ApiException (com.google.api.gax.grpc.ApiException)8 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)8 PushConfig (com.google.pubsub.v1.PushConfig)8 ByteString (com.google.protobuf.ByteString)7 Subscriber (com.google.cloud.pubsub.v1.Subscriber)5 PubsubMessage (com.google.pubsub.v1.PubsubMessage)5 Empty (com.google.protobuf.Empty)4 ArrayList (java.util.ArrayList)4 AckReplyConsumer (com.google.cloud.pubsub.spi.v1.AckReplyConsumer)3 MessageReceiver (com.google.cloud.pubsub.spi.v1.MessageReceiver)3 Subscriber (com.google.cloud.pubsub.spi.v1.Subscriber)3 TopicAdminClient (com.google.cloud.pubsub.spi.v1.TopicAdminClient)3 SubscriptionAdminClient (com.google.cloud.pubsub.v1.SubscriptionAdminClient)3