/// <summary> /// 同步模式:处理 /// </summary> /// <param name="msg"></param> /// <returns></returns> public override object DoExecuteMessageRequest(string serviceName, WQMessage msg, bool async) { // Log.Write("ProxyHandler.DoExecuteMessageRequest:" + services[serviceName].GroupName); Log.Write(LogAction.Info, className, "DoExecuteMessageRequest", serviceName, -1, string.Format(startFormat, serviceName, msg.TransactionID)); long t = DateTime.Now.Ticks; string data = (string)SerializeHelper.Serialize(msg.Format, msg.Body); DovaHttpMessage hmsg=msg as DovaHttpMessage; string contentType = hmsg.ContentType; string groupName = services[serviceName].GroupName; using (IDisposable service = ServiceFactory<HttpProxy>.GetServer(groupName).Instance() as IDisposable) { try { Log.Write(LogAction.Info, className, "DoExecuteMessageRequest", serviceName, DateTime.Now.Ticks - t, "获取目标服务:" + groupName + ",msg:" + msg.TransactionID); t = DateTime.Now.Ticks; string res = (service as HttpProxy).Request(data, hmsg.Method, hmsg.ContentType, hmsg.Encoding); Log.Write(LogAction.Info, className, "DoExecuteMessageRequest", serviceName, DateTime.Now.Ticks - t, "同步代理转发:" + groupName + ":" + serviceName + ",msg:" + msg.ToKeyString()); return SerializeHelper.DeSerialize<DovaResponse>(msg.Format, res); } catch (Exception e) { Log.Write(LogAction.Error, className, "DoExecuteMessageRequest", serviceName, DateTime.Now.Ticks - t, string.Format(errorFormat, serviceName, msg.ToKeyString(), e.ToString())); throw new Exception("消息处理失败:DoExecuteMessageRequest.serviceName:" + serviceName + ",TransactionID:" + msg.TransactionID + ",错误信息:" + e.Message); } } }
protected override void AsyncExecute(string serviceName, long pidx, long idx, WQMessage msg) { long t = DateTime.Now.Ticks; try { PlugingItem service = services[serviceName]; Log.Write(LogAction.Info, className, "AsyncExecute", serviceName, DateTime.Now.Ticks - t, "准备异步处理:serviceName:" + serviceName + ",service.RoutingGroupName:" + service.RoutingGroupName + ",msg.TransactionID:" + msg.TransactionID + ",pidx:" + pidx + ",idx:" + idx); //Dictionary<long, Dictionary<long, string>> groups = ServiceConfigs.ServiceGroups; Dictionary<long, string> group = null; long subIdx = msg.SubRoutingKey; foreach (long pgk in groups.Keys) { if ((pidx & pgk) != 0) { group = groups[pgk]; foreach (long gk in group.Keys) { if ((subIdx & gk) != 0) AsyncExecuteItem(serviceName, msg, pidx, idx, gk, group[gk]); } } } } catch (Exception e) { Log.Write(LogAction.Error, className, "AsyncExecute", serviceName, DateTime.Now.Ticks - t, string.Format(errorFormat, serviceName, msg.ToKeyString() + ",pidx:" + pidx + ",idx:" + idx, e.ToString())); } }
/// <summary> /// 同步模式:处理 /// </summary> /// <param name="msg"></param> /// <returns></returns> public override object DoExecuteMessageRequest(string serviceName, WQMessage msg, bool async) { // Log.Write("ProxyHandler.DoExecuteMessageRequest:" + services[serviceName].GroupName); Log.Write(LogAction.Info, className, "DoExecuteMessageRequest" , serviceName, -1, string.Format(startFormat, serviceName, msg.TransactionID)); long t = DateTime.Now.Ticks; string groupName = services[serviceName].GroupName; using (IDisposable service = ServiceFactory<IMessageService>.GetServer(groupName).Instance() as IDisposable) { try { Log.Write(LogAction.Info, className, "DoExecuteMessageRequest", serviceName, DateTime.Now.Ticks - t, "获取目标服务:"+groupName+",msg:" + msg.TransactionID); t = DateTime.Now.Ticks; object o = (service as IMessageService).RequestMessage(serviceName, msg); Log.Write(LogAction.Info, className, "DoExecuteMessageRequest", serviceName, DateTime.Now.Ticks - t, "同步代理转发:"+groupName+":"+serviceName+",msg:" + msg.ToKeyString()); return o; } catch (Exception e) { Log.Write(LogAction.Error, className, "DoExecuteMessageRequest", serviceName, DateTime.Now.Ticks - t, string.Format(errorFormat, serviceName, msg.ToKeyString(), e.ToString())); throw new Exception("消息处理失败:DoExecuteMessageRequest.serviceName:" + serviceName + ",TransactionID:" + msg.TransactionID + ",错误信息:" + e.Message); } } }
protected object EmptyRouting(string serviceName, WQMessage msg, bool async) { try { IMessageHandler handler = null; PlugingItem plug = services[serviceName]; string emptyHandlerName = ""; if (!routings.ContainsKey(plug.RoutingGroupName) || !routings[plug.RoutingGroupName].ContainsKey(msg.RoutingKey)) { Log.Write(LogAction.Info, className, "EmptyRouting" , serviceName, -1,string.Format(startFormat, serviceName, "没有找到路由,由服务默认空Handler处理:RoutingKey:" + msg.RoutingKey + ",msg:" + msg.ToKeyString())); emptyHandlerName = services[serviceName].EmptyHandlerName; } else { Routing routing = routings[plug.RoutingGroupName][msg.RoutingKey]; emptyHandlerName = routing.EmptyHandlerName; Log.Write(LogAction.Info, className, "EmptyRouting" , serviceName, -1, string.Format(startFormat, serviceName,"路由配置为空处理,由名为 " + emptyHandlerName + " 的PlugItem对应处理类处理:routing:" + routing + ",msg:" + msg.ToKeyString())); } if (emptyHandlerName != string.Empty) { handler = LoadHandler(emptyHandlerName, msg); long t = DateTime.Now.Ticks; object o = handler.DoExecuteMessageRequest(serviceName, msg, async); Log.Write(LogAction.Info, className, "EmptyRouting" , serviceName, DateTime.Now.Ticks - t, string.Format(endFormat, serviceName, "msg:" + msg.ToKeyString())); return o; } else { Log.Write(LogAction.Info, className, "EmptyRouting", serviceName,-1, string.Format(errorFormat, serviceName, "msg:" + msg.ToKeyString(),"没有找到空处理类:")); throw new Exception("消息处理失败:EmptyRouting.serviceName:" + serviceName + ",TransactionID:" + msg.TransactionID + ",错误信息: 消息没有正确的RoutingKey,并且没有指定空处理类"); } } catch (Exception e) { Log.Write(LogAction.Error, className, "EmptyRouting", serviceName, -1, string.Format(errorFormat, serviceName, "msg:" + msg.ToKeyString(),e.ToString())); throw new Exception("消息处理失败:EmptyRouting.serviceName:" + serviceName + ",TransactionID:" + msg.TransactionID + ",错误信息:" + e.Message); } }
/// <summary> /// 同步根据主题发送自定义消息 /// </summary> /// <param name="topicID"></param> /// <param name="msg"></param> /// <returns></returns> public DovaResponse RequestMessage(WQMessage msg) { long t = DateTime.Now.Ticks; //if (msg.Format == "raw") msg.Body = SerializeHelper.Serialize(msg.Format, msg.Body); DovaResponse res = null; try { object o = MessageHandler.ExecuteMessage(msg.ServiceName, msg, false); //if (msg.Format == "raw") res = SerializeHelper.DeSerialize<DovaResponse>(msg.Format, o); // else // res = o as DovaResponse; } catch (Exception e) { Log.Write(LogAction.Error, "MessageService", "RequestMessage", msg.ServiceName, DateTime.Now.Ticks - t, msg.ToKeyString() + ";" + e.ToString()); } return res; }
/// <summary> /// 同步模式:处理 /// </summary> /// <param name="msg"></param> /// <returns></returns> public override object DoExecuteMessageRequest(string serviceName, WQMessage msg, bool async) { Log.Write(LogAction.Info, className, "DoExecuteMessageRequest" , serviceName, -1, string.Format(startFormat, serviceName, msg.TransactionID)); Routing routing = Routing(serviceName, msg); if (null == routing || routing.EmptyHandlerName.Length > 0) { return EmptyRouting(serviceName, msg, async); } string svcName = routing.GroupName; long t = DateTime.Now.Ticks; DebugLog("DoExecuteMessageRequest:svcName=" + svcName); using (IDisposable service = ServiceFactory<IMessageService>.GetServer(svcName).Instance() as IDisposable) { try { Log.Write(LogAction.Info, className, "DoExecuteMessageRequest" , serviceName, DateTime.Now.Ticks - t, "获取路由目标服务:" + svcName + ",msg:" + msg.TransactionID); t = DateTime.Now.Ticks; object o = (service as IMessageService).RequestMessage(serviceName, msg); Log.Write(LogAction.Info, className, "DoExecuteMessageRequest" , serviceName, DateTime.Now.Ticks - t, "同步路由代理转发:" + svcName +":"+serviceName+ ",msg:" + msg.ToKeyString()); return o; } catch (Exception e) { Log.Write(LogAction.Error, className, "DoExecuteMessageRequest" , serviceName, DateTime.Now.Ticks - t, string.Format(errorFormat, serviceName,msg.ToKeyString(), e.ToString())); throw new Exception("消息处理失败:DoExecuteMessageRequest.serviceName:" + serviceName + ",TransactionID:" + msg.TransactionID + ",错误信息:" + e.Message); } } }