/// <summary> /// 发送消息 /// 支持json可序列化,byte[],string类型 /// mqpath:"queue://FOO.BAR",topic://FOO.BAR 示例 /// </summary> public void SendMessage <T>(T obj, string mqpath, Dictionary <string, string> propertydic, ActiveMQMsgDeliveryMode mode = ActiveMQMsgDeliveryMode.Persistent) { var json = ""; if (!(obj is string)) { json = new Serialization.JsonHelper().Serializer(obj); } else { json = obj as string; } IDestination destination = SessionUtil.GetDestination(Session, mqpath); //通过会话创建生产者,方法里面new出来的是MQ中的Queue IMessageProducer prod = Session.CreateProducer(destination); //创建一个发送的消息对象 ITextMessage message = prod.CreateTextMessage(); //给这个对象赋实际的消息 message.Text = json; //设置消息对象的属性,这个很重要哦,是Queue的过滤条件,也是P2P消息的唯一指定属性 if (propertydic != null) { foreach (var map in propertydic) { message.Properties.SetString(map.Key, map.Value); } } //生产者把消息发送出去,几个枚举参数MsgDeliveryMode是否长链,MsgPriority消息优先级别,发送最小单位,当然还有其他重载 prod.Send(message, (MsgDeliveryMode)mode, MsgPriority.Normal, TimeSpan.MinValue); }
/// <summary> /// 发送消息 /// 支持json可序列化,byte[],string类型 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="queuename"></param> /// <param name="obj"></param> public void SendMessage <T>(string queuename, T obj) { byte[] data = null; if (!(obj is string) && !(obj is byte[])) { var json = new Serialization.JsonHelper().Serializer(obj); data = XXF.Db.LibConvert.StrToBytes(json); } else if (obj is string) { data = XXF.Db.LibConvert.StrToBytes(obj as string); } else if (obj is byte[]) { data = obj as byte[]; } client.LPush(MQTag + queuename, data); }
/// <summary> /// 发送消息 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="objmsg"></param> public void SendMessage <T>(T objmsg) { try { var json = ""; ProducterTimeWatchInfo.JsonHelperSerializer += Log.TimeWatchLog.Debug(() => { if (!(objmsg is string)) { json = new Serialization.JsonHelper().Serializer(objmsg); } else { json = objmsg as string; } }); //发送消息有n次重试机会 int errortrycount = 0; while (errortrycount < Context.ProducterInfo.LoadBalance.SendMessageErrorTryAgainCount) { LoadBalanceNodeInfo loadbalancenodeinfo = null; ProducterTimeWatchInfo.GetLoadBalanceNodeInfo += Log.TimeWatchLog.Debug(() => { loadbalancenodeinfo = Context.ProducterInfo.GetLoadBalanceNodeInfo(); }); if (loadbalancenodeinfo == null) { throw new BusinessMQException(string.Format("无法获取队列{0}的可用的负载均衡数据节点", MQPath)); } string datanodeconnectstring = new ProducterBLL().GetDataNodeConnectString(SystemParamConfig.Producter_DataNode_ConnectString_Template_ToSendMessage, loadbalancenodeinfo.DataNodeModel); var partitionidinfo = PartitionRuleHelper.GetPartitionIDInfo(loadbalancenodeinfo.MQPathPartitionModel.partitionid); var manageservertime = Context.ManageServerTime;//.AddSeconds(1);发送消息要比标准时间提前1s,这样消息分表可以提前1s string tablename = PartitionRuleHelper.GetTableName(partitionidinfo.TablePartition, manageservertime); try { ProducterTimeWatchInfo.SendMessage += Log.TimeWatchLog.Debug(() => { double inserttime = 0; double allinserttime = Log.TimeWatchLog.Debug(() => { inserttime = Log.TimeWatchLog.Debug(() => { tb_messagequeue_dal dal = new tb_messagequeue_dal(); dal.TableName = tablename; SqlHelper.ExcuteSql(datanodeconnectstring, (c) => { dal.Add2(c, new tb_messagequeue_model() { message = json, mqcreatetime = DateTime.Now, sqlcreatetime = manageservertime , source = (int)EnumMessageSource.Common, state = (int)EnumMessageState.CanRead }); }); }); }); //ProducterTimeWatchTest.AddMessages(string.Format("总插入消息:{0}s,插入消息:{1}s",allinserttime,inserttime)); }); NetCommand.SendMessage(mqpath); return; } catch (SqlException exp) { ErrorLogHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "SendMessage", string.Format("发送消息出现节点错误,节点:{0}", loadbalancenodeinfo.DataNodeModel.datanodepartition), exp); Context.ProducterInfo.RemoveMQPathPartition(loadbalancenodeinfo.DataNodeModel.datanodepartition);//数据层出错视为数据节点异常,则移除。将在一定时间内尝试恢复 Context.ProducterInfo.LoadBalance.AddError(new ErrorLoadBalancePartitionInfo() { PartitionId = loadbalancenodeinfo.MQPathPartitionModel.partitionid, PartitionIndex = loadbalancenodeinfo.MQPathPartitionModel.partitionindex }); //Context.IsNeedReload = true; if (Context.SendMessageErrorTime == null) { Context.SendMessageErrorTime = DateTime.Now; } } errortrycount++; } throw new BusinessMQException(string.Format("发送消息出现系统级错误,并超过重试次数,请检查。队列:{0}", MQPath)); } catch (Exception exp) { ErrorLogHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "SendMessage", "生产者发送消息出错", exp); throw exp; } }