2016-10-20 1 views
0

나는 RabbitMq with .NET을 배우고있다. tutorial에 따르면, 소비자의 간단한 구현은 다음과 같습니다C# RabbitMq : 메서드 리팩토링 중단 기능?

public class Receive 
{ 
    public static void Main() 
    { 
    var factory = new ConnectionFactory() { HostName = "localhost" }; 
    using(var connection = factory.CreateConnection()) 
    using(var channel = connection.CreateModel()) 
    { 
     channel.QueueDeclare(queue: "hello", 
          durable: false, 
          exclusive: false, 
          autoDelete: false, 
          arguments: null); 

     var consumer = new EventingBasicConsumer(channel); 
     consumer.Received += (model, ea) => 
     { 
      var body = ea.Body; 
      var message = Encoding.UTF8.GetString(body); 
      Console.WriteLine(" [x] Received {0}", message); 
     }; 
     channel.BasicConsume(queue: "hello", 
          noAck: true, 
          consumer: consumer); 

     Console.WriteLine(" Press [enter] to exit."); 
     Console.ReadLine(); 
    } 
    } 
} 

그리고 올바르게 작동합니다. 그러나 리팩터링하고 싶었습니다. 리시버의 기능을 별도의 메서드로 정의 해 봅시다.

public class Recieve 
{ 
    private ConnectionFactory factory; 
    public void ConsumeSimpleMessage(string queueName = "default") 
    { 
     using(var connection = factory.CreateConnection()) 
     { 
      using(var channel = connection.CreateModel()) 
      { 
       channel.QueueDeclare(queue: queueName, durable: false, 
        exclusive: false, autoDelete: false, arguments: null); 
       var consumer = new EventingBasicConsumer(channel); 
       consumer.Received += (model, ea) => 
       { 
        var body = ea.Body; 
        var message = Encoding.UTF8.GetString(body); 
        Console.WriteLine(message); 
       }; 

       channel.BasicConsume(queue: queueName, 
          noAck: true, 
          consumer: consumer); 
      } 
     } 


    } 
    public Recieve(string hostName = "localhost") 
    { 
     factory = new ConnectionFactory() { HostName = hostName }; 
    } 
} 

그리고 홈페이지 (에서이 방법을)를 호출 할 때 : 그것은처럼 보이는 여기

class Program 
{ 
    static void Main(string[] args) 
    { 
     Recieve reciever = new Recieve(); 

     reciever.ConsumeSimpleMessage(); 

     Console.ReadLine(); 
    } 
} 

작동하지 않습니다. 그것은 아무것도 보이지 않습니다. 그러나 메시지는 삭제되어 수신되었음을 나타냅니다. 왜 그렇게됩니까? 이벤트 전달에 대해 모르는 내용이 있습니까?

+0

데드 레터/오류 대기열을 확인하고 거기에 무엇이 있는지 확인할 수 있습니까? – MindingData

+0

대기열 이름이 안녕하세요? – Joe

답변

2

사용하지 않고 작동하는지 확인하거나 사용 설명서를 사용하려는 경우 사용 내역에 Console.Read()을 보관하고 작동하는지 확인하십시오. 연결 및 채널을 열린 상태로 유지하고 수동으로 닫을 필요가 없습니다.

정말로 디버그하고 싶다면 소비자에 중단 점을 넣을 수 있습니다.받은 메시지가 그대로 있는지 확인할 수 있습니다. 그렇게하면 메시지가 삭제 될 때를 알 수 있습니다.

또한 흔히 쉽게 추적 할 수 있도록 서버에 들어오는 모든 메시지를 기록하기 때문에 rabbitmq 용 추적 프로그램을 사용하는 것이 좋습니다.

+0

Console.Read()를 using 문 내에 배치 한 후 정확하게 작동합니다. 내가 왜 여기에서 작동하는지에 대한 간단한 설명을 찾았습니다. http://stackoverflow.com/questions/37192346/rabbitmq-basicconsume-and-event-driven-issues-relating-to-console-readline, 그러나 깊이있는 모든 설명이 있습니다. , 왜 Console.Read() 또는 Console.ReadLine()을 호출하여 작동해야합니까? –

+0

개체를 처리 할 때 명령문을 사용할 때 연결 및 채널을 만들기 위해 "using"문을 사용하지 않아도됩니다. 그게 도움이 될거야. 작은 응용 프로그램을 사용하는 경우 연결 및 채널을 열어두면 많은 트래픽이 발생하지 않습니다. –