public async Task Execute(IJobExecutionContext context) { const string projectId = "sunkang-iot-monitor-service"; const string subscriptionId = "pull-test-message-in-order"; // using (var scope = _serviceProvider.CreateScope()) // {{ _logger.LogInformation($"{DateTime.Now.ToString("HH:mm:ss.ffff")}: {context.JobDetail.Key} job executing, triggered by {context.Trigger.Key}"); // Add PubSub Client integration - Pull messages in order _logger.LogInformation($"Start pulling messages from subscription"); var numberOfMessageProcessed = await _googleCloudPubSubClient.PullMessagesAsync(projectId, subscriptionId, (_, pubsubMessage) => { string decodedMessageText = Encoding.UTF8.GetString(pubsubMessage.Data.ToArray()); // retrieve the custom attributes from metadata if (pubsubMessage.Attributes.ContainsKey("type")) { RemoteTask remoteTask = JsonConvert.DeserializeObject <RemoteTask>(decodedMessageText); Console.WriteLine($"Pulled message {decodedMessageText} with messageId {pubsubMessage.MessageId}, OrderingKey: {pubsubMessage.OrderingKey}, Type: {pubsubMessage.Attributes["type"]}"); if (pubsubMessage.Attributes["type"] == "Response") { remoteTask.completed = true; _remoteTasksCache.AddOrUpdate(remoteTask); } // foreach (var attribute in pubsubMessage.Attributes) // { // Console.WriteLine($"{attribute.Key} = {attribute.Value}"); // } } }, 5000); _logger.LogInformation($"Total {numberOfMessageProcessed} messages processed"); }
public async Task RunTest2() { const string projectId = "sunkang-iot-monitor-service"; const string topicId = "test"; const string subscriptionId = "pull-test-message-in-order"; // add task to cache RemoteTask remoteTask1 = new RemoteTask() { id = Guid.NewGuid(), name = "task1", completed = false }; _remoteTasksCache.AddOrUpdate(remoteTask1); RemoteTask remoteTask2 = new RemoteTask() { id = Guid.NewGuid(), name = "task2", completed = false }; _remoteTasksCache.AddOrUpdate(remoteTask2); // Manual test on the same Topic // Add PubSub Client integration - Publish messages with ordering key var messagesWithOrderingKey = new List <(string, string, MapField <string, string>)>() { ("OrderingKey2", JsonConvert.SerializeObject(remoteTask2).ToString(), new MapField <string, string> { { "type", "Notify" } }), ("OrderingKey1", JsonConvert.SerializeObject(remoteTask1).ToString(), new MapField <string, string> { { "type", "Response" } }), ("OrderingKey2", JsonConvert.SerializeObject(remoteTask2).ToString(), new MapField <string, string> { { "type", "Notify" } }), ("OrderingKey1", JsonConvert.SerializeObject(remoteTask1).ToString(), new MapField <string, string> { { "type", "Notify" } }), ("OrderingKey2", JsonConvert.SerializeObject(remoteTask2).ToString(), new MapField <string, string> { { "type", "Response" } }), }; _remoteTasksCache.AddOrUpdate(remoteTask1); _remoteTasksCache.AddOrUpdate(remoteTask2); await _googleCloudPubSubClient.PublishOrderedMessagesAsync(projectId, topicId, messagesWithOrderingKey); // Add PubSub Client integration - Pull messages in order _logger.LogInformation($"Start pulling messages from subscription"); var numberOfMessageProcessed = await _googleCloudPubSubClient.PullMessagesAsync(projectId, subscriptionId, (_, pubsubMessage) => { string decodedMessageText = Encoding.UTF8.GetString(pubsubMessage.Data.ToArray()); // retrieve the custom attributes from metadata if (pubsubMessage.Attributes.ContainsKey("type")) { RemoteTask remoteTask = JsonConvert.DeserializeObject <RemoteTask>(decodedMessageText); Console.WriteLine($"Pulled message {decodedMessageText} with messageId {pubsubMessage.MessageId}, OrderingKey: {pubsubMessage.OrderingKey}, Type: {pubsubMessage.Attributes["type"]}"); if (pubsubMessage.Attributes["type"] == "Response") { remoteTask.completed = true; _remoteTasksCache.AddOrUpdate(remoteTask); } // foreach (var attribute in pubsubMessage.Attributes) // { // Console.WriteLine($"{attribute.Key} = {attribute.Value}"); // } } }, 10000); _logger.LogInformation($"Total {numberOfMessageProcessed} messages processed"); }