public Task <ITopic> GetTopic(string topicName) { return(_executor.Run(() => SessionUtil.GetTopic(_session, topicName), CancellationToken)); }
/// <summary> /// Initializes listening to a specific path on the Active MQ server. /// Please note, that, if not otherwise specified, it will be assumed that the path should be a queue. /// As such, if you want to specifically listen to a queue, prepend the path parameter with "queue://" otherwise with "topic://" /// </summary> /// <param name="path">The path to the message exchange</param> /// <param name="subscription">Not used by Active MQ. The subscription will be named automatically by the active mq server</param> /// <param name="filter">An SQL-like string to filter for values set on the meta properties of a message. This maps directly to the "properties" parameter (including "ContentType") of SendAsync</param> /// <returns></returns> public async Task <bool> ListenAsync(string path, string subscription = null, string filter = null) { _logger.Trace(ConnectorLogging.Process((nameof(path), path), (nameof(subscription), subscription), (nameof(filter), filter))); if (_isInternalListeningHooked) { _logger.Error($"Currently already listening to some path. Please call {nameof(StopListening)} before calling {nameof(ListenAsync)} again"); return(false); } if (!_isConnectionGood) { _logger.Error($"Currently not connected. Please wait for connection to be established before listening"); return(false); } if (_session == null) { _logger.Error($"Cannot listen to path if session has not been established"); return(false); } if (string.IsNullOrEmpty(path)) { _logger.Error($"Bad Argument: {nameof(path)} was null"); return(false); } PrependWithPathDefault(ref path); try { _listenToPath = path; _listenToSubscription = subscription; _listenToFilter = filter; IDestination destination; //todo: add code to verify, path is not for queue when subscription is not null and vice versa destination = SessionUtil.GetDestination(_session, path); ITopic topicDestination = SessionUtil.GetTopic(_session, path); if (string.IsNullOrEmpty(filter)) { if (path.StartsWith("topic://")) { _logger.Trace($"Creating durable consumer for {topicDestination.ToString()}"); _consumer = _session.CreateDurableConsumer(topicDestination, subscription, filter, false); } else if (path.StartsWith("queue://")) { _logger.Trace($"Creating consumer for {destination.ToString()}"); _consumer = _session.CreateConsumer(destination); } else { _logger.Error($"Could not start listening because a path of {path} cannot be handled"); return(false); } } else { if (path.StartsWith("topic://")) { _logger.Trace($"Creating durable consumer for {topicDestination.ToString()}"); _consumer = _session.CreateDurableConsumer(topicDestination, subscription, filter, false); } else if (path.StartsWith("queue://")) { _logger.Trace($"Creating consumer for {destination.ToString()}"); _consumer = _session.CreateConsumer(destination, filter); } else { _logger.Error($"Could not start listening because a path of {path} cannot be handled"); return(false); } } _consumer.Listener += OnMessageReceived; _isInternalListeningHooked = true; _logger.Info($"Initialization for listening to {path} successful."); return(true); } catch (Exception e) { _logger.Error($"Exception while starting listener: {e.ToString()}"); } return(false); }
/// <summary> /// Extension function to get the destination by parsing the embedded type prefix. /// </summary> public static ITopic GetTopic(this ISession session, string topicName) { return(SessionUtil.GetTopic(session, topicName)); }
public Task <ITopic> GetTopic(string topicName) { return(Task.Factory.StartNew(() => SessionUtil.GetTopic(_session, topicName), CancellationToken, TaskCreationOptions.None, _taskScheduler)); }
/// <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); } }
public TopicSubscriber(ISession session, string destination) { this.session = session; this.destination = destination; topic = SessionUtil.GetTopic(session, this.destination); }