Example #1
0
 public string CreateTopic([FromBody] CreatTopicModel attr)
 {
     try
     {
         PublisherServiceApiClient publisherService = PublisherServiceApiClient.Create();
         TopicName topicName = new TopicName(attr.projectId, attr.topicId);
         publisherService.CreateTopic(topicName);
         return(topicName.TopicId);
     }
     catch (Exception ex)
     {
         return(ex.Message);
     }
 }
Example #2
0
        public IEnumerable <string> ListTopics([FromBody] CreatTopicModel attr)
        {
            List <string> retorno = new List <string>();

            try
            {
                // List all topics for the project
                PublisherServiceApiClient publisher = PublisherServiceApiClient.Create();
                var topics = publisher.ListTopics(new ProjectName(attr.projectId));

                foreach (Topic topic1 in topics)
                {
                    retorno.Add(topic1.Name);
                }
            }
            catch (Exception ex)
            {
                retorno.Add(ex.Message + "-------" + ex.StackTrace);
            }
            return(retorno);
        }
Example #3
0
        public object CreateSubscription([FromBody] CreatTopicModel attr)
        {
            // [START pubsub_create_pull_subscription]
            SubscriberServiceApiClient subscriber = SubscriberServiceApiClient.Create();
            TopicName        topicName            = new TopicName(attr.projectId, attr.topicId);
            SubscriptionName subscriptionName     = new SubscriptionName(attr.projectId, attr.subscriptionID);

            try
            {
                Subscription subscription = subscriber.CreateSubscription(
                    subscriptionName, topicName, pushConfig: null,
                    ackDeadlineSeconds: 60);
            }
            catch (RpcException e)
                when(e.Status.StatusCode == Grpc.Core.StatusCode.AlreadyExists)
                {
                    // Already exists.  That's fine.
                }
            // [END pubsub_create_pull_subscription]
            return(0);
        }
Example #4
0
        public IEnumerable <ListaRetorno> ShowMessagesForSubscription([FromBody] CreatTopicModel attr)
        {
            List <ListaRetorno> retorno = new List <ListaRetorno>();

            var subscriptionName = new SubscriptionName(attr.projectId, attr.subscriptionID);

            var subscription = SubscriberServiceApiClient.Create();

            try
            {
                PullResponse response = subscription.Pull(subscriptionName, true, 10);

                var all = response.ReceivedMessages;

                foreach (ReceivedMessage message in all)
                {
                    retorno.Add(new ListaRetorno
                    {
                        id          = message.Message.MessageId,
                        publishDate = message.Message.PublishTime.ToDateTime().ToString("dd-M-yyyy HH:MM:ss"),
                        data        = Encoding.UTF8.GetString(message.Message.Data.ToArray(), 0, message.Message.Data.Length),
                    });

                    subscription.Acknowledge(subscriptionName, new string[] { message.AckId });
                }

                return(retorno);
            }
            catch (RpcException ex)
            {
                Console.WriteLine("Erro: {0}", ex.Message);
                retorno.Add(new ListaRetorno {
                    data = ex.Message
                });
                return(retorno);
            }
        }
Example #5
0
        public IEnumerable <string> PublishToTopic([FromBody] CreatTopicModel attr)
        {
            List <string> retorno = new List <string>();

            try
            {
                var topicName = new TopicName(attr.projectId, attr.topicId);

                PublisherServiceApiClient publisher = PublisherServiceApiClient.Create();

                // Create a message
                var message = new PubsubMessage()
                {
                    Data = ByteString.CopyFromUtf8(attr.message)
                };
                //message.Attributes.Add("myattrib", "its value");
                var messageList = new List <PubsubMessage>()
                {
                    message
                };

                var response = publisher.Publish(topicName, messageList);
                foreach (string messageId in response.MessageIds)
                {
                    retorno.Add(messageId);
                }
            }
            catch (Exception ex)
            {
                retorno.Add(ex.Message);
                return(retorno);
            }


            return(retorno);
        }
Example #6
0
        public async Task <ICollection <ListaRetorno> > ShowMessagesForSubscriptionWithListen([FromBody] CreatTopicModel attr)
        {
            List <ListaRetorno> retorno = new List <ListaRetorno>();

            try
            {
                SubscriptionName subscriptionName = new SubscriptionName(attr.projectId, attr.subscriptionID);
                SubscriberClient subscriber       = await SubscriberClient.CreateAsync(subscriptionName);

                // SubscriberClient runs your message handle function on multiple
                // threads to maximize throughput.
                await subscriber.StartAsync(async (PubsubMessage message, CancellationToken cancel) =>
                {
                    string text = Encoding.UTF8.GetString(message.Data.ToArray());
                    retorno.Add(new ListaRetorno
                    {
                        id          = message.MessageId,
                        publishDate = message.PublishTime.ToDateTime().ToString("dd-M-yyyy HH:MM:ss"),
                        data        = text,
                    });
                    return(attr.acknowledge ? SubscriberClient.Reply.Ack: SubscriberClient.Reply.Nack);
                });

                // Run for 3 seconds.
                await Task.Delay(3000);

                await subscriber.StopAsync(CancellationToken.None);

                return(retorno);
            }
            catch (RpcException ex)
            {
                Console.WriteLine("Erro: {0}", ex.Message);
                retorno.Add(new ListaRetorno {
                    data = ex.Message
                });
                return(retorno);
            }
        }