2016-11-18 20 views
5

Tôi mới trong Microsoft Message Queue trong Windows Server, tôi cần phải đẩy, nếu EmployeeID là NULL.Xác nhận hàng đợi ngoại lệ bằng MSMQ trong C#

Các nhân viên Mẫu Class là

public class Employee 
{ 
    public string EmployeeID { get; set; } 
    public string EmployeeName { get; set; } 
} 

public void ValidationProcess(Employee emp) 
{ 
    if((emp != null) || (emp.EmployeeID == null)) 
    { 
     // Push into Validation Exception Queue using MSMQ 
    } 
} 

Một khi dữ liệu bị đẩy vào mà Queue Validation ngoại lệ, nó phải được xử lý bằng quá trình riêng biệt. Mỗi 1 giờ quy trình cần bắt đầu và cần gọi phương thức sau

public void ValidationExceptionProcess(object obj) 
{ 
    // Some Inner Process 

    // Log the Error 
} 

Vui lòng hướng dẫn tôi cách tạo và xử lý.

Trả lời

2

Bước đầu tiên: Install MSMQs như một cửa sổ đặc trưng trên máy chủ/máy

Sau đó:
- Tạo hàng đợi nếu nó không tồn tại
- Đẩy thông điệp trong hàng đợi đồng bộ
hữu ích guide

dụ Mã để đẩy và lấy tin nhắn từ msmq:

public class ExceptionMSMQ 
    { 
     private static readonly string description = "Example description"; 
     private static readonly string path = @".\Private$\myqueue"; 
     private static MessageQueue exceptionQueue; 
     public static MessageQueue ExceptionQueue 
     { 
      get 
      { 
       if (exceptionQueue == null) 
       { 
        try 
        { 
         if (MessageQueue.Exists(path)) 
         { 
          exceptionQueue = new MessageQueue(path); 
          exceptionQueue.Label = description; 
         } 
         else 
         { 
          MessageQueue.Create(path); 
          exceptionQueue = new MessageQueue(path); 
          exceptionQueue.Label = description; 
         } 
        } 
        catch 
        { 
         throw; 
        } 
        finally 
        { 
         exceptionQueue.Dispose(); 
        } 
       } 
       return exceptionQueue; 
      } 
     } 

     public static void PushMessage(string message) 
     { 
      ExceptionQueue.Send(message); 
     } 

     private static List<string> RetrieveMessages() 
     { 
      List<string> messages = new List<string>(); 
      using (ExceptionQueue) 
      { 
       System.Messaging.Message[] queueMessages = ExceptionQueue.GetAllMessages(); 
       foreach (System.Messaging.Message message in queueMessages) 
       { 
        message.Formatter = new XmlMessageFormatter(
        new String[] { "System.String, mscorlib" }); 
        string msg = message.Body.ToString(); 
        messages.Add(msg); 
       } 
      } 
      return messages; 
     } 

     public static void Main(string[] args) 
     { 
      ExceptionMSMQ.PushMessage("my exception string"); 

     } 
    } 


Một cách khác được sử dụng rộng rãi để làm điều đó cũng sẽ là sử dụng ra khỏi hộp logger đã có chức năng này như Thư viện doanh nghiệp hoặc NLog cung cấp giao diện dễ dàng để làm điều đó.

Để truy xuất thư, tôi khuyên bạn nên sử dụng dịch vụ cửa sổ riêng để định kỳ đọc thư và xử lý chúng. Một ví dụ điển hình về cách để làm điều đó được đưa ra ở đây: Windows service with timer

Cập nhật: Windows Service Ví dụ:

MSMQConsumerService.cs

public partial class MSMQConsumerService : ServiceBase 
{ 
    private System.Timers.Timer timer; 

    public MSMQConsumerService() 
    { 
     InitializeComponent(); 
    } 

    protected override void OnStart(string[] args) 
    { 
     this.timer = new System.Timers.Timer(30000D); // 30000 milliseconds = 30 seconds 
     this.timer.AutoReset = true; 
     this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.ProcessQueueMessages); 
     this.timer.Start(); 
    } 

    protected override void OnStop() 
    { 
     this.timer.Stop(); 
     this.timer = null; 
    } 

    private void ProcessQueueMessages(object sender, System.Timers.ElapsedEventArgs e) 
    { 
     MessageProcessor.StartProcessing(); 
    } 

} 

và MessageProcessor.cs

public class MessageProcessor 
    { 
     public static void StartProcessing() 
     { 
      List<string> messages = ExceptionMSMQ.RetrieveMessages(); 
      foreach(string message in messages) 
      { 
       //write message in database 
      } 
     } 
    } 
+0

Bạn có thể vui lòng xây dựng câu trả lời của bạn với mã tương ứng. –

+0

@ B.Balamanigandan ở đây bạn là bạn đời. Đã thêm một số mã để hỗ trợ bạn. Tôi hy vọng mọi thứ rõ ràng hơn bây giờ – Pan

+0

Khá tốt ... Tôi cần gọi phương thức đọc qua dịch vụ windows. Dịch vụ này sẽ bắt đầu cuộc gọi để thực thi phương pháp đọc. - Vui lòng chia sẻ ý tưởng của bạn trong phần đó. –

Các vấn đề liên quan