Exemplo n.º 1
0
        public static void DeleteTopic(string projectId, string topicId)
        {
            PublisherServiceApiClient publisher = PublisherServiceApiClient.Create();
            TopicName topicName = TopicName.FromProjectTopic(projectId, topicId);

            publisher.DeleteTopic(topicName);
        }
Exemplo n.º 2
0
        public void Overview()
        {
            string projectId      = _fixture.ProjectId;
            string topicId        = _fixture.CreateTopicId();
            string subscriptionId = _fixture.CreateSubscriptionId();

            // Sample: Overview
            // First create a topic.
            PublisherServiceApiClient publisher = PublisherServiceApiClient.Create();
            TopicName topicName = new TopicName(projectId, topicId);

            publisher.CreateTopic(topicName);

            // Subscribe to the topic.
            SubscriberServiceApiClient subscriber       = SubscriberServiceApiClient.Create();
            SubscriptionName           subscriptionName = new SubscriptionName(projectId, subscriptionId);

            subscriber.CreateSubscription(subscriptionName, topicName, pushConfig: null, ackDeadlineSeconds: 60);

            // Publish a message to the topic.
            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" }
                }
            };

            publisher.Publish(topicName, new[] { message });

            // Pull messages from the subscription. We're returning immediately, whether or not there
            // are messages; in other cases you'll want to allow the call to wait until a message arrives.
            PullResponse response = subscriber.Pull(subscriptionName, returnImmediately: true, maxMessages: 10);

            foreach (ReceivedMessage received in response.ReceivedMessages)
            {
                PubsubMessage msg = received.Message;
                Console.WriteLine($"Received message {msg.MessageId} published at {msg.PublishTime.ToDateTime()}");
                Console.WriteLine($"Text: '{msg.Data.ToStringUtf8()}'");
            }

            // Acknowledge that we've received the messages. If we don't do this within 60 seconds (as specified
            // when we created the subscription) we'll receive the messages again when we next pull.
            subscriber.Acknowledge(subscriptionName, response.ReceivedMessages.Select(m => m.AckId));

            // Tidy up by deleting the subscription and the topic.
            subscriber.DeleteSubscription(subscriptionName);
            publisher.DeleteTopic(topicName);
            // End sample

            Assert.Equal(1, response.ReceivedMessages.Count);
            Assert.Equal("Hello, Pubsub", response.ReceivedMessages[0].Message.Data.ToStringUtf8());
            Assert.Equal("Simple text message", response.ReceivedMessages[0].Message.Attributes["description"]);
        }
Exemplo n.º 3
0
        public async Task SimpleOverview()
        {
            string projectId      = _fixture.ProjectId;
            string topicId        = _fixture.CreateTopicId();
            string subscriptionId = _fixture.CreateSubscriptionId();

            // Sample: SimpleOverview
            // First create a topic.
            PublisherServiceApiClient publisherService = await PublisherServiceApiClient.CreateAsync();

            TopicName topicName = new TopicName(projectId, topicId);

            publisherService.CreateTopic(topicName);

            // Subscribe to the topic.
            SubscriberServiceApiClient subscriberService = await SubscriberServiceApiClient.CreateAsync();

            SubscriptionName subscriptionName = new SubscriptionName(projectId, subscriptionId);

            subscriberService.CreateSubscription(subscriptionName, topicName, pushConfig: null, ackDeadlineSeconds: 60);

            // Publish a message to the topic using PublisherClient.
            PublisherClient publisher = await PublisherClient.CreateAsync(topicName);

            // PublishAsync() has various overloads. Here we're using the string overload.
            string messageId = await publisher.PublishAsync("Hello, Pubsub");

            // PublisherClient instance should be shutdown after use.
            // The TimeSpan specifies for how long to attempt to publish locally queued messages.
            await publisher.ShutdownAsync(TimeSpan.FromSeconds(15));

            // Pull messages from the subscription using SimpleSubscriber.
            SubscriberClient subscriber = await SubscriberClient.CreateAsync(subscriptionName);

            List <PubsubMessage> receivedMessages = new List <PubsubMessage>();
            // Start the subscriber listening for messages.
            await subscriber.StartAsync((msg, cancellationToken) =>
            {
                receivedMessages.Add(msg);
                Console.WriteLine($"Received message {msg.MessageId} published at {msg.PublishTime.ToDateTime()}");
                Console.WriteLine($"Text: '{msg.Data.ToStringUtf8()}'");
                // Stop this subscriber after one message is received.
                // This is non-blocking, and the returned Task may be awaited.
                subscriber.StopAsync(TimeSpan.FromSeconds(15));
                // Return Reply.Ack to indicate this message has been handled.
                return(Task.FromResult(SubscriberClient.Reply.Ack));
            });

            // Tidy up by deleting the subscription and the topic.
            subscriberService.DeleteSubscription(subscriptionName);
            publisherService.DeleteTopic(topicName);
            // End sample

            Assert.Equal(1, receivedMessages.Count);
            Assert.Equal("Hello, Pubsub", receivedMessages[0].Data.ToStringUtf8());
        }
Exemplo n.º 4
0
        public static object DeleteTopic(string projectId, string topicId)
        {
            // [START pubsub_delete_topic]
            PublisherServiceApiClient publisher = PublisherServiceApiClient.Create();
            TopicName topicName = new TopicName(projectId, topicId);

            publisher.DeleteTopic(topicName);
            Console.WriteLine("Topic deleted.");
            // [END pubsub_delete_topic]
            return(0);
        }
        /// <summary>Snippet for DeleteTopic</summary>
        public void DeleteTopic()
        {
            // Snippet: DeleteTopic(TopicName,CallSettings)
            // Create client
            PublisherServiceApiClient publisherServiceApiClient = PublisherServiceApiClient.Create();
            // Initialize request argument(s)
            TopicName topic = new TopicName("[PROJECT]", "[TOPIC]");

            // Make the request
            publisherServiceApiClient.DeleteTopic(topic);
            // End snippet
        }
Exemplo n.º 6
0
        /// <summary>Snippet for DeleteTopic</summary>
        public void DeleteTopic()
        {
            // Snippet: DeleteTopic(string, CallSettings)
            // Create client
            PublisherServiceApiClient publisherServiceApiClient = PublisherServiceApiClient.Create();
            // Initialize request argument(s)
            string topic = "projects/[PROJECT]/topics/[TOPIC]";

            // Make the request
            publisherServiceApiClient.DeleteTopic(topic);
            // End snippet
        }
        /// <summary>Snippet for DeleteTopic</summary>
        public void DeleteTopic_RequestObject()
        {
            // Snippet: DeleteTopic(DeleteTopicRequest,CallSettings)
            // Create client
            PublisherServiceApiClient publisherServiceApiClient = PublisherServiceApiClient.Create();
            // Initialize request argument(s)
            DeleteTopicRequest request = new DeleteTopicRequest
            {
                TopicAsTopicName = new TopicName("[PROJECT]", "[TOPIC]"),
            };

            // Make the request
            publisherServiceApiClient.DeleteTopic(request);
            // End snippet
        }
        public void DeleteTopic()
        {
            string projectId = _fixture.ProjectId;
            string topicId   = _fixture.CreateTopicId();

            PublisherServiceApiClient.Create().CreateTopic(new TopicName(projectId, topicId));

            // Snippet: DeleteTopic(TopicName,*)
            PublisherServiceApiClient client = PublisherServiceApiClient.Create();

            TopicName topicName = new TopicName(projectId, topicId);

            client.DeleteTopic(topicName);
            Console.WriteLine($"Deleted {topicName}");
            // End snippet
        }
Exemplo n.º 9
0
        public void DeletePubSubTopic(TopicName topicName)
        {
            PublisherServiceApiClient publisher = PublisherServiceApiClient.Create();

            publisher.DeleteTopic(topicName);
        }
Exemplo n.º 10
0
        private static void Start()
        {
            SubscriberClient _subscriber;
            PublisherClient  _publisher;
            // Instantiates a client
            PublisherServiceApiClient publisherApi = PublisherServiceApiClient.Create();
            // Subscribe to the topic.
            TopicName                  pubsubTopicName  = new TopicName(projectId, topicName);
            SubscriptionName           subscriptionName = new SubscriptionName(projectId, subscriptionId);
            SubscriberServiceApiClient subscriberApi    = SubscriberServiceApiClient.Create();

            // Creates the new topic
            try
            {
                Topic topic = publisherApi.CreateTopic(pubsubTopicName);
                Console.WriteLine($"Topic {topic.Name} created.");
            }
            catch (Grpc.Core.RpcException e)
                when(e.Status.StatusCode == Grpc.Core.StatusCode.AlreadyExists)
                {
                    Console.WriteLine($"Topic {topicName} already exists.");
                }
            // Create the new subscription
            try
            {
                subscriberApi.CreateSubscription(subscriptionName, pubsubTopicName, null, 120);
                Console.WriteLine($"Subscription {subscriptionName.Kind} created.");
            }
            catch (Grpc.Core.RpcException e) when(e.Status.StatusCode == Grpc.Core.StatusCode.AlreadyExists)
            {
                // OK
                Console.WriteLine($"Subscription {subscriptionName.Kind} already exists");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            _subscriber = SubscriberClient.Create(subscriptionName, new[] { subscriberApi });

            _publisher = PublisherClient.Create(pubsubTopicName, new[] { publisherApi });

            _publisher.PublishAsync("Bla-Bla-Bla-Message.");

            _subscriber.StartAsync((message, token) =>
            {
                string data = message.Data.ToStringUtf8();
                try
                {
                    Console.WriteLine($"Pubsub message id={message.MessageId}, " +
                                      $"created at {message.PublishTime}, data{message.Data.ToStringUtf8()}");

                    // TODO: Replace with ACK
                    return(System.Threading.Tasks.Task.FromResult(SubscriberClient.Reply.Nack));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return(System.Threading.Tasks.Task.FromResult(SubscriberClient.Reply.Nack));
                }
            });

            // VARIAN II :

            // Pull messages from the subscription.We're returning immediately, whether or not there
            // are messages; in other cases you'll want to allow the call to wait until a message arrives.
            PullResponse response = subscriberApi.Pull(subscriptionName, returnImmediately: true, maxMessages: 10);

            foreach (ReceivedMessage received in response.ReceivedMessages)
            {
                PubsubMessage msg = received.Message;
                Console.WriteLine($"Received message {msg.MessageId} published at {msg.PublishTime.ToDateTime()}");
                Console.WriteLine($"Text: '{msg.Data.ToStringUtf8()}'");
            }
            // Acknowledge that we've received the messages. If we don't do this within 60 seconds (as specified
            // when we created the subscription) we'll receive the messages again when we next pull.
            subscriberApi.Acknowledge(subscriptionName, response.ReceivedMessages.Select(m => m.AckId));

            // Tidy up by deleting the subscription and the topic.
            subscriberApi.DeleteSubscription(subscriptionName);
            publisherApi.DeleteTopic(pubsubTopicName);
        }
Exemplo n.º 11
0
 public void Dispose()
 {
     _logger.Debug("[GCPFanout.Dispose()] Delete topic: {_topicID}.", _topicID);
     _publisher.ShutdownAsync(TimeSpan.FromSeconds(15));
     _publisherService.DeleteTopic(_topicName);
 }