public async Task PublishAsync() { string projectId = _fixture.ProjectId; string topicId = _fixture.CreateTopicId(); // Snippet: PublishAsync(*,*,CallSettings) // Additional: PublishAsync(*,*,CancellationToken) PublisherServiceApiClient client = PublisherServiceApiClient.Create(); // Make sure we have a topic to publish to TopicName topicName = new TopicName(projectId, topicId); await client.CreateTopicAsync(topicName); PubsubMessage message = new PubsubMessage { // The data is any arbitrary ByteString. Here, we're using text. Data = ByteString.CopyFromUtf8("Hello, Pubsub"), // The attributes provide metadata in a string-to-string dictionary. Attributes = { { "description", "Simple text message" } } }; await client.PublishAsync(topicName, new[] { message }); // End snippet }
/// <summary>Snippet for CreateTopicAsync</summary> public async Task CreateTopicAsync() { // Snippet: CreateTopicAsync(string, CallSettings) // Additional: CreateTopicAsync(string, CancellationToken) // Create client PublisherServiceApiClient publisherServiceApiClient = await PublisherServiceApiClient.CreateAsync(); // Initialize request argument(s) string name = "projects/[PROJECT]/topics/[TOPIC]"; // Make the request Topic response = await publisherServiceApiClient.CreateTopicAsync(name); // End snippet }
/// <summary>Snippet for CreateTopicAsync</summary> public async Task CreateTopicAsync() { // Snippet: CreateTopicAsync(TopicName,CallSettings) // Additional: CreateTopicAsync(TopicName,CancellationToken) // Create client PublisherServiceApiClient publisherServiceApiClient = await PublisherServiceApiClient.CreateAsync(); // Initialize request argument(s) TopicName name = new TopicName("[PROJECT]", "[TOPIC]"); // Make the request Topic response = await publisherServiceApiClient.CreateTopicAsync(name); // End snippet }
/// <summary> /// Creates the topic if it does not exist. /// </summary> /// <param name="topicName">Name of the topic.</param> /// <returns><c>true</c> if created, <c>false</c> if not (already exists).</returns> public async Task <bool> CreateTopicIfNotExists(string topicName) { try { await _publisherServiceApiClient.CreateTopicAsync(new TopicName(_projectId, topicName)); return(true); } catch (RpcException ex) when(ex.StatusCode == StatusCode.AlreadyExists) { // If it already exists do nothing as that was the desired outcome. return(false); } }
public async Task CreateTopicAsync() { string projectId = _fixture.ProjectId; string topicId = _fixture.CreateTopicId(); // Snippet: CreateTopicAsync(TopicName,CallSettings) // Additional: CreateTopicAsync(TopicName,CancellationToken) PublisherServiceApiClient client = PublisherServiceApiClient.Create(); TopicName topicName = new TopicName(projectId, topicId); Topic topic = await client.CreateTopicAsync(topicName); Console.WriteLine($"Created {topic.Name}"); // End snippet }
/// <summary> /// Asynchronously creates a Google PubSub topic if it does not exist /// </summary> /// <param name="publisher">publisher service</param> /// <param name="topicName">topic name</param> /// <returns>A Task containing the RPC response</returns> private async Task CreateTopicIfNotExistsAsync(PublisherServiceApiClient publisher, TopicName topicName) { try { await publisher.CreateTopicAsync(topicName); } catch (RpcException e) { // If we get any error other than already exists if (e.Status.StatusCode != StatusCode.AlreadyExists) { throw e; } } }
/// <summary>Snippet for CreateTopicAsync</summary> public async Task CreateTopicAsync_RequestObject() { // Snippet: CreateTopicAsync(Topic,CallSettings) // Create client PublisherServiceApiClient publisherServiceApiClient = await PublisherServiceApiClient.CreateAsync(); // Initialize request argument(s) Topic request = new Topic { TopicName = new TopicName("[PROJECT]", "[TOPIC]"), }; // Make the request Topic response = await publisherServiceApiClient.CreateTopicAsync(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>Snippet for CreateTopicAsync</summary> public async Task CreateTopicRequestObjectAsync() { // Snippet: CreateTopicAsync(Topic, CallSettings) // Additional: CreateTopicAsync(Topic, CancellationToken) // Create client PublisherServiceApiClient publisherServiceApiClient = await PublisherServiceApiClient.CreateAsync(); // Initialize request argument(s) Topic request = new Topic { TopicName = TopicName.FromProjectTopic("[PROJECT]", "[TOPIC]"), Labels = { { "", "" }, }, MessageStoragePolicy = new MessageStoragePolicy(), KmsKeyName = "", }; // Make the request Topic response = await publisherServiceApiClient.CreateTopicAsync(request); // End snippet }
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)); } }