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]
}
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);
}
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);
}
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<String> ackIds = new ArrayList<>();
* 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);
}
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);
}
Aggregations