use of com.google.pubsub.v1.TopicName in project google-cloud-java by GoogleCloudPlatform.
the class CreateTopicAndPublishMessages method publishMessages.
public static void publishMessages() throws Exception {
// [START pubsub_publish]
TopicName topicName = TopicName.create("my-project-id", "my-topic-id");
Publisher publisher = null;
List<ApiFuture<String>> messageIdFutures = new ArrayList<>();
try {
// Create a publisher instance with default settings bound to the topic
publisher = Publisher.defaultBuilder(topicName).build();
List<String> messages = Arrays.asList("first message", "second message");
// schedule publishing one message at a time : messages get automatically batched
for (String message : messages) {
ByteString data = ByteString.copyFromUtf8(message);
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
// Once published, returns a server-assigned message id (unique within the topic)
ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
messageIdFutures.add(messageIdFuture);
}
} finally {
// wait on any pending publish requests.
List<String> messageIds = ApiFutures.allAsList(messageIdFutures).get();
for (String messageId : messageIds) {
System.out.println("published with message ID: " + messageId);
}
if (publisher != null) {
// When finished with the publisher, shutdown to free up resources.
publisher.shutdown();
}
}
// [END pubsub_publish]
}
use of com.google.pubsub.v1.TopicName in project google-cloud-java by GoogleCloudPlatform.
the class SubscriptionAdminClientSnippets method createSubscriptionWithPushEndpoint.
/** Example of creating a subscription with a push endpoint. */
public Subscription createSubscriptionWithPushEndpoint(String topicId, String subscriptionId, String endpoint) throws Exception {
// [START pubsub_create_push_subscription]
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
TopicName topicName = TopicName.create(projectId, topicId);
SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
// eg. endpoint = "https://my-test-project.appspot.com/push"
PushConfig pushConfig = PushConfig.newBuilder().setPushEndpoint(endpoint).build();
// acknowledgement deadline in seconds for the message received over the push endpoint
int ackDeadlineInSeconds = 10;
Subscription subscription = subscriptionAdminClient.createSubscription(subscriptionName, topicName, pushConfig, ackDeadlineInSeconds);
return subscription;
}
// [END pubsub_create_push_subscription]
}
use of com.google.pubsub.v1.TopicName in project google-cloud-java by GoogleCloudPlatform.
the class TopicAdminClientSnippets method getTopic.
/** Example of getting a topic. */
public Topic getTopic(String topicId) throws Exception {
// [START pubsub_get_topic]
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
TopicName topicName = TopicName.create(projectId, topicId);
Topic topic = topicAdminClient.getTopic(topicName);
return topic;
}
// [END pubsub_get_topic]
}
use of com.google.pubsub.v1.TopicName 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.TopicName 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]
}
Aggregations