/// <summary>Snippet for GetTopicAsync</summary> public async Task GetTopicAsync() { // Snippet: GetTopicAsync(TopicName,CallSettings) // Additional: GetTopicAsync(TopicName,CancellationToken) // Create client PublisherServiceApiClient publisherServiceApiClient = await PublisherServiceApiClient.CreateAsync(); // Initialize request argument(s) TopicName topic = new TopicName("[PROJECT]", "[TOPIC]"); // Make the request Topic response = await publisherServiceApiClient.GetTopicAsync(topic); // End snippet }
/// <summary>Snippet for GetTopicAsync</summary> public async Task GetTopicAsync() { // Snippet: GetTopicAsync(string, CallSettings) // Additional: GetTopicAsync(string, CancellationToken) // Create client PublisherServiceApiClient publisherServiceApiClient = await PublisherServiceApiClient.CreateAsync(); // Initialize request argument(s) string topic = "projects/[PROJECT]/topics/[TOPIC]"; // Make the request Topic response = await publisherServiceApiClient.GetTopicAsync(topic); // End snippet }
/// <summary>Snippet for GetTopicAsync</summary> public async Task GetTopicAsync_RequestObject() { // Snippet: GetTopicAsync(GetTopicRequest,CallSettings) // Create client PublisherServiceApiClient publisherServiceApiClient = await PublisherServiceApiClient.CreateAsync(); // Initialize request argument(s) GetTopicRequest request = new GetTopicRequest { TopicAsTopicName = new TopicName("[PROJECT]", "[TOPIC]"), }; // Make the request Topic response = await publisherServiceApiClient.GetTopicAsync(request); // End snippet }
private static async Task <Topic> EnsureTopicExists(string projectId, string topicId) { PublisherServiceApiClient publisher = await PublisherServiceApiClient.CreateAsync(); var topicName = TopicName.FromProjectTopic(projectId, topicId); Topic topic; try { topic = await publisher.CreateTopicAsync(topicName); } catch (RpcException e) when(e.Status.StatusCode == StatusCode.AlreadyExists) { topic = await publisher.GetTopicAsync(topicName); } return(topic); }
/// <summary>Check if the topic exists.</summary> /// <param name="topicName">Name of the topic.</param> /// <returns><c>true</c> if topic exists, <c>false</c> otherwise.</returns> public async Task <bool> TopicExists(string topicName) { try { await _publisherServiceApiClient.GetTopicAsync(new GetTopicRequest { TopicAsTopicName = new TopicName(_projectId, topicName) }); // Found subscription, return true. return(true); } catch (RpcException e) when(e.StatusCode == StatusCode.NotFound) { // Not found error, return false. return(false); } }
public async Task InitalizeAsync(CancellationToken cancellationToken) { PublisherServiceApiClient publisherService = await PublisherServiceApiClient.CreateAsync(cancellationToken); SubscriberServiceApiClient subscriberService = await SubscriberServiceApiClient.CreateAsync(cancellationToken); // ensure each topic exists foreach (string topicId in Topics.AllTopics) { if (cancellationToken.IsCancellationRequested) { return; } TopicName topicName = new TopicName(m_projectId, topicId); try { await publisherService.GetTopicAsync(topicName, cancellationToken); } catch (RpcException) { Topic topic = await publisherService.CreateTopicAsync(topicName, cancellationToken); m_logger.Info($"Created topic {topic.Name}"); } m_publisherClients.Add(topicName, await PublisherClient.CreateAsync(topicName)); } // ensure each subscription exists foreach (var(topicId, subscriptionId) in Subscriptions.AllSubscriptions) { if (cancellationToken.IsCancellationRequested) { return; } SubscriptionName subscriptionName = new SubscriptionName(m_projectId, subscriptionId); try { await subscriberService.GetSubscriptionAsync(subscriptionName, cancellationToken); } catch (RpcException) { Subscription subscription = await subscriberService.CreateSubscriptionAsync( new Subscription { TopicAsTopicName = new TopicName(m_projectId, topicId), SubscriptionName = subscriptionName, AckDeadlineSeconds = 30, ExpirationPolicy = new ExpirationPolicy { Ttl = Duration.FromTimeSpan(TimeSpan.FromDays(365)), }, }, cancellationToken); m_logger.Info($"Created subscription {subscription.Name}"); } m_subscriberClients.Add(subscriptionName, await SubscriberClient.CreateAsync(subscriptionName)); } }