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