-
Notifications
You must be signed in to change notification settings - Fork 0
/
Service1.cs
170 lines (153 loc) · 5.28 KB
/
Service1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using Amazon;
using Amazon.SQS;
using Amazon.SQS.Model;
using NLog;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.ServiceProcess;
using System.Threading;
using System.Threading.Tasks;
using Message = Amazon.SQS.Model.Message;
namespace msmq_to_sqs
{
public partial class Service1 : ServiceBase
{
private static readonly Logger L = LogManager.GetCurrentClassLogger();
private static bool _isOn = true;
private AmazonSQSClient _sqsClient;
private const int MaxMessages = 10;
private const int WaitTime = 20;
private static readonly Dictionary<string, string> MsmqToSqs = new Dictionary<string, string>();
private static readonly Dictionary<string, string> SqsToMsmq = new Dictionary<string, string>();
private async Task SqsToMSMQ(string queueFrom, string queueTo)
{
do
{
try
{
L.Debug("waiting for sqs messages on {0}", queueFrom);
var msg = await GetMessage(_sqsClient, queueFrom, WaitTime);
foreach (Message m in msg.Messages)
{
ProcessMessage(m, queueTo);
await DeleteMessage(_sqsClient, m, queueFrom);
}
}
catch(Exception e)
{
L.Error(e, e.Message + " sleeping 5 seconds...");
Thread.Sleep(5000);
}
} while (_isOn);
L.Warn("sqs task finished");
}
private static async Task<ReceiveMessageResponse> GetMessage(
IAmazonSQS sqsClient, string qUrl, int waitTime = 0)
{
return await sqsClient.ReceiveMessageAsync(new ReceiveMessageRequest
{
QueueUrl = qUrl,
MaxNumberOfMessages = MaxMessages,
WaitTimeSeconds = waitTime
});
}
private static void ProcessMessage(Message message, string queue)
{
L.Debug($"{queue} MessageId: {message.MessageId}:");
L.Debug($"{message.Body}");
QueueHelper.Send(message.Body, queue);
}
private static async Task DeleteMessage(
IAmazonSQS sqsClient, Message message, string qUrl)
{
L.Debug($"Deleting message {message.MessageId} from queue...");
await sqsClient.DeleteMessageAsync(qUrl, message.ReceiptHandle);
}
private async Task MsmqToSQS(string msmq, string sqs)
{
do
{
try
{
L.Warn("waiting for msmq messages on {0}", msmq);
var s = QueueHelper.Receive(msmq).Body;
L.Debug($"{msmq} receiveid {s}");
await ProcessMessage((string)s, sqs);
}
catch (Exception ex)
{
L.Error(ex);
Thread.Sleep(1000);
}
}
while (_isOn);
L.Warn("msmq task finished");
}
private async Task ProcessMessage(string m, string sqs)
{
L.Debug("sending {0}", m);
await _sqsClient.SendMessageAsync(sqs, m.ToString());
}
public Service1()
{
InitializeComponent();
}
private void Start()
{
L.Warn("service start v5");
try
{
ReadSettings();
_sqsClient = new AmazonSQSClient(RegionEndpoint.SAEast1);
foreach (string msmq in MsmqToSqs.Keys)
{
Task.Run(() => MsmqToSQS(msmq, MsmqToSqs[msmq]));
}
foreach (string sqs in SqsToMsmq.Keys)
{
Task.Run(() => SqsToMSMQ(sqs, SqsToMsmq[sqs]));
}
}
catch (Exception e)
{
L.Error(e, e.Message);
}
}
private void ReadSettings()
{
var appSettings = ConfigurationManager.AppSettings;
if (appSettings.Count == 0)
{
L.Warn("AppSettings is empty.");
}
else
{
foreach (var key in appSettings.AllKeys)
{
if (key.StartsWith("msmqToSqs"))
{
MsmqToSqs.Add(key.Split('|')[1], appSettings[key]);
}
else if(key.StartsWith("sqsToMsmq"))
{
SqsToMsmq.Add(key.Split('|')[1], appSettings[key]);
}
else if(key == "awsAccessKeyId")
{
L.Warn($"creating sqsClient {appSettings[key]}");
}
}
}
}
protected override void OnStart(string[] args)
{
Start();
}
protected override void OnStop()
{
L.Warn("service stop");
_isOn = false;
}
}
}