/// <summary> /// 反序列化把json字节流转换成实体类 /// </summary> /// <param name="msg"></param> /// <returns></returns> private MQModel ConvertObjectModelByJson(string msg) { try { Log.Instance.Info(string.Format("接收消息字节流:{0}", msg)); DataContractJsonSerializer seralizer = new DataContractJsonSerializer(typeof(MQModel)); MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(msg)); object obj = seralizer.ReadObject(stream); MQModel model = (MQModel)obj; return(model); } catch (Exception ex) { Log.Instance.Error("反序列化解析消息出现错误", ex); throw new Exception(ex.Message); } }
/// <summary> /// 消息事件处理 /// </summary> /// <param name="message"></param> private void Consumer_Listener(IMessage message) { try { ITextMessage msg = (ITextMessage)message; //指定委托进行消息处理 Task.Factory.StartNew(() => { MQModel model = ConvertObjectModelByJson(msg.Text); Log.Instance.Info(string.Format("处理ActiveMQ消息[Guid:{0},Method:{1},ContenJson:{2}]", model.Guid, model.Method, model.ContenJson)); }); } catch (Exception ex) { Log.Instance.Error("注册监听事件出现错误", ex); throw new Exception(ex.Message); } }
/// <summary> /// 向消息对列发送消息 /// </summary> /// <param name="guid"></param> /// <param name="method"></param> /// <param name="contenjson"></param> protected void SendProduceMsg(string guid, string method, string contenjson) { try { Log.Instance.Info(string.Format("向消息队列发送消息[GUID:{0},Method:{1},ContenJson:{2}]", guid, method, contenjson)); var model = new MQModel() { Guid = guid, Method = method, ContenJson = contenjson }; var msg = session.CreateObjectMessage(model); messageProducer.Send(msg); } catch (Exception ex) { Log.Instance.Error("向消息对列发送消息出现错误", ex); throw new Exception(ex.Message); } }