-
-
Save videlalvaro/fe00d36360b77fa109f337cee82a0a0e to your computer and use it in GitHub Desktop.
Sample telemetry producer for RabbitMQ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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