/// <summary> /// 创建队列 /// </summary> /// <param name="queueName"></param> private IMessageProducer CreateProducer(string queueName) { if (_session == null) { Open(); } //创建新生产者 Func <string, IMessageProducer> CreateNewProducter = (name) => { IMessageProducer _newProducer = null; switch (MQMode) { case MQMode.Queue: { _newProducer = _session.CreateProducer(new ActiveMQQueue(name)); break; } case MQMode.Topic: { _newProducer = _session.CreateProducer(new ActiveMQTopic(name)); break; } default: { throw new Exception(string.Format("无法识别的MQMode类型:{0}", MQMode.ToString())); } } return(_newProducer); }; return(this._concrtProcuder.GetOrAdd(queueName, CreateNewProducter)); }
private IMessageConsumer CreateConsumer(string _QueueName = null) { if (!string.IsNullOrEmpty(_QueueName)) { QueueName = _QueueName; } if (string.IsNullOrEmpty(QueueName)) { throw new Exception($"队列名称不能为空"); } switch (MQMode) { case MQMode.Queue: { _consumer = _session.CreateConsumer(new ActiveMQQueue(QueueName)); break; } case MQMode.Topic: { _consumer = _session.CreateDurableConsumer(new ActiveMQTopic(QueueName), ClientID, null, false); break; } default: { throw new Exception(string.Format("无法识别的MQMode类型:{0}", MQMode.ToString())); } } return(_consumer); }
/// <summary> /// 设置监听接收到消息后的方法 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="action"></param> /// <param name="mqMode"></param> /// <param name="queueName"></param> private void SetMessageReceivedAction <T>(Action <T> action, MQMode mqMode, string queueName) where T : class { #region 消费者 _consumer.Listener += (msg) => { if (msg is ActiveMQTextMessage textMessage) { var result = textMessage.Text as T; action(result); } else if (msg is ActiveMQBytesMessage bytesMessage) { var buffer = new byte[bytesMessage.BodyLength]; bytesMessage.WriteBytes(buffer); var result = ToObject <T>(buffer); action(result); } else if (msg is ActiveMQObjectMessage objectMessage) { var result = (T)objectMessage.Body; action(result); } }; Open(); #endregion }
/// <summary> /// 创建队列 /// </summary> /// <param name="queueName"></param> private IMessageProducer CreateProducer(string queueName) { if (_session == null) { Open(); } //创建新生产者 Func <string, IMessageProducer> CreateNewProducter = (name) => { IMessageProducer newProducer = null; switch (MQMode) { case MQMode.Queue: { newProducer = _session.CreateProducer(new ActiveMQQueue(name)); //队列模式,适合一对一的情况 /*var destination = SessionUtil.GetDestination(_session, "queue://" + name); * _newProducer = _session.CreateProducer(destination);*/ break; } case MQMode.Topic: { newProducer = _session.CreateProducer(new ActiveMQTopic(name)); //发布/订阅模式,适合一对多的情况 /*var destination = SessionUtil.GetDestination(_session, "topic://" + name); * _newProducer = _session.CreateProducer(destination);*/ break; } default: { throw new Exception($"无法识别的MQMode类型:{MQMode.ToString()}"); } } return(newProducer); }; // ConcurrentDictionary使用GetOrAdd方法添加委托的Value存在线程安全问题,可使用Lazy类型来避免 // https://www.cnblogs.com/CreateMyself/p/6086752.html //多线程情况下ConcurrentDictionary的方法只保证key/value线程安全,不能保证key/valueFactory的线程安全,直接把新建的生产者当做value才行 var newProducter = CreateNewProducter(queueName); return(this._concrtProcuder.GetOrAdd(queueName + "-" + MQMode, newProducter)); }
/// <summary> /// 打开连接 /// </summary> public void Open() { if (string.IsNullOrWhiteSpace(this.BrokerUri)) { throw new MemberAccessException("未指定BrokerUri"); } if (string.IsNullOrWhiteSpace(this.QueueName)) { throw new MemberAccessException("未指定QueueName"); } var factory = new ConnectionFactory(this.BrokerUri); if (string.IsNullOrWhiteSpace(this.UserName) && string.IsNullOrWhiteSpace(this.Password)) { _connection = factory.CreateConnection(); _connection.Start(); _session = _connection.CreateSession(AcknowledgementMode.AutoAcknowledge); } else { _connection = factory.CreateConnection(this.UserName, this.Password); _connection.Start(); _session = _connection.CreateSession(AcknowledgementMode.AutoAcknowledge); } switch (MQMode) { case MQMode.Queue: { _consumer = _session.CreateConsumer(new ActiveMQQueue(this.QueueName)); break; } case MQMode.Topic: { _consumer = _session.CreateConsumer(new ActiveMQTopic(this.QueueName)); break; } default: { throw new Exception(string.Format("无法识别的MQMode类型:{0}", MQMode.ToString())); } } }
private void SendMessage <T>(T message, MQMode mqMode, string queueName) where T : class { var msgType = message.GetType(); if (msgType != typeof(string) && msgType != typeof(byte[])) { var serializableAttrs = msgType.GetCustomAttributes(typeof(SerializableAttribute), false); if (serializableAttrs.Length < 1) { throw new Exception("message的类型需要添加序列化特性SerializableAttribute(string和byte[]类型除外)"); } } #region 生产者 QueueName = queueName; MQMode = mqMode; Open(); //发送到队列, Put对象类必须使用[Serializable]注解属性 Put(message); #endregion }