public async Task PublishEvent(JObject outputEvent) { // Convert object to string outputEvent["id"] = Guid.NewGuid().ToString(); string jsonOutputEvent = JsonConvert.SerializeObject(outputEvent); // Publish a message to the topic. PubsubMessage message = new PubsubMessage { // The data is any arbitrary ByteString. Here, we're using text. Data = ByteString.CopyFromUtf8(jsonOutputEvent) }; await _publisher.PublishAsync(_eventTopicName, new[] { message }); _logger.LogInformation("Event sent to GCP PubSub. Topic: {topic}", _eventTopicName.TopicId); }
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 }
public async Task <string> EnviarMensagem(string mensagem) { var topicName = new TopicName("estudo-ci-cd", "tp-poc-ex"); string retornoIdMensagem = ""; PublisherServiceApiClient publisher = PublisherServiceApiClient.Create(); // Create a message var message = new PubsubMessage() { Data = ByteString.CopyFromUtf8(mensagem) }; //message.Attributes.Add("myattrib", "its value"); var messageList = new List <PubsubMessage>() { message }; var response = await publisher.PublishAsync(topicName, messageList); foreach (var item in response.MessageIds) { retornoIdMensagem = item; } return(retornoIdMensagem); }
/// <summary> /// Asynchronously adds a metadata payload to Google PubSub /// </summary> /// <param name="metadata">metadata to add to queue</param> /// <returns>success of operation</returns> public async Task <bool> Enqueue(QueueMetadata metadata) { try { PublisherServiceApiClient publisher = PublisherServiceApiClient.Create(); TopicName topicName = new TopicName(this.cloud.GoogleProjectID, this.cloud.QueueName); await this.CreateTopicIfNotExistsAsync(publisher, topicName); var message = new PubsubMessage() { Data = ByteString.CopyFromUtf8(JsonConvert.SerializeObject(metadata)), }; var messageList = new List <PubsubMessage>() { message }; await publisher.PublishAsync(topicName, messageList); } catch { return(false); } return(true); }
/// <summary>Snippet for PublishAsync</summary> public async Task PublishResourceNamesAsync() { // Snippet: PublishAsync(TopicName, IEnumerable<PubsubMessage>, CallSettings) // Additional: PublishAsync(TopicName, IEnumerable<PubsubMessage>, CancellationToken) // Create client PublisherServiceApiClient publisherServiceApiClient = await PublisherServiceApiClient.CreateAsync(); // Initialize request argument(s) TopicName topic = TopicName.FromProjectTopic("[PROJECT]", "[TOPIC]"); IEnumerable <PubsubMessage> messages = new PubsubMessage[] { new PubsubMessage(), }; // Make the request PublishResponse response = await publisherServiceApiClient.PublishAsync(topic, messages); // End snippet }
/// <summary>Snippet for PublishAsync</summary> public async Task PublishAsync() { // Snippet: PublishAsync(string, IEnumerable<PubsubMessage>, CallSettings) // Additional: PublishAsync(string, IEnumerable<PubsubMessage>, CancellationToken) // Create client PublisherServiceApiClient publisherServiceApiClient = await PublisherServiceApiClient.CreateAsync(); // Initialize request argument(s) string topic = "projects/[PROJECT]/topics/[TOPIC]"; IEnumerable <PubsubMessage> messages = new PubsubMessage[] { new PubsubMessage(), }; // Make the request PublishResponse response = await publisherServiceApiClient.PublishAsync(topic, messages); // End snippet }
public async Task <bool> Publish(string solveRequestId, IEnumerable <GameBoard> gameBoards, CancellationToken cancellationToken) { var messages = gameBoards.Select(board => new GameBoardMessage() { SolveRequestId = solveRequestId, Stack = new[] { new BoardAndWidth { Board = board, ParallelBranches = 1 } }, }); var pubsubMessages = messages.Select(message => new PubsubMessage() { Data = ByteString.CopyFromUtf8(JsonConvert.SerializeObject( message)) }); await _publisherApi.PublishAsync(MyTopic, pubsubMessages, CallSettings.FromCancellationToken(cancellationToken)); return(false); }
/// <summary>Snippet for PublishAsync</summary> public async Task PublishAsync() { // Snippet: PublishAsync(TopicName,IEnumerable<PubsubMessage>,CallSettings) // Additional: PublishAsync(TopicName,IEnumerable<PubsubMessage>,CancellationToken) // Create client PublisherServiceApiClient publisherServiceApiClient = await PublisherServiceApiClient.CreateAsync(); // Initialize request argument(s) TopicName topic = new TopicName("[PROJECT]", "[TOPIC]"); IEnumerable <PubsubMessage> messages = new[] { new PubsubMessage { Data = Google.Protobuf.ByteString.CopyFromUtf8(""), }, }; // Make the request PublishResponse response = await publisherServiceApiClient.PublishAsync(topic, messages); // End snippet }
/// <summary>Snippet for PublishAsync</summary> public async Task PublishRequestObjectAsync() { // Snippet: PublishAsync(PublishRequest, CallSettings) // Additional: PublishAsync(PublishRequest, CancellationToken) // Create client PublisherServiceApiClient publisherServiceApiClient = await PublisherServiceApiClient.CreateAsync(); // Initialize request argument(s) PublishRequest request = new PublishRequest { TopicAsTopicName = TopicName.FromProjectTopic("[PROJECT]", "[TOPIC]"), Messages = { new PubsubMessage(), }, }; // Make the request PublishResponse response = await publisherServiceApiClient.PublishAsync(request); // End snippet }
/// <summary>Snippet for PublishAsync</summary> public async Task PublishAsync_RequestObject() { // Snippet: PublishAsync(PublishRequest,CallSettings) // Create client PublisherServiceApiClient publisherServiceApiClient = await PublisherServiceApiClient.CreateAsync(); // Initialize request argument(s) PublishRequest request = new PublishRequest { TopicAsTopicName = new TopicName("[PROJECT]", "[TOPIC]"), Messages = { new PubsubMessage { Data = ByteString.CopyFromUtf8(""), }, }, }; // Make the request PublishResponse response = await publisherServiceApiClient.PublishAsync(request); // End snippet }
private async Task sendMessage(T messageObject) { InitializeQueue(); var queueName = _gcpSettings.PubSub.QueueMappings[messageObject.GetType().FullName]; var _topicName = new TopicName(_gcpSettings.PubSub.ProjectId, queueName); var json = JsonConvert.SerializeObject(messageObject); var attributes = new Dictionary <string, string> { { "Message.Type.FullName", messageObject.GetType().FullName } }; var message = new PubsubMessage() { Data = Google.Protobuf.ByteString.CopyFromUtf8(json) }; message.Attributes.Add(attributes); var messageResponse = await _pub.PublishAsync(_topicName, new[] { message }); _appLogger.LogMessage($"Message Type: {messageObject.GetType().FullName} queued with MessageId: {messageResponse.MessageIds[0]}"); }
private Task <PublishResponse> PublishAsync <T>(PublisherServiceApiClient publisher, TopicName topic, T obj) { var message = PubSubHelper.CreateMessage(obj); return(publisher.PublishAsync(topic, new[] { message })); }