use of com.google.pubsub.v1.Topic in project google-cloud-java by GoogleCloudPlatform.
the class TopicAdminClientSnippets method createTopic.
/** Example of creating a topic. */
public Topic createTopic(String topicId) throws Exception {
// [START pubsub_create_topic]
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
// projectId <= unique project identifier, eg. "my-project-id"
// topicId <= "my-topic-id"
TopicName topicName = TopicName.create(projectId, topicId);
Topic topic = topicAdminClient.createTopic(topicName);
return topic;
}
// [END pubsub_create_topic]
}
use of com.google.pubsub.v1.Topic in project google-cloud-java by GoogleCloudPlatform.
the class SubscriptionAdminClientSnippets method createSubscription.
/** Example of creating a pull subscription for a topic. */
public Subscription createSubscription(String topicId, String subscriptionId) throws Exception {
// [START pubsub_create_pull_subscription]
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
// eg. projectId = "my-test-project", topicId = "my-test-topic"
TopicName topicName = TopicName.create(projectId, topicId);
// eg. subscriptionId = "my-test-subscription"
SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
// create a pull subscription with default acknowledgement deadline
Subscription subscription = subscriptionAdminClient.createSubscription(subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
return subscription;
}
// [END pubsub_create_pull_subscription]
}
use of com.google.pubsub.v1.Topic in project google-cloud-java by GoogleCloudPlatform.
the class PublisherSnippets method newBuilder.
/** Example of creating a {@code Publisher}. */
// [TARGET newBuilder(TopicName)]
// [VARIABLE "my_project"]
// [VARIABLE "my_topic"]
public static void newBuilder(String projectId, String topicId) throws Exception {
TopicName topic = TopicName.create(projectId, topicId);
Publisher publisher = Publisher.defaultBuilder(topic).build();
try {
// ...
} finally {
// When finished with the publisher, make sure to shutdown to free up resources.
publisher.shutdown();
}
}
use of com.google.pubsub.v1.Topic in project google-cloud-java by GoogleCloudPlatform.
the class ITTopicAdminClientSnippets method listTopicsRetreivesAddedTopics.
@Test
public void listTopicsRetreivesAddedTopics() throws Exception {
List<Topic> addedTopics = new ArrayList<>();
String topicName1 = topics[0];
addedTopics.add(topicAdminClientSnippets.createTopic(topicName1));
String topicName2 = topics[1];
addedTopics.add(topicAdminClientSnippets.createTopic(topicName2));
boolean[] topicFound = { false, false };
ListTopicsPagedResponse response = topicAdminClientSnippets.listTopics();
assertNotNull(response);
Iterable<Topic> topics = response.iterateAll();
for (int i = 0; i < 2; i++) {
if (!topicFound[i]) {
topicFound[i] = Iterables.contains(topics, addedTopics.get(i));
}
}
assertTrue(topicFound[0] && topicFound[1]);
}
use of com.google.pubsub.v1.Topic in project google-cloud-java by GoogleCloudPlatform.
the class ITPubSubSnippets method testPublisherSubscriber.
@Test
public void testPublisherSubscriber() throws Exception {
TopicName topicName = TopicName.create(ServiceOptions.getDefaultProjectId(), formatForTest("test-topic"));
SubscriptionName subscriptionName = SubscriptionName.create(ServiceOptions.getDefaultProjectId(), formatForTest("test-subscription"));
try (TopicAdminClient publisherClient = TopicAdminClient.create();
SubscriptionAdminClient subscriberClient = SubscriptionAdminClient.create()) {
publisherClient.createTopic(topicName);
subscriberClient.createSubscription(subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
testPublisherSubscriberHelper(topicName, subscriptionName);
subscriberClient.deleteSubscription(subscriptionName);
publisherClient.deleteTopic(topicName);
}
}
Aggregations