Skip to content

Commit

Permalink
fix #112 rabbitmq가 연결될때까지 연결 시도를 계속하도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
pspace-jwkwak committed Oct 7, 2022
1 parent 337f1d6 commit 2aed426
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
22 changes: 17 additions & 5 deletions portal/PortalSvr/RabbitMqReceivers/RabbitMqReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace PortalSvr.RabbitMQReceivers
public abstract class RabbitMQReceiver : BackgroundService
{
/// <summary>Rabbit MQ 연결 객체</summary>
private IConnection m_connection;
private IConnection m_connection = null;
/// <summary>Rabbit MQ 채널 객체</summary>
private IModel m_channel;
/// <summary>큐 이름</summary>
Expand Down Expand Up @@ -98,7 +98,18 @@ private void InitializeRabbitMQListener()
};

// 연결 객체 생성
m_connection = Factory.CreateConnection();
while (m_connection == null)
{
try
{
m_connection = Factory.CreateConnection();
}
catch (Exception e)
{
NNException.Log(e);
}
Thread.Sleep(1000);
}
// 연결 해제 이벤트 설정
m_connection.ConnectionShutdown += (_, _) =>
{
Expand All @@ -110,9 +121,10 @@ private void InitializeRabbitMQListener()
var arguments = new Dictionary<string, object>
{
{ "x-queue-type", "quorum" },
{"x-single-active-consumer", true}
};
m_channel.QueueDeclare(queue: m_queueName, durable: true, exclusive: false, autoDelete: false, arguments: arguments);
// Exchange 설절
// Exchange 설정
m_channel.ExchangeDeclare(exchange: m_config.ExchangeName, type: "topic");
// 모든 바인딩 키 처리
if (m_bindingKeys != null)
Expand Down Expand Up @@ -151,7 +163,7 @@ protected override Task ExecuteAsync(CancellationToken StoppingToken)
// 처리된 건인 경우
if (ResponseHandleMessage.IsProcessed)
{
m_logger.LogDebug($"[Process] MQ Message Received on [{{0}}] : {{1}}, Data = {{2}}", m_queueName, ea.RoutingKey, ea.Body.ToArray().GetString());
m_logger.LogDebug($"[Process] MQ Message Received on [{m_queueName}] : {ea.RoutingKey}, Data = {ea.Body.ToArray().GetString()}");

// 응답 연관 아이디가 존재하는 경우
var Properties = ea.BasicProperties;
Expand All @@ -177,7 +189,7 @@ protected override Task ExecuteAsync(CancellationToken StoppingToken)
// 처리되지 않은 건인 경우
else
{
m_logger.LogDebug($"[Reject] MQ Message Received on [{{0}}] : {{1}}, Data = {{2}}", m_queueName, ea.RoutingKey, ea.Body.ToArray().GetString());
m_logger.LogDebug($"[Reject] MQ Message Received on [{m_queueName}] : {ea.RoutingKey}, Data = {ea.Body.ToArray().GetString()}");

// 처리 거부
m_channel.BasicReject(ea.DeliveryTag, false);
Expand Down
6 changes: 3 additions & 3 deletions portal/PortalSvr/RabbitMqReceivers/RabbitMqServerReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ protected override async Task<ResponseMqData> HandleMessage(string RoutingKey, b
return new ResponseMqData(EnumResponseResult.Error, Resource.EC_COMMON__CANNOT_CREATE_INSTANCE, Resource.EM_COMMON__CANNOT_CREATE_INSTANCE);

// 내부 시스템 API 키 정보를 가져온다.
var ResponseApiKey = await ApiKeyProvider.GetMainApiKey();
var ApiKey = await ApiKeyProvider.GetMainApiKey();

// API 키를 가져오는데 실패한 경우
if (ResponseApiKey == null)
if (ApiKey == null)
return new ResponseMqData(EnumResponseResult.Error, Resource.EC_COMMON__NOT_FOUND, Resource.EM_COMMON__NOT_FOUND);

// 서버 상태 관련인 경우
Expand All @@ -103,7 +103,7 @@ protected override async Task<ResponseMqData> HandleMessage(string RoutingKey, b
var Request = JsonConvert.DeserializeObject<RequestServerState>(json);

// 서버 상태 수정
var Response = await DataProvider.UpdateState(Request, ResponseApiKey.UserId, ResponseApiKey.UserName);
var Response = await DataProvider.UpdateState(Request, ApiKey.UserId, ApiKey.UserName);

Result.CopyValueFrom(Response);
Result.IsProcessed = true;
Expand Down
8 changes: 4 additions & 4 deletions portal/PortalSvr/RabbitMqReceivers/RabbitMqServiceReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ protected override async Task<ResponseMqData> HandleMessage(string RoutingKey, b
return new ResponseMqData(EnumResponseResult.Error, Resource.EC_COMMON__CANNOT_CREATE_INSTANCE, Resource.EM_COMMON__CANNOT_CREATE_INSTANCE);

// 내부 시스템 API 키 정보를 가져온다.
var ResponseApiKey = await ApiKeyProvider.GetMainApiKey();
var ApiKey = await ApiKeyProvider.GetMainApiKey();

// API 키를 가져오는데 실패한 경우
if (ResponseApiKey == null)
if (ApiKey == null)
return new ResponseMqData(EnumResponseResult.Error, Resource.EC_COMMON__NOT_FOUND, Resource.EM_COMMON__NOT_FOUND);

// 서비스 상태 관련인 경우
Expand All @@ -104,7 +104,7 @@ protected override async Task<ResponseMqData> HandleMessage(string RoutingKey, b
var Request = JsonConvert.DeserializeObject<RequestServiceState>(json);

// 서버 상태 수정
var Response = await DataProvider.UpdateState(Request, ResponseApiKey.UserId, ResponseApiKey.UserName);
var Response = await DataProvider.UpdateState(Request, ApiKey.UserId, ApiKey.UserName);

Result.CopyValueFrom(Response);
Result.IsProcessed = true;
Expand All @@ -121,7 +121,7 @@ protected override async Task<ResponseMqData> HandleMessage(string RoutingKey, b
var Request = JsonConvert.DeserializeObject<RequestServiceHaAction>(json);

// 서비스 HA 상태 수정
var Response = await DataProvider.UpdateHaAction(Request, ResponseApiKey.UserId, ResponseApiKey.UserName);
var Response = await DataProvider.UpdateHaAction(Request, ApiKey.UserId, ApiKey.UserName);

Result.CopyValueFrom(Response);
Result.IsProcessed = true;
Expand Down

0 comments on commit 2aed426

Please sign in to comment.