annotate Messaging/Server/Listeners/MsmqListener.cs @ 29:9919ee227c93

msmq added
author adminsh@apollo
date Wed, 21 Mar 2012 22:09:18 +0000
parents
children 7d9de5746f18
rev   line source
29
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
1 using System;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
2 using System.Messaging;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
3 using Common.Messages;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
4 using GalaSoft.MvvmLight.Messaging;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
5 using Server.Interfaces;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
6
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
7 namespace Server.Listeners
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
8 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
9 public class MsmqListener : IListener
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
10 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
11 public bool IsListening { get; set; }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
12
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
13 private readonly MessageQueue _mq;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
14
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
15 public MsmqListener(string queueName)
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
16 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
17 _mq = MessageQueue.Exists(queueName)
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
18 ? new MessageQueue(queueName)
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
19 : MessageQueue.Create(queueName);
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
20
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
21 _mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
22 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
23
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
24 public void Start()
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
25 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
26 if (IsListening) return;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
27
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
28 Messenger.Default.Send(new MsmqLogMessage() { Body = "Opening listener..." });
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
29 IsListening = true;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
30 while (IsListening)
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
31 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
32 try
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
33 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
34 var message = _mq.Receive().Body;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
35 Messenger.Default.Send(new MsmqClientMessage());
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
36 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
37 catch (MessageQueueException mqe)
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
38 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
39 Messenger.Default.Send(new MsmqLogMessage() { Body = mqe.Message });
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
40 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
41 catch (Exception e)
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
42 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
43 Messenger.Default.Send(new MsmqLogMessage() { Body = e.Message });
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
44 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
45 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
46 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
47
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
48 public void Stop()
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
49 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
50 if (!IsListening) return;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
51 Messenger.Default.Send(new MsmqLogMessage() { Body = "Closing listener..." });
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
52 _mq.Close();
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
53 IsListening = false;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
54 Messenger.Default.Send(new MsmqLogMessage() { Body = "Closed." });
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
55 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
56 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
57 }