/// <summary> /// 生成“数据源分组计时器”集合 /// 根据“数据源”UDI分组 /// </summary> /// <param name="dictSrcUrl"></param> /// <returns></returns> public static Dictionary <string, SrcUrlGroupTimer> CreateList(Dictionary <string, ISrcUrl> dictSrcUrl) { List <SrcUrlGroupTimer> sugTimerList = new List <SrcUrlGroupTimer>(); foreach (ISrcUrl isu in dictSrcUrl.Values) { SrcUrlGroupTimer sugTimer = sugTimerList.Find( delegate(SrcUrlGroupTimer sugt) { return(sugt.IntervalSeconds == isu.UDI); } ); if (sugTimer != null) { sugTimer.listISU.Add(isu.Tag, isu); } else { string name = Language.GetSomeTime(isu.UDI); if (isu.UDI <= 0) { sugTimer = new SrcUrlGroupTimer(name); } else { sugTimer = new SrcUrlGroupTimer(name, isu.UDI); } sugTimer.listISU.Add(isu.Tag, isu); sugTimerList.Add(sugTimer); } } return(sugTimerList.ToDictionary(sugt => sugt.name, sugt => sugt)); }
/// <summary> /// 合并ISU /// Main控制线程调用 /// </summary> /// <param name="timer"></param> public bool Union(SrcUrlGroupTimer timer) { if (timer.thisLock.TryEnterWriteLock(1000)) { try { if (this.thisLock.TryEnterWriteLock(1000)) { this.listISU = this.listISU.Concat <KeyValuePair <string, ISrcUrl> >(timer.listISU).ToDictionary <KeyValuePair <string, ISrcUrl>, string, ISrcUrl>(x => x.Key, x => x.Value); timer.listISU.Clear(); return(true); } } finally { this.thisLock.ExitWriteLock(); timer.thisLock.ExitWriteLock(); } } return(false); }
public static Dictionary <string, SrcUrlGroupTimer> CreateList(List <ISrcUrl> listSrcUrl, Action <object, System.Timers.ElapsedEventArgs> firstMethod) { List <SrcUrlGroupTimer> source = new List <SrcUrlGroupTimer>(); using (List <ISrcUrl> .Enumerator enumerator = listSrcUrl.GetEnumerator()) { while (enumerator.MoveNext()) { Predicate <SrcUrlGroupTimer> match = null; ISrcUrl isu = enumerator.Current; if (match == null) { match = sugt => sugt.IntervalSeconds == isu.UDI; } SrcUrlGroupTimer item = source.Find(match); if (item != null) { item.listISU.Add(isu.Tag, isu); } else { string someTime = Language.GetSomeTime(isu.UDI); if (isu.UDI <= 0.0) { item = new SrcUrlGroupTimer(someTime, firstMethod); } else { item = new SrcUrlGroupTimer(someTime, isu.UDI, firstMethod); } item.listISU.Add(isu.Tag, isu); source.Add(item); } } } return(source.ToDictionary <SrcUrlGroupTimer, string, SrcUrlGroupTimer>(sugt => sugt.name, sugt => sugt)); }
/// <summary> /// 合并ISU /// Main控制线程调用 /// </summary> /// <param name="timer"></param> public bool Union(SrcUrlGroupTimer timer) { if (timer.thisLock.TryEnterWriteLock(1000)) { try { if (this.thisLock.TryEnterWriteLock(1000)) { this.listISU = this.listISU.Concat<KeyValuePair<string, ISrcUrl>>(timer.listISU).ToDictionary<KeyValuePair<string, ISrcUrl>, string, ISrcUrl>(x => x.Key, x => x.Value); timer.listISU.Clear(); return true; } } finally { this.thisLock.ExitWriteLock(); timer.thisLock.ExitWriteLock(); } } return false; }
public static Dictionary<string, SrcUrlGroupTimer> CreateList(List<ISrcUrl> listSrcUrl, Action<object, System.Timers.ElapsedEventArgs> firstMethod) { List<SrcUrlGroupTimer> source = new List<SrcUrlGroupTimer>(); using (List<ISrcUrl>.Enumerator enumerator = listSrcUrl.GetEnumerator()) { while (enumerator.MoveNext()) { Predicate<SrcUrlGroupTimer> match = null; ISrcUrl isu = enumerator.Current; if (match == null) { match = sugt => sugt.IntervalSeconds == isu.UDI; } SrcUrlGroupTimer item = source.Find(match); if (item != null) { item.listISU.Add(isu.Tag, isu); } else { string someTime = Language.GetSomeTime(isu.UDI); if (isu.UDI <= 0.0) { item = new SrcUrlGroupTimer(someTime, firstMethod); } else { item = new SrcUrlGroupTimer(someTime, isu.UDI, firstMethod); } item.listISU.Add(isu.Tag, isu); source.Add(item); } } } return source.ToDictionary<SrcUrlGroupTimer, string, SrcUrlGroupTimer>(sugt => sugt.name, sugt => sugt); }
/// <summary> /// 生成“数据源分组计时器”集合 /// 根据“数据源”UDI分组 /// </summary> /// <param name="dictSrcUrl"></param> /// <returns></returns> public static Dictionary<string, SrcUrlGroupTimer> CreateList(Dictionary<string, ISrcUrl> dictSrcUrl) { List<SrcUrlGroupTimer> sugTimerList = new List<SrcUrlGroupTimer>(); foreach (ISrcUrl isu in dictSrcUrl.Values) { SrcUrlGroupTimer sugTimer = sugTimerList.Find( delegate(SrcUrlGroupTimer sugt) { return sugt.IntervalSeconds == isu.UDI; } ); if (sugTimer != null) { sugTimer.listISU.Add(isu.Tag, isu); } else { string name = Language.GetSomeTime(isu.UDI); if (isu.UDI <= 0) { sugTimer = new SrcUrlGroupTimer(name); } else { sugTimer = new SrcUrlGroupTimer(name, isu.UDI); } sugTimer.listISU.Add(isu.Tag, isu); sugTimerList.Add(sugTimer); } } return sugTimerList.ToDictionary(sugt => sugt.name, sugt => sugt); }
/// <summary> /// 添加timer /// </summary> /// <param name="timer"></param> public void Add(SrcUrlGroupTimer timer) { if (sugtlist.ContainsKey(timer.Name)) { sugtlist[timer.Name].Union(timer); } else { sugtlist.Add(timer.Name, timer); timer.StartFirst(timerRun); } }
/// <summary> /// 路由处理 /// </summary> /// <param name="isu">数据接口</param> /// <param name="sug">定时器,用于堵塞</param> public void routeProcess(ISrcUrl isu, SrcUrlGroupTimer sug) { if (isu.USEPARAM) { IMakeParam ip = isu as IMakeParam; List<AqiParam> paramList = ip.enumParams(); while (paramList == null || paramList.Count == 0) { bool bEvent = ThrowWaitEvent(isu.NAME + ":缺少参数,请输入以下参数", isu); if (bEvent) { //若接受事件 //由用户生成参数 //TEST等待用户输入参数 sug.Wait(); } else { //不接受事件则无法输入参数,忽略 ThrowEvent(RunMessage.RunType.TIP, isu.NAME + ":缺少参数,而且无法获取忽略此数据接口"); return; } } foreach (AqiParam ap in paramList) { getProcess(isu, ap); } } else { getProcess(isu, null); } }
/* * Process处理函数应该保持线程安全 * Runner下的多个Timer会调用此Runner的处理函数 * * */ /// <summary> /// 路由处理 /// 应该由Timer线程调用,可能会嵌套 /// 再此方法中调用的方法应保证线程安全 /// </summary> /// <param name="isu">数据接口</param> /// <param name="sugt">定时器,用于堵塞</param> public void RouteProcess(ISrcUrl isu, SrcUrlGroupTimer sugt) { //加载配置 if(isu.IAW is ICacheConfig) { ICacheConfig icc = isu.IAW as ICacheConfig; if (!icc.IsSrcUrlEnabled(isu.Tag)) { return; } } //双向确认是否使用参数 if (isu.UseParam && isu is ISrcUrlParam) { this.GetProcess(isu as ISrcUrlParam, sugt); } else { this.GetProcess(isu, sugt); } }
//TODO 应该合并GetProcess /// <summary> /// /// 获取处理 /// </summary> /// <param name="isup"></param> /// <param name="sugt"></param> public void GetProcess(ISrcUrlParam isup, SrcUrlGroupTimer sugt) { //获取参数 List<AqiParam> list = null; ISrcUrl isu = isup as ISrcUrl; try { if (isup is ICacheParam) { ICacheParam icp = isup as ICacheParam; if (icp.IsParamsExpired()) { icp.LoadParams(); } list = icp.FilterParams(); } else { list = isup.EnumParams(); } } catch (Exception ex) { AqiManage.Remind.Log_Error("获取参数错误,错误严重无法继续", ex, this.name, sugt.Name, isu.Name); return; } //TODO //应该在检验参数 while ((list == null) || (list.Count == 0)) { if (isup.ParamIgnoreEmpty) { AqiManage.Remind.Log_Error("缺少参数,此数据接口在无参数是将被忽略", new string[] { this.name, sugt.Name, isu.Name }); return; } if (this.ThrowWaitEvent(isu.Name + ":缺少参数,请输入以下参数", isu)) { AqiManage.Remind.Log_Debug("缺少参数,进入等待", new string[] { this.name, sugt.Name, isu.Name }); sugt.Wait(); } else { AqiManage.Remind.Log_Error("缺少参数,而且无法获取忽略此数据接口", new string[] { this.name, sugt.Name, isu.Name }); return; } } foreach (AqiParam ap in list) { if (sugt.IsCancellationRequested) { break; } byte[] data = null; try { data = isup.GetData(ap); } catch (Exception ex) { AqiManage.Remind.Log_Error("数据获取失败,进入重试队列", ex, new string[] { this.name, sugt.Name, isu.Name }); this.ar.PutNew(this.name, isu, ap, ex); } this.SaveProcess(data, isu, ap); } }
/// <summary> /// 获取处理 /// </summary> /// <param name="isu">数据接口</param> /// <param name="sugt">定时器</param> public void GetProcess(ISrcUrl isu, SrcUrlGroupTimer sugt) { byte[] data = null; try { data = isu.GetData(); } catch (Exception exception) { AqiManage.Remind.Log_Error("数据获取失败,进入重试队列", new string[] { this.name, sugt.Name, isu.Name }); this.ar.PutNew(this.name, isu, null, exception); return; } this.SaveProcess(data, isu, null); }
/// <summary> /// 添加timer /// </summary> /// <param name="timer"></param> public bool Add(SrcUrlGroupTimer timer) { try { this.thisLock.EnterUpgradeableReadLock(); if (this.dictTimer.ContainsKey(timer.Name)) { //合并 return this.dictTimer[timer.Name].Union(timer); } else { this.thisLock.EnterWriteLock(); this.dictTimer.Add(timer.Name, timer); timer.Elapsed += new ElapsedEventHandler(timer_RunEvent); timer.Start(); this.thisLock.ExitWriteLock(); } } finally { this.thisLock.ExitUpgradeableReadLock(); } return true; }