This code sets up a consumer to receive messages from a RabbitMQ queue and save the messages as JSON files. The consumer uses a ConnectionFactory to create a connection to the RabbitMQ server, and then creates a EventingBasicConsumer to receive messages from the queue. When a message is received, the consumer converts the message from a System.ReadOnlyMemory<byte> value to a byte[] value, and then uses the System.Text.Encoding.UTF8.GetString method to convert the byte[] value to a string.
The code then gets the current date and time in the UTC +0 time zone and converts it to a DateTime object. The DateTime object is then converted to a string in the desired format and used to generate the file path for the JSON file. Finally, the File.WriteAllText method is used to save the JSON string to the file.
The CreateConnectionFactory method is a helper method that creates a ConnectionFactory instance with the specified connection parameters, such as the host name, port, user name, password, and virtual host. The ConnectionFactory is then used to create the connection to the RabbitMQ server.
usingSystem.Text;usingRabbitMQ.Client;usingRabbitMQ.Client.Events;usingNewtonsoft.Json;usingSystem;usingSystem.Collections.Generic;namespaceRmqConsumer{classProgram {staticvoidMain(string[] args) { // Set up RabbitMQ connectionvar factory =CreateConnectionFactory("HostName",5672,"email","password","VHost");using (var connection =factory.CreateConnection())using (var channel =connection.CreateModel()) { // Set up consumer to receive messages from the queuevar consumer =newEventingBasicConsumer(channel);consumer.Received+= (model, ea) => { // Convert the System.ReadOnlyMemory<byte> value to a byte[] valuevar body =ea.Body.ToArray(); // Parse the JSON string into an objectvar updateObject =JsonConvert.DeserializeObject(Encoding.UTF8.GetString(body)); // Serialize the object back to a string, with indentation and line breaksvar beautifiedUpdate =JsonConvert.SerializeObject(updateObject,Formatting.Indented); // Get the current date and time in the UTC +0 time zoneDateTime utcTime =DateTime.UtcNow; // Convert the tick value to a DateTime objectDateTime dateTime =DateTime.FromBinary(utcTime.Ticks); // Convert the DateTime object to a string in the desired formatstring dateTimeString =dateTime.ToString("yyyy-MM-dd'T'HH-mm-ss.fff'Z'"); // Generate the file path using the formatted date and time stringstring filePath =Path.Combine("<FilesPath>", dateTimeString +".json"); // Save the beautified JSON string to a fileFile.WriteAllText(filePath, beautifiedUpdate); };channel.BasicConsume(queue:"_QUEUE_", autoAck:true, consumer: consumer); // Keep the program runningConsole.ReadLine(); } } private static ConnectionFactory CreateConnectionFactory(string hostName, int port, string userName, string password, string virtualHost)
{ return newConnectionFactory() { HostName = hostName, Port = port, UserName = userName, Password = password, AutomaticRecoveryEnabled =true, VirtualHost = virtualHost, RequestedHeartbeat =TimeSpan.FromSeconds(580), NetworkRecoveryInterval =TimeSpan.FromSeconds(1) }; } }}