2016-11-18 2 views
5

Windows ServerのMicrosoft Message Queueで新しく、EmployeeIDがNULLの場合はプッシュする必要があります。C#でMSMQを使用する検証例外キュー

従業員モデルクラスは、データがその検証例外キュー内に押し込まれると、それは別のプロセスによって処理されるべき

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 
    } 
} 

あります。 1時間ごとにプロセスを開始する必要があり、次のメソッドを呼び出す必要があります。

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

    // Log the Error 
} 

作成および処理方法を教えてください。

答えて

2

ファーストステップ:
- それは
存在しない場合は、キューを作成します - 非同期
便利なキューにメッセージをプッシュ: 窓はその後、サーバ/ PC上

を特色としてMSMQsをインストールします。 MSMQからメッセージをプッシュして取得するためのguide

コード例:

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"); 

     } 
    } 


これを行うために広く使用されている他の方法は、エンタープライズライブラリやNLogのようなこの機能を既に備えている簡単なインターフェイスを提供する、すぐに使用できるロガーを使用することです。

メッセージを取得するには、定期的にメッセージを読んで処理する別のWindowsサービスをお勧めします。それを行う方法の良い例がここに与えられます。Windows service with timer

更新: Windowsサービス例:

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(); 
    } 

} 

とMessageProcessor.csを

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

あなたはそれぞれのコードであなたの答えを詳しく教えてください。 –

+0

@ B.バラマニガンダンここにあなたは仲間です。あなたを助けるためのコードをいくつか追加しました。私は物事がより明確になることを願っています – Pan

+0

かなり良い...私はWindowsサービス経由で読書方法を呼び出す必要があります。このサービスは、読み取りメソッドを実行するための呼び出しを開始する必要があります。 - その部分であなたの考えを親切に分かち合う。 –

関連する問題