/[projects]/dao/DaoMqPump2/MQFilter/FilterController.cs
ViewVC logotype

Contents of /dao/DaoMqPump2/MQFilter/FilterController.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2168 - (show annotations) (download)
Fri May 16 20:56:22 2014 UTC (10 years ago) by torben
File size: 8471 byte(s)
WIP
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 private Hashtable getConnectionProperties()
87 {
88 Hashtable connProperties = new Hashtable();
89 connProperties.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_CLIENT);
90 connProperties.Add(MQC.HOST_NAME_PROPERTY, mqHost);
91 connProperties.Add(MQC.CHANNEL_PROPERTY, mqChannel);
92 return connProperties;
93 }
94
95
96 public void transportAllMessages()
97 {
98 int messageCount = 0;
99
100 MQQueueManager mqMgr = null;
101 MQQueue queueIndbakke = null;
102 MQQueue queueMysql = null;
103 MQQueue queueDimaps = null;
104 MQQueue queueStore = null;
105 try
106 {
107 //MQ options
108 Hashtable connProps = getConnectionProperties();
109 int openInputOptions = MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING;
110 int openOutputOptions = MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING;
111
112
113 //MQ objects i v6 implementerer ikke IDisposable og kan derfor ikke bruges med "using" statement
114 mqMgr = new MQQueueManager(mqQueueManager, connProps);//stage 1 connect to mq
115
116 queueIndbakke = mqMgr.AccessQueue(queueNameIndbakke, openInputOptions);
117
118 queueMysql = mqMgr.AccessQueue(queueNameMysql, openOutputOptions);
119 queueDimaps = mqMgr.AccessQueue(queueNameDimaps, openOutputOptions);
120 queueStore = mqMgr.AccessQueue(queueNameStore, openOutputOptions);
121
122
123 bool isContinue = true;
124 while (isContinue)
125 {
126
127 MQMessage mqMsg = new MQMessage();
128 MQGetMessageOptions mqGetMsgOpts = new MQGetMessageOptions();
129
130
131 try
132 {
133 queueIndbakke.Get(mqMsg, mqGetMsgOpts);
134 if (mqMsg.Format.CompareTo(MQC.MQFMT_STRING) == 0)
135 {
136 string msgString = mqMsg.ReadString(mqMsg.MessageLength);
137 //System.Console.WriteLine(msgString);
138
139
140 // Hvis transaktionen starter med et ? er det ikke en gyldig transaktion
141 // validér ligeledes at headeren er gyldig
142 if (msgString.StartsWith("?") || DaoUtil.validateSalt2Header(msgString) == false)
143 {
144 string discarded_filename = Logfile.getLogFilename(LogfileType.LogTransactions, logDirectory, "filter");
145 using (StreamWriter discardedlog = new StreamWriter(discarded_filename, true))
146 {
147 discardedlog.WriteLine(Logfile.getNowString() + " " + msgString);
148 }
149 continue; //gå frem til at tage næste transaktion fra køen
150 }
151
152
153
154
155
156
157
158
159 messageCount++;// increment per run message counter
160 if (messageCount >= 10000) // if we have moved 10000 messages in this run - let it go
161 {
162 isContinue = false;
163 }
164
165
166
167
168 }
169 else
170 {
171 System.Console.WriteLine("Non-text message");
172 }
173 }
174 catch (MQException mqe)
175 {
176 isContinue = false;
177
178 // report reason, if any
179 if (mqe.Reason == MQC.MQRC_NO_MSG_AVAILABLE)
180 {
181 // special report for normal end
182 System.Console.WriteLine("no more messages");
183 }
184 else
185 {
186 // general report for other reasons
187 System.Console.WriteLine("MQQueue::Get ended with " + mqe.Message); ;
188
189 }
190
191 }
192
193 }
194
195
196
197 }
198 finally
199 {
200 closeQueue(queueIndbakke);
201 closeQueue(queueMysql);
202 closeQueue(queueDimaps);
203 closeQueue(queueStore);
204
205
206 if (mqMgr != null && mqMgr.IsOpen)
207 {
208 try
209 {
210 mqMgr.Close();
211 }
212 catch (Exception e)
213 {
214 Console.WriteLine("Error cleaning up qmgr " + e.Message);
215 }
216 }
217 }
218 }
219
220 private void closeQueue(MQQueue queue)
221 {
222 if (queue != null && queue.IsOpen)
223 {
224 try
225 {
226 queue.Close();
227 }
228 catch (Exception e)
229 {
230 Console.WriteLine("Error cleaning up queue " + e.Message);
231 }
232 }
233
234 }
235
236
237
238 /* Singleton */
239 private static FilterController instance = null;
240 public static FilterController getInstance()
241 {
242 if (instance == null)
243 instance = new FilterController();
244
245 return instance;
246 }
247
248 }
249 }

  ViewVC Help
Powered by ViewVC 1.1.20