1 |
using System;
|
2 |
using System.Collections;
|
3 |
using System.Collections.Generic;
|
4 |
using System.IO;
|
5 |
using System.Text.RegularExpressions;
|
6 |
|
7 |
using IBM.WMQ;
|
8 |
|
9 |
using Microsoft.Win32;
|
10 |
|
11 |
using DaoCommon;
|
12 |
|
13 |
|
14 |
namespace MQFilter
|
15 |
{
|
16 |
class FilterController
|
17 |
{
|
18 |
|
19 |
public string mqHost { get; private set; }
|
20 |
public string mqChannel { get; private set; }
|
21 |
public string mqQueueManager { get; private set; }
|
22 |
|
23 |
public string logDirectory { get; private set; }
|
24 |
|
25 |
public string[] filterTranscations { get; private set; }
|
26 |
|
27 |
|
28 |
public static readonly string queueNameIndbakke = "DAO.INDBAKKE";
|
29 |
public static readonly string queueNameDimaps = "DAO.SAMLET";
|
30 |
public static readonly string queueNameMysql = "DAO.ALL";
|
31 |
public static readonly string queueNameStore = "DAO.STORE";
|
32 |
|
33 |
|
34 |
protected FilterController()
|
35 |
{
|
36 |
initialize();
|
37 |
}
|
38 |
|
39 |
private void initialize()
|
40 |
{
|
41 |
Console.WriteLine("FilterController: Loading config");
|
42 |
RegistryKey key = Registry.LocalMachine.CreateSubKey("Software\\DAO\\MQFilter");
|
43 |
|
44 |
|
45 |
//Læser globale MQ Parametre
|
46 |
mqHost = (string)key.GetValue("MQHost");
|
47 |
if (mqHost == null || mqHost.Length == 0)
|
48 |
throw new System.ArgumentException("MQHost cannot be null or empty");
|
49 |
|
50 |
mqChannel = (string)key.GetValue("MQChannel");
|
51 |
if (mqChannel == null || mqChannel.Length == 0)
|
52 |
throw new System.ArgumentException("MQChannel cannot be null or empty");
|
53 |
|
54 |
mqQueueManager = (string)key.GetValue("MQQueueManager");
|
55 |
if (mqQueueManager == null || mqQueueManager.Length == 0)
|
56 |
throw new System.ArgumentException("MQQueueManager cannot be null or empty");
|
57 |
|
58 |
////////////
|
59 |
|
60 |
logDirectory = (string)key.GetValue("LogDirectory");
|
61 |
if (logDirectory == null || logDirectory.Length == 0)
|
62 |
throw new System.ArgumentException("LogDirectory cannot be null or empty");
|
63 |
|
64 |
if (Directory.Exists(logDirectory) == false)
|
65 |
{
|
66 |
Directory.CreateDirectory(logDirectory);
|
67 |
}
|
68 |
|
69 |
////////////
|
70 |
|
71 |
String tmpFilterTransactions = (string)key.GetValue("FilterTransactions");
|
72 |
if (tmpFilterTransactions == null || tmpFilterTransactions.Length == 0)
|
73 |
throw new System.ArgumentException("FilterTransactions cannot be null or empty");
|
74 |
filterTranscations = Regex.Split(tmpFilterTransactions, ",");
|
75 |
|
76 |
for (int i = 0; i < filterTranscations.Length; i++)
|
77 |
{
|
78 |
filterTranscations[i] = filterTranscations[i].Trim();
|
79 |
}
|
80 |
|
81 |
////////////
|
82 |
|
83 |
}
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
public void transportAllMessages()
|
90 |
{
|
91 |
int messageCount = 0;
|
92 |
|
93 |
MQQueueManager mqMgr = null;
|
94 |
MQQueue queueIndbakke = null;
|
95 |
MQQueue queueMysql = null;
|
96 |
MQQueue queueDimaps = null;
|
97 |
MQQueue queueStore = null;
|
98 |
try
|
99 |
{
|
100 |
//MQ options
|
101 |
Hashtable connProps = MQHelper.getConnectionProperties(mqHost, mqChannel);
|
102 |
int openInputOptions = MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING;
|
103 |
int openOutputOptions = MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING;
|
104 |
|
105 |
|
106 |
//MQ objects i v6 implementerer ikke IDisposable og kan derfor ikke bruges med "using" statement
|
107 |
mqMgr = new MQQueueManager(mqQueueManager, connProps);//stage 1 connect to mq
|
108 |
|
109 |
queueIndbakke = mqMgr.AccessQueue(queueNameIndbakke, openInputOptions);
|
110 |
|
111 |
queueMysql = mqMgr.AccessQueue(queueNameMysql, openOutputOptions);
|
112 |
queueDimaps = mqMgr.AccessQueue(queueNameDimaps, openOutputOptions);
|
113 |
queueStore = mqMgr.AccessQueue(queueNameStore, openOutputOptions);
|
114 |
|
115 |
|
116 |
bool isContinue = true;
|
117 |
while (isContinue)
|
118 |
{
|
119 |
|
120 |
MQMessage mqMsg = new MQMessage();
|
121 |
MQGetMessageOptions mqGetMsgOpts = new MQGetMessageOptions();
|
122 |
|
123 |
|
124 |
try
|
125 |
{
|
126 |
queueIndbakke.Get(mqMsg, mqGetMsgOpts);
|
127 |
if (mqMsg.Format.CompareTo(MQC.MQFMT_STRING) == 0)
|
128 |
{
|
129 |
string salt2String = mqMsg.ReadString(mqMsg.MessageLength);
|
130 |
//System.Console.WriteLine(msgString);
|
131 |
|
132 |
|
133 |
// validér at headeren er gyldig
|
134 |
if ( Salt2Helper.validateSalt2Header(salt2String) == false)
|
135 |
{
|
136 |
string discarded_filename = Logfile.getLogFilename(LogfileType.LogTransactions, logDirectory, "filter");
|
137 |
using (StreamWriter discardedlog = new StreamWriter(discarded_filename, true))
|
138 |
{
|
139 |
discardedlog.WriteLine(Logfile.getNowString() + " " + salt2String);
|
140 |
}
|
141 |
continue; //gå frem til at tage næste transaktion fra køen
|
142 |
}
|
143 |
|
144 |
MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the defaults,
|
145 |
// same as MQPMO_DEFAULT
|
146 |
|
147 |
MQMessage msg = new MQMessage();
|
148 |
msg.Format = MQC.MQFMT_STRING;
|
149 |
msg.CharacterSet = 1252;
|
150 |
msg.WriteString(salt2String);
|
151 |
|
152 |
Salt2Header header = Salt2Helper.parseHeader(salt2String);
|
153 |
queueMysql.Put(msg, pmo);
|
154 |
if (saveForLater(header))
|
155 |
{
|
156 |
queueStore.Put(msg, pmo);
|
157 |
}
|
158 |
else
|
159 |
{
|
160 |
queueDimaps.Put(msg, pmo);
|
161 |
}
|
162 |
|
163 |
|
164 |
|
165 |
messageCount++;// increment per run message counter
|
166 |
if (messageCount >= 10000) // if we have moved 10000 messages in this run - let it go
|
167 |
{
|
168 |
isContinue = false;
|
169 |
}
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
}
|
175 |
else
|
176 |
{
|
177 |
System.Console.WriteLine("Non-text message");
|
178 |
}
|
179 |
}
|
180 |
catch (MQException mqe)
|
181 |
{
|
182 |
isContinue = false;
|
183 |
|
184 |
// report reason, if any
|
185 |
if (mqe.Reason == MQC.MQRC_NO_MSG_AVAILABLE)
|
186 |
{
|
187 |
// special report for normal end
|
188 |
System.Console.WriteLine("no more messages");
|
189 |
}
|
190 |
else
|
191 |
{
|
192 |
// general report for other reasons
|
193 |
System.Console.WriteLine("MQQueue::Get ended with " + mqe.Message); ;
|
194 |
|
195 |
}
|
196 |
|
197 |
}
|
198 |
|
199 |
}
|
200 |
|
201 |
|
202 |
|
203 |
}
|
204 |
finally
|
205 |
{
|
206 |
MQHelper.closeQueueSafe(queueIndbakke);
|
207 |
MQHelper.closeQueueSafe(queueMysql);
|
208 |
MQHelper.closeQueueSafe(queueDimaps);
|
209 |
MQHelper.closeQueueSafe(queueStore);
|
210 |
|
211 |
MQHelper.closeQueueManagerSafe(mqMgr);
|
212 |
}
|
213 |
}
|
214 |
|
215 |
private Boolean saveForLater(Salt2Header header)
|
216 |
{
|
217 |
DateTime now = DateTime.Now;
|
218 |
int hour = now.Hour;
|
219 |
if (hour >= 14 && hour < 18)
|
220 |
{
|
221 |
|
222 |
if (contains(header.transaktionForkortelse, this.filterTranscations) ) //Så længe vi skal være .net3.0 kompatible er LINQ problematisk (LINQ kræver 3.5)
|
223 |
{
|
224 |
return true;
|
225 |
}
|
226 |
else
|
227 |
{
|
228 |
return false;
|
229 |
}
|
230 |
}
|
231 |
else //normal operation - send straight trough
|
232 |
{
|
233 |
return false;
|
234 |
}
|
235 |
|
236 |
}
|
237 |
|
238 |
private bool contains(string needle, string[] haystack) // s
|
239 |
{
|
240 |
foreach(string hay in haystack)
|
241 |
{
|
242 |
if (needle.Equals(hay))
|
243 |
{
|
244 |
return true;
|
245 |
}
|
246 |
}
|
247 |
|
248 |
return false;
|
249 |
}
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
/* Singleton */
|
255 |
private static FilterController instance = null;
|
256 |
public static FilterController getInstance()
|
257 |
{
|
258 |
if (instance == null)
|
259 |
instance = new FilterController();
|
260 |
|
261 |
return instance;
|
262 |
}
|
263 |
|
264 |
}
|
265 |
}
|