diff --git a/csharp/App/SinexcelCommunication/MiddlewareClasses/RabbitMQManager.cs b/csharp/App/SinexcelCommunication/MiddlewareClasses/RabbitMQManager.cs index 759e08477..253cdc0f6 100644 --- a/csharp/App/SinexcelCommunication/MiddlewareClasses/RabbitMQManager.cs +++ b/csharp/App/SinexcelCommunication/MiddlewareClasses/RabbitMQManager.cs @@ -7,12 +7,11 @@ using RabbitMQ.Client; namespace InnovEnergy.App.SinexcelCommunication.MiddlewareClasses; -[SuppressMessage("Trimming", "IL2026:Members annotated with \'RequiresUnreferencedCodeAttribute\' require dynamic access otherwise can break functionality when trimming application code")] public static class RabbitMqManager { - public static ConnectionFactory? Factory ; - public static IConnection ? Connection; - public static IModel? Channel; + private static ConnectionFactory? _factory ; + private static IConnection ? _connection; + private static IModel? _channel; public static Boolean SubscribeToQueue(StatusMessage currentSalimaxState, String? s3Bucket,String VpnServerIp) { @@ -20,19 +19,22 @@ public static class RabbitMqManager { //_factory = new ConnectionFactory { HostName = VpnServerIp }; - Factory = new ConnectionFactory + _factory = new ConnectionFactory { HostName = VpnServerIp, Port = 5672, VirtualHost = "/", UserName = "producer", Password = "b187ceaddb54d5485063ddc1d41af66f", - + AutomaticRecoveryEnabled = true, + NetworkRecoveryInterval = TimeSpan.FromSeconds(10), + RequestedHeartbeat = TimeSpan.FromSeconds(30) + }; - Connection = Factory.CreateConnection(); - Channel = Connection.CreateModel(); - Channel.QueueDeclare(queue: "statusQueue", durable: true, exclusive: false, autoDelete: false, arguments: null); + _connection = _factory.CreateConnection(); + _channel = _connection.CreateModel(); + _channel.QueueDeclare(queue: "statusQueue", durable: true, exclusive: false, autoDelete: false, arguments: null); Console.WriteLine("The controller sends its status to the middleware for the first time"); if (s3Bucket != null) InformMiddleware(currentSalimaxState); @@ -52,13 +54,36 @@ public static class RabbitMqManager var message = JsonSerializer.Serialize(status); var body = Encoding.UTF8.GetBytes(message); - Channel.BasicPublish(exchange: string.Empty, + _channel.BasicPublish(exchange: string.Empty, routingKey: "statusQueue", basicProperties: null, body: body); Console.WriteLine($"Producer sent message: {message}"); } - + public static Boolean EnsureConnected(StatusMessage currentSalimaxState, string? s3Bucket, string vpnServerIp) + { + try + { + if (_connection == null || !_connection.IsOpen || + _channel == null || _channel.IsClosed) + { + Console.WriteLine("⚠ RabbitMQ connection lost. Reconnecting..."); + + _connection?.Dispose(); + _channel?.Dispose(); + + return SubscribeToQueue(currentSalimaxState, s3Bucket, vpnServerIp); + } + + return true; + } + catch (Exception ex) + { + Console.WriteLine("❌ Error while ensuring RabbitMQ connection: " + ex); + return false; + } + } + } \ No newline at end of file