static void Main(string[] args) { var rabbitMQService = new RabbitMQService(); using (var connection = rabbitMQService.GetRabbitMqConnection()) { using (var channel = connection.CreateModel()) { var consumer = new EventingBasicConsumer(channel); consumer.Received += (sender, eventArgs) => { var body = eventArgs.Body; var message = Encoding.UTF8.GetString(body); //we publish message as string, so we can get it in this way. Console.WriteLine("Yayyyy!!!! I get the message! \nThe coming message is =" + message); }; channel.BasicConsume("Comon.SignalCollector", true, consumer); Console.ReadLine(); } } }
static void Main(string[] args) { RabbitMQService rabbitMQService = new RabbitMQService(); IConnection connection = rabbitMQService.GetRabbitMqConnection(); IModel channel = connection.CreateModel(); channel.QueueDeclare(rabbitMQService.queueName, false, false, false, null); channel.BasicQos(0, rabbitMQService.processCount, false); EventingBasicConsumer eventingTaskConsumer = new EventingBasicConsumer(channel); bool cancelAll = false; Dictionary <string, CancellationTokenSource> tokenSourcesList = new Dictionary <string, CancellationTokenSource>(); List <String> tasksToCancel = new List <String>(); eventingTaskConsumer.Received += (sender, basicDeliveryEventArgs) => { tokenSourcesList.Add(basicDeliveryEventArgs.BasicProperties.CorrelationId, new CancellationTokenSource()); Task.Run(async() => { bool cancelled = cancelAll || tasksToCancel.Contains(basicDeliveryEventArgs.BasicProperties.CorrelationId); string taskId = await DoWork(channel, basicDeliveryEventArgs, cancelled, tokenSourcesList[basicDeliveryEventArgs.BasicProperties.CorrelationId]); tokenSourcesList[taskId].Dispose(); tokenSourcesList.Remove(taskId); tasksToCancel.Remove(taskId); }, tokenSourcesList[basicDeliveryEventArgs.BasicProperties.CorrelationId].Token); }; channel.BasicConsume(rabbitMQService.queueName, false, eventingTaskConsumer); channel.QueueDeclare(rabbitMQService.stopQueueName, false, false, false, null); channel.BasicQos(0, 1, false); EventingBasicConsumer eventingStopConsumer = new EventingBasicConsumer(channel); eventingStopConsumer.Received += (sender, basicDeliveryEventArgs) => { string response = Encoding.UTF8.GetString(basicDeliveryEventArgs.Body); Console.WriteLine("[Received] " + response); if (response == "stop") { cancelAll = true; Dictionary <string, CancellationTokenSource> .KeyCollection keys = tokenSourcesList.Keys; foreach (string key in keys) { tokenSourcesList[key].Cancel(); } } else if (response.Contains("stop")) { string[] splitString = response.Split(' '); string taskToCancel; if (splitString.Length > 1) { taskToCancel = splitString[1]; if (tokenSourcesList.ContainsKey(taskToCancel)) { tokenSourcesList[taskToCancel].Cancel(); } else { tasksToCancel.Add(taskToCancel); } } } }; channel.BasicConsume(rabbitMQService.stopQueueName, true, eventingStopConsumer); }
static void Main(string[] args) { RabbitMQService rabbitMQService = new RabbitMQService(); IConnection connection = rabbitMQService.GetRabbitMqConnection(); IModel channel = connection.CreateModel(); channel.QueueDeclare(rabbitMQService.queueName, false, false, false, null); string responseQueue = channel.QueueDeclare().QueueName; EventingBasicConsumer basicResponseConsumer = new EventingBasicConsumer(channel); basicResponseConsumer.Received += (sender, basicDeliveryEventArgs) => { IBasicProperties props = basicDeliveryEventArgs.BasicProperties; PiTask response = new PiTask(); if (props != null) { response = JsonConvert.DeserializeObject <PiTask>(Encoding.UTF8.GetString(basicDeliveryEventArgs.Body)); } channel.BasicAck(basicDeliveryEventArgs.DeliveryTag, false); Console.WriteLine("[Received] " + "Task No. " + response.id + " Precision: " + response.precision + " Result: " + response.result); }; channel.BasicConsume(responseQueue, false, basicResponseConsumer); string[] tasks = new string[100]; Random rnd = new Random(); for (int i = 0; i < 100; i++) { PiTask message = new PiTask() { id = i, name = "Calculate Pi Precision Task No. " + i.ToString(), precision = rnd.Next(1, maxPrecision) }; string correlationId = Guid.NewGuid().ToString(); tasks[i] = correlationId; IBasicProperties basicProperties = channel.CreateBasicProperties(); basicProperties.ReplyTo = responseQueue; basicProperties.CorrelationId = correlationId; byte[] messageBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message)); channel.BasicPublish("", rabbitMQService.queueName, basicProperties, messageBytes); Console.WriteLine("[Sent] " + "Task No. " + message.id + " Precision: " + message.precision); } channel.QueueDeclare(rabbitMQService.stopQueueName, false, false, false, null); IBasicProperties basicPropertiess = channel.CreateBasicProperties(); bool running = true; Console.WriteLine("Write \"exit\" to stop program"); Console.WriteLine("Write \"stop\" to stop all tasks"); Console.WriteLine("Write \"stop\" [id] to stop task with directed ID"); string command; int result; bool parse; string[] splitString; while (running) { command = Console.ReadLine().ToLower(); if (command == "exit") { running = false; } else if (command == "stop") { channel.BasicPublish("", rabbitMQService.stopQueueName, basicPropertiess, Encoding.UTF8.GetBytes("stop")); } else { splitString = command.Split(' '); if (splitString.Length > 1) { parse = Int32.TryParse(splitString[1], out result); if (parse && result < 100) { channel.BasicPublish("", rabbitMQService.stopQueueName, basicPropertiess, Encoding.UTF8.GetBytes("stop " + tasks[result])); } } } } Console.ReadKey(); channel.Close(); connection.Close(); }