循環請求接口A獲得A的數據后線程池添加A入庫,同時線程池添加B和C,(將A作為條件提供給接口B和C發起請求),獲得返回信息入庫,
我用了,ManualResetEvent和AutoResetEvent,當A執行時候使B和C等待WaitOne(),B和C中都是循環請求的,B和C循環執行后設置信號,B設置?autoEvents[0].Set();C設置?autoEvents[1].Set();
問題是B完全可以執行完成然后執行了autoEvents[0].Set(),而C卻沒有全部執行完,有時候獲取幾十條就不動了, 有時候執行上千條也不動,但是根本沒執行到autoEvents[1].Set();
不報異常,什么錯誤都沒顯示,就是不動了。我看不到線程是否還在運行中(不知道怎么看)。只用VS2010看到調試狀態這些線程都是正常、
現在不知道怎么排查原因,為什么B可以正常執行,C卻不能、代碼有很多,做了精簡
View Code
1 partial class CtripHotelBaseInfoService : ServiceBase
2 {
3 private readonly Timer _timer;
4 //Log.Info("循環下載數據開始--");
5 private static readonly ILog Log = LogManager.GetLogger("CtripHotelBaseInfoLog");
6 public CtripHotelBaseInfoService()
7 {
8 InitializeComponent();
9 _timer = new Timer(Settings.Default.TimeSplit * 20 * 1000); //單位為分鐘
10 _timer.Enabled = true;
11 _timer.Elapsed += SubmitTimerElapsed;
12 }
13
14 protected override void OnStart(string[] args)
15 {
16 // TODO: 在此處添加代碼以啟動服務。
17 _timer.Start();
18 Log.Info("基本信息落地服務已啟動....");
19 }
20
21 protected override void OnStop()
22 {
23 // TODO: 在此處添加代碼以執行停止服務所需的關閉操作。
24 _timer.Stop();
25 Log.Info("基本信息落地服務已停止....");
26 }
27
28 internal void SubmitTimerElapsed(object sender, ElapsedEventArgs e)
29 {
30 _timer.Stop();
31 try
32 {
33 var serviceClient = new CtripHotelClient("CtripHotelService");
34 CtripHotelDataContext context = new CtripHotelDataContext(Settings.Default.CtripHotelConnectionString);
35 var cityList = from c in context.Ctrip_Base_City select c;
36 int count = cityList.Count();
37 Log.InfoFormat("共獲取城市信息成功共{0}個", count);
38 foreach (var city in cityList)
39 {
40 Log.InfoFormat("請求接口,城市ID:{0},城市名稱:{1} 的信息開始--", city.CityID, city.CityName);
41
42 string result = serviceClient.OTA_HotelSearchStr(city.CityID.ToString());
43
44 if (string.IsNullOrEmpty(result))
45 {
46 return;
47 }
48 Alpha alpha = new Alpha();
49 OTA_HotelSearchRSEntity entity = new OTA_HotelSearchRSEntity();
50 entity.head = new HeadEntity();
51 #region 解析XML
52
53 #endregion
54 if (entity != null && entity.head != null && entity.OTA_Hotel != null
55 && entity.head.ResultCode.Equals("Success") && entity.head.SubCode.Equals("Success")
56 && entity.OTA_Hotel.Count > 0)
57 {
58 //添加到線程池中區
59
60 ThreadPool.QueueUserWorkItem(new WaitCallback(alpha.GetGotelBase), entity);
61 ThreadPool.QueueUserWorkItem(new WaitCallback(alpha.OTA_HotelDescriptiveInfo), entity.OTA_Hotel);
62 ThreadPool.QueueUserWorkItem(new WaitCallback(alpha.OTA_HotelRatePlan), entity.OTA_Hotel);
63
64 WaitHandle.WaitAll(alpha.autoEvents);
65 //GetGotelBase(entity);
66 //OTA_HotelDescriptiveInfo(entity.OTA_Hotel);
67 }
68 else
69 {
70 // Log.Info(string.Format("請求結束,城市:{0}出錯了,時間:{1}", city.CityName, DateTime.Now));
71 Log.InfoFormat("基礎信息:ResultCode:{0},ResultMsg:{1},SubCode:{2},SubMsg:{3}", entity.head.ResultCode, entity.head.ResultMsg, entity.head.SubCode, entity.head.SubMsg);
72 Log.InfoFormat("城市ID:{0},城市名稱:{1} 的信息開始--", city.CityID, city.CityName);
73 Log.InfoFormat("返回信息:{0}", result);
74 }
75 Log.InfoFormat("請求接口,城市ID:{0},城市名稱:{1} 的信息結束--", city.CityID, city.CityName);
76
77 }
78 }
79 catch (Exception ex)
80 {
81 Log.InfoFormat("獲取城市信息異常:{0},堆棧:{1}", ex.Message, ex);
82 }
83 Log.Info("循環下載數據結束--");
84 _timer.Start();
85 }
86
87 }
88
89 public class Alpha
90 {
91 private static readonly ILog Log = LogManager.GetLogger("CtripHotelBaseInfoLog");
92 public ManualResetEvent eventX;
93 public AutoResetEvent[] autoEvents;
94 public Alpha()
95 {
96 autoEvents = new AutoResetEvent[]
97 {
98 new AutoResetEvent(false),
99 new AutoResetEvent(false)
100 };
101 eventX = new ManualResetEvent(false);
102 }
103
104 ///
105 /// 基本信息入庫
106 ///
107 ///
108 public void GetGotelBase(object entity)
109 {
110 CtripHotelDataContext context = new CtripHotelDataContext(Settings.Default.CtripHotelConnectionString);
111 Log.Info("存入數據庫開始--");
112 context.Connection.Open();
113 DbTransaction tran = context.Connection.BeginTransaction();
114 context.Transaction = tran;
115 try
116 {
117 OTA_HotelSearchRSEntity en = (OTA_HotelSearchRSEntity)entity;
118 Ctrip_HotelBase hoteBase = null;
119 foreach (OTA_HotelEntity hotelEntity in en.OTA_Hotel)
120 {
121
122 }
123 context.SubmitChanges();
124 tran.Commit();
125 Log.Info("存入數據庫結束--");
126 eventX.Set();
127 }
128 catch (Exception ex)
129 {
130 eventX.Set();
131 Log.InfoFormat("基礎信息入庫異常:{0}", ex);
132 tran.Rollback();
133 context.Connection.Close();
134 context.Connection.Dispose();
135 }
136 finally
137 {
138 context.Connection.Close();
139 context.Connection.Dispose();
140 }
141 }
142
143 ///
144 /// 請求詳細信息
145 ///
146 ///
147 public void OTA_HotelDescriptiveInfo(object objhotelList)
148 {
149 Log.Info("請求詳細信息開始--");
150 try
151 {
152 eventX.WaitOne();
153 string ss = string.Empty;
154 List hotelList = (List)objhotelList;
155 int pageIndex = 0;
156 int PageSize = 10;
157 int pageCount = (int)Math.Ceiling(hotelList.Count / (float)PageSize);
158 for (pageIndex = 0; pageIndex 0)
201 {
202 //ThreadPool.QueueUserWorkItem(new WaitCallback(ImportHotelDescriptiveInfoWaitCall), entity);
203 GetHotelDescriptiveInfo(entity);
204 }
205 else
206 {
207 //Log.InfoFormat("詳細信息出錯,ID:{0},Name:{1},詳細信息:ResultCode:{2},ResultMsg:{3},SubCode:{4},SubMsg:{5}", hotelEntity.HotelCode, hotelEntity.HotelName,
208 // entity.Head.ResultCode, entity.Head.ResultMsg, entity.Head.SubCode, entity.Head.SubMsg);
209 Log.InfoFormat("返回信息:{0}", result);
210 }
211 }
212 else
213 {
214 Log.InfoFormat("未請求到詳細信息ID:{0},Name:{1}詳細信息", hotelId, hotelName);
215 }
216 #endregion
217 }
218 Log.InfoFormat("計劃循環執行完成:" + ss);
219 autoEvents[0].Set();
220 autoEvents[0].Dispose();
221 }
222 catch (Exception ex)
223 {
224 Log.InfoFormat("詳細信息異常:{0}", ex);
225 }
226 Log.Info("請求詳細信息完成");
227 }
228
229 ///
230 /// 詳細信息入庫
231 ///
232 ///
233 private void GetHotelDescriptiveInfo(OTA_HotelDescriptiveInfoRSEntity entity)
234 {
235 #region
236 CtripHotelDataContext context = new CtripHotelDataContext(Settings.Default.CtripHotelConnectionString);
237 context.Connection.Open();
238 DbTransaction tran = context.Connection.BeginTransaction();
239 context.Transaction = tran;
240 try
241 {
242 Ctrip_HotelDescriptiveContent descriptiveResult = null;
243 //Log.InfoFormat("批量存入數據庫開始共:{0}條記錄--", entity.HotelDescriptiveContent.Count);
244 foreach (HotelDescriptiveContentEntity contentEntity in entity.HotelDescriptiveContent)
245 {
246
247 }
248 context.SubmitChanges();
249 tran.Commit();
250 //Log.Info("批量存入數據庫結束--");
251 }
252 catch (Exception ex)
253 {
254 Log.InfoFormat("詳細信息入庫異常:{0},堆棧:{1}", ex.Message, ex);
255 Log.InfoFormat("詳細信息ID:{0},名稱:{1}", entity.HotelDescriptiveContent[0].HotelCode, entity.HotelDescriptiveContent[0].HotelName);
256 tran.Rollback();
257 context.Connection.Close();
258 context.Connection.Dispose();
259 }
260 finally
261 {
262 context.Connection.Close();
263 context.Connection.Dispose();
264 }
265 #endregion
266 }
267
268 public void OTA_HotelRatePlan(object objhotelList)
269 {
270 Log.Info("請求價格計劃開始");
271 eventX.WaitOne();
272 string hotelCode = string.Empty;
273 string hotelName = string.Empty;
274 try
275 {
276 List hotelList = (List)objhotelList;
277 int pageIndex = 0;
278 int PageSize = 5;
279 int pageCount = (int)Math.Ceiling(hotelList.Count / (float)PageSize);
280 for (pageIndex = 0; pageIndex 0)
319 {
320 HotelRatePlanAdd(entity);
321 }
322 else
323 {
324 Log.InfoFormat("價格計劃出錯,ID:{0},Name:{1},詳細信息:ResultCode:{2},ResultMsg:{3},SubCode:{4},SubMsg:{5}", hotelCode, hotelName,
325 entity.Head.ResultCode, entity.Head.ResultMsg, entity.Head.SubCode, entity.Head.SubMsg);
326 }
327 }
328 else
329 {
330 Log.InfoFormat("未請求到價格計劃ID:{0},Name:{1}詳細信息", hotelCode, hotelName);
331 }
332 }
333 autoEvents[1].Set();
334 autoEvents[1].Dispose();
335 }
336 catch (Exception ex)
337 {
338 Log.InfoFormat("請求價格計劃異常:{0},堆棧:{1}", ex.Message, ex);
339 Log.InfoFormat("ID:{0},名稱:{1},價格信息信息", hotelCode, hotelName);
340 }
341 Log.Info("請求價格計劃完成");
342 }
343
344
345 ///
346 /// 初始化價格入庫
347 ///
348 ///
349 private void HotelRatePlanAdd(OTA_HotelRatePlanRSEntity entity)
350 {
351 CtripHotelDataContext context = new CtripHotelDataContext(Settings.Default.CtripHotelConnectionString);
352 context.Connection.Open();
353 DbTransaction tran = context.Connection.BeginTransaction();
354 context.Transaction = tran;
355 string hotelCode = string.Empty;
356 try
357 {
358 Ctrip_HotelRatePlan ratePlan = null;
359 foreach (RatePlanSRSEntity ratePlansEntity in entity.RatePlansList)
360 {
361
362 }
363 context.SubmitChanges();
364 tran.Commit();
365 }
366 catch (Exception ex)
367 {
368 Log.InfoFormat("名稱:{0}價格計劃入庫異常:{1};", hotelCode, ex);
369 tran.Rollback();
370 context.Connection.Close();
371 context.Connection.Dispose();
372 }
373 finally
374 {
375 context.Connection.Close();
376 context.Connection.Dispose();
377 }
378 }
379 }
線程池問題,跑數據跑了一些之后就不執行了、請大俠賜教
慕碼人8056858
2018-12-07 13:16:38