Skip to content

Instantly share code, notes, and snippets.

@videlalvaro
Created October 23, 2023 09:43
Show Gist options
  • Save videlalvaro/fe00d36360b77fa109f337cee82a0a0e to your computer and use it in GitHub Desktop.
Save videlalvaro/fe00d36360b77fa109f337cee82a0a0e to your computer and use it in GitHub Desktop.
Sample telemetry producer for RabbitMQ
using System.Text;
using RabbitMQ.Client;
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "rmq-fabric",
durable: true,
exclusive: false,
autoDelete: false,
arguments: null);
int i = 0;
while (true)
{
var message = GenerateRandomMessage();
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "",
routingKey: "rmq-fabric",
basicProperties: null,
body: body);
if (i++ % 100 == 0)
{
Console.WriteLine(" [x] Sent {0}", message);
System.Threading.Thread.Sleep(100);
}
}
}
string GenerateRandomMessage()
{
var random = new Random();
var temperature = random.Next(0, 50);
var humidity = random.Next(0, 100);
var timestamp = DateTime.UtcNow.ToString("o");
return $"{{\"Timestamp\": \"{timestamp}\", \"Temperature\": {temperature}, \"Humidity\": {humidity}, \"Source\": \"rabbitmq\"}}";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment