/// <summary> /// 订阅主题信息 /// </summary> public bool SubscribeMsg(MQMsgType msgType, string topicName, string filter = null) { try { if (!connection.IsStarted) { return(false); } //建立Session ISession session = connection.CreateSession(); IMessageConsumer consumer = null; //创建Consumer接受消息 if (msgType == MQMsgType.TOPIC) { ITopic topic = SessionUtil.GetTopic(session, topicName); if (String.IsNullOrEmpty(filter)) { consumer = session.CreateConsumer(topic); } else { consumer = session.CreateConsumer(topic, filter); } } else if (msgType == MQMsgType.QUEUE) { IQueue queue = SessionUtil.GetQueue(session, topicName); if (String.IsNullOrEmpty(filter)) { consumer = session.CreateConsumer(queue); } else { consumer = session.CreateConsumer(queue, filter); } } consumer.Listener += new MessageListener(OnMessage); return(true); } catch (Exception ex) { throw ex; } }
private void Publish <T>(List <T> data, string clientId, MQMsgType msgType, string topic, string refreshType, bool sendFinishMsg = false) { if (data == null || data.Count == 0) { return; } Dictionary <string, object> properties = new Dictionary <string, object>(); properties["refreshType"] = refreshType; if (!string.IsNullOrEmpty(clientId)) { properties["clientId"] = clientId; } string msgId; for (int index = 0; index < data.Count; index += this.config.RecordsPerPackage) { var subList = data.Skip(index).Take(this.config.RecordsPerPackage).ToList(); // var dataBytes = JsonSerializer.ObjectToBytes<List<T>>(subList); var dataStr = JsonSerializer.ObjectToString <List <T> >(subList); bool succeed = this.mqProducer.SendMsg(msgType, topic, dataStr, properties, 1000, out msgId); if (succeed) { this.logHelper.LogInfoMsg("向MQ发送数据成功,clientId={4},topic={5},refreshType={6},MsgId={0},数据类型={1},数量={2},数据:\r\n{3}", msgId, typeof(T).Name, subList.Count, dataStr, clientId, topic, refreshType); } else { this.logHelper.LogInfoMsg("向MQ发送数据失败,clientId={4},topic={5},refreshType={6},MsgId={0},数据类型={1},数量={2},数据:\r\n{3}", msgId, typeof(T).Name, subList.Count, dataStr, clientId, topic, refreshType); } } if (sendFinishMsg) { properties["refreshSignal"] = "Finish"; bool s = this.mqProducer.SendMsg(msgType, topic, string.Empty, properties, 1000, out msgId); this.logHelper.LogInfoMsg("向MQ发送数据结束消息,MsgId={0},数据类型={1},Succeed={2}", msgId, typeof(T).Name, s); } }
/// <summary> /// 实现发送数据方法 /// </summary> public bool SendMsg(MQMsgType msgType, string topicName, byte[] data, IDictionary <string, object> properties, int timeToLiveMS, out string msgId) { try { msgId = ""; if (Connection == null) { this.logHelper.LogInfoMsg("MQProducer获取到的连接为空,可能是该MQ控件没有启动"); return(false); } if (!Connection.IsStarted) { this.logHelper.LogInfoMsg("MQProducer没有连接到MQ"); return(false); } //建立ISession,会话,一个发送或接收消息的线程 ISession session = Connection.CreateSession(); //创建Producer接受消息 if (msgType == MQMsgType.TOPIC) { ITopic topic = SessionUtil.GetTopic(session, topicName); Producer = session.CreateProducer(topic); } else if (msgType == MQMsgType.QUEUE) { IQueue queue = SessionUtil.GetQueue(session, topicName); Producer = session.CreateProducer(queue); } //持久化 Producer.DeliveryMode = MsgDeliveryMode.NonPersistent; //创建消息 IBytesMessage ibytesMessage = session.CreateBytesMessage(); if (properties != null) { //设置消息属性 foreach (KeyValuePair <string, object> pair in properties) { ibytesMessage.Properties[pair.Key] = pair.Value; } } if (data != null && data.Length > 0) { ibytesMessage.WriteBytes(data); } //发送超时时间,如果timeToLive == 0 则永远不过期 if (timeToLiveMS != 0) { Producer.TimeToLive = TimeSpan.FromMilliseconds((double)timeToLiveMS); } //向MQ发送消息 Producer.Send(ibytesMessage); msgId = ibytesMessage.NMSMessageId; return(true); } catch (Exception ex) { this.logHelper.LogErrMsg(ex, "MQProducer发送消息异常"); msgId = string.Empty; return(false); } }
/// <summary> /// 订阅主题信息 /// </summary> public bool SubscribeMsg(MQMsgType msgType, string topicName, string filter = null) { try { if (!connection.IsStarted) { return false; } //建立Session ISession session = connection.CreateSession(); IMessageConsumer consumer = null; //创建Consumer接受消息 if (msgType == MQMsgType.TOPIC) { ITopic topic = SessionUtil.GetTopic(session, topicName); if (String.IsNullOrEmpty(filter)) consumer = session.CreateConsumer(topic); else consumer = session.CreateConsumer(topic, filter); } else if (msgType == MQMsgType.QUEUE) { IQueue queue = SessionUtil.GetQueue(session, topicName); if (String.IsNullOrEmpty(filter)) consumer = session.CreateConsumer(queue); else consumer = session.CreateConsumer(queue, filter); } consumer.Listener += new MessageListener(OnMessage); return true; } catch (Exception ex) { throw ex; } }
/// <summary> /// 实现发送数据方法 /// </summary> public bool SendMsg(MQMsgType msgType, string topicName, byte[] data, IDictionary<string, object> properties, int timeToLiveMS, out string msgId) { try { msgId = ""; if (Connection == null) { this.logHelper.LogInfoMsg("MQProducer获取到的连接为空,可能是该MQ控件没有启动"); return false; } if (!Connection.IsStarted) { this.logHelper.LogInfoMsg("MQProducer没有连接到MQ"); return false; } //建立ISession,会话,一个发送或接收消息的线程 ISession session = Connection.CreateSession(); //创建Producer接受消息 if (msgType == MQMsgType.TOPIC) { ITopic topic = SessionUtil.GetTopic(session, topicName); Producer = session.CreateProducer(topic); } else if (msgType == MQMsgType.QUEUE) { IQueue queue = SessionUtil.GetQueue(session, topicName); Producer = session.CreateProducer(queue); } //持久化 Producer.DeliveryMode = MsgDeliveryMode.NonPersistent; //创建消息 IBytesMessage ibytesMessage = session.CreateBytesMessage(); if (properties != null) { //设置消息属性 foreach (KeyValuePair<string, object> pair in properties) { ibytesMessage.Properties[pair.Key] = pair.Value; } } if (data != null && data.Length > 0) ibytesMessage.WriteBytes(data); //发送超时时间,如果timeToLive == 0 则永远不过期 if (timeToLiveMS != 0) { Producer.TimeToLive = TimeSpan.FromMilliseconds((double)timeToLiveMS); } //向MQ发送消息 Producer.Send(ibytesMessage); msgId = ibytesMessage.NMSMessageId; return true; } catch (Exception ex) { this.logHelper.LogErrMsg(ex, "MQProducer发送消息异常"); msgId = string.Empty; return false; } }