Exemple #1
0
 /// <summary>
 /// 异步发送消息到队列
 /// </summary>
 /// <param name="activeMessages"></param>
 /// <param name="activeMessageOptions"></param>
 /// <returns></returns>
 public async Task SendAsync(ActiveMessage[] activeMessages, ActiveMessageOptions activeMessageOptions = null)
 {
     await Task.Run(() =>
     {
         Send(activeMessages, activeMessageOptions);
     });
 }
Exemple #2
0
 /// <summary>
 /// 异步发布消息到Topic
 /// </summary>
 /// <param name="activeMessages"></param>
 /// <param name="activeMessageOptions"></param>
 /// <returns></returns>
 public async Task PublishAsync(ActiveMessage[] activeMessages, ActiveMessageOptions activeMessageOptions = null)
 {
     await Task.Run(() =>
     {
         Publish(activeMessages, activeMessageOptions);
     });
 }
Exemple #3
0
 /// <summary>
 /// 异步发布消息到Topic
 /// </summary>
 /// <param name="topic"></param>
 /// <param name="messages"></param>
 /// <param name="activeMessageOptions"></param>
 /// <returns></returns>
 public async Task PublishAsync(string topic, string[] messages, ActiveMessageOptions activeMessageOptions = null)
 {
     await PublishAsync(messages.Select(f => new ActiveMessage()
     {
         Destination = topic,
         Message = f
     }).ToArray(), activeMessageOptions);
 }
Exemple #4
0
 /// <summary>
 /// 发布消息到Topic
 /// </summary>
 /// <param name="topic"></param>
 /// <param name="messages"></param>
 /// <param name="activeMessageOptions"></param>
 public void Publish(string topic, string[] messages, ActiveMessageOptions activeMessageOptions = null)
 {
     Publish(messages.Select(f => new ActiveMessage()
     {
         Destination = topic,
         Message     = f
     }).ToArray(), activeMessageOptions);
 }
Exemple #5
0
 /// <summary>
 /// 异步发送消息到队列
 /// </summary>
 /// <param name="queue"></param>
 /// <param name="messages"></param>
 /// <param name="activeMessageOptions"></param>
 /// <returns></returns>
 public async Task SendAsync(string queue, string[] messages, ActiveMessageOptions activeMessageOptions = null)
 {
     await SendAsync(messages.Select(f => new ActiveMessage()
     {
         Destination = queue,
         Message = f
     }).ToArray(), activeMessageOptions);
 }
Exemple #6
0
 /// <summary>
 /// 发送消息到队列
 /// </summary>
 /// <param name="queue"></param>
 /// <param name="messages"></param>
 /// <param name="activeMessageOptions"></param>
 public void Send(string queue, string[] messages, ActiveMessageOptions activeMessageOptions = null)
 {
     Send(messages.Select(f => new ActiveMessage()
     {
         Destination = queue,
         Message     = f
     }).ToArray(), activeMessageOptions);
 }
Exemple #7
0
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="activeMessageOptions"></param>
        /// <param name="activeMessages"></param>
        /// <param name="func"></param>
        /// <param name="maxTime"></param>
        private void SendMessage(ActiveMessageOptions activeMessageOptions, ActiveMessage[] activeMessages, Func <ISession, string, IDestination> func)
        {
            var connection = RentConnection();

            if (!connection.IsStarted)
            {
                connection.Start();
            }

            activeMessageOptions = activeMessageOptions ?? new ActiveMessageOptions();
            using (ISession session = connection.CreateSession(activeMessageOptions.Transactional ? AcknowledgementMode.Transactional : AcknowledgementMode.AutoAcknowledge))
            {
                ReturnConnection(connection);//创建会话后不用等会话结束

                try
                {
                    IMessageProducer producer = session.CreateProducer();
                    Dictionary <string, IDestination> dict = new Dictionary <string, IDestination>();
                    foreach (var activeMessage in activeMessages)
                    {
                        if (!dict.ContainsKey(activeMessage.Destination))
                        {
                            var _dest = func.Invoke(session, activeMessage.Destination);
                            dict[activeMessage.Destination] = _dest;
                        }
                        var dest = dict[activeMessage.Destination];

                        var message = producer.CreateTextMessage(activeMessage.Message);
                        if (activeMessage.Properties != null)
                        {
                            foreach (var item in activeMessage.Properties)
                            {
                                message.Properties[item.Key] = item.Value;
                            }
                        }

                        producer.DeliveryMode = activeMessage.IsPersistent == null || activeMessage.IsPersistent.Value ? MsgDeliveryMode.Persistent : MsgDeliveryMode.NonPersistent;
                        producer.Priority     = MsgPriority.Normal;
                        if (activeMessage.TimeToLive != null)
                        {
                            producer.Send(dest, message, producer.DeliveryMode, producer.Priority, activeMessage.TimeToLive.Value);
                        }
                        else
                        {
                            producer.Send(dest, message);
                        }
                    }
                    if (activeMessageOptions.Transactional)
                    {
                        session.Commit();
                    }
                }
                catch
                {
                    if (activeMessageOptions.Transactional)
                    {
                        session.Rollback();
                    }

                    throw;
                }
            }
        }
Exemple #8
0
 /// <summary>
 /// 异步发布消息到Topic
 /// </summary>
 /// <param name="activeMessage"></param>
 /// <param name="activeMessageOptions"></param>
 /// <returns></returns>
 public async Task PublishAsync(ActiveMessage activeMessage, ActiveMessageOptions activeMessageOptions = null)
 {
     await PublishAsync(new ActiveMessage[] { activeMessage }, activeMessageOptions);
 }
Exemple #9
0
 /// <summary>
 /// 异步发布消息到Topic
 /// </summary>
 /// <param name="topic"></param>
 /// <param name="message"></param>
 /// <param name="activeMessageOptions"></param>
 /// <returns></returns>
 public async Task PublishAsync(string topic, string message, ActiveMessageOptions activeMessageOptions = null)
 {
     await PublishAsync(topic, new string[] { message }, activeMessageOptions);
 }
Exemple #10
0
 /// <summary>
 /// 发布消息到Topic
 /// </summary>
 /// <param name="activeMessages"></param>
 /// <param name="activeMessageOptions"></param>
 public void Publish(ActiveMessage[] activeMessages, ActiveMessageOptions activeMessageOptions = null)
 {
     SendMessage(activeMessageOptions, activeMessages, (session, destination) => session.GetTopic(destination));
 }
Exemple #11
0
 /// <summary>
 /// 发布消息到Topic
 /// </summary>
 /// <param name="activeMessage"></param>
 /// <param name="activeMessageOptions"></param>
 public void Publish(ActiveMessage activeMessage, ActiveMessageOptions activeMessageOptions = null)
 {
     Publish(new ActiveMessage[] { activeMessage }, activeMessageOptions);
 }
Exemple #12
0
 /// <summary>
 /// 发布消息到Topic
 /// </summary>
 /// <param name="topic"></param>
 /// <param name="message"></param>
 /// <param name="activeMessageOptions"></param>
 public void Publish(string topic, string message, ActiveMessageOptions activeMessageOptions = null)
 {
     Publish(topic, new string[] { message }, activeMessageOptions);
 }
Exemple #13
0
 /// <summary>
 /// 异步发送消息到队列
 /// </summary>
 /// <param name="queue"></param>
 /// <param name="message"></param>
 /// <param name="activeMessageOptions"></param>
 /// <returns></returns>
 public async Task SendAsync(string queue, string message, ActiveMessageOptions activeMessageOptions = null)
 {
     await SendAsync(queue, new string[] { message }, activeMessageOptions);
 }
Exemple #14
0
 /// <summary>
 /// 发送消息到队列
 /// </summary>
 /// <param name="activeMessages"></param>
 /// <param name="activeMessageOptions"></param>
 public void Send(ActiveMessage[] activeMessages, ActiveMessageOptions activeMessageOptions = null)
 {
     SendMessage(activeMessageOptions, activeMessages, (session, destination) => session.GetQueue(destination));
 }
Exemple #15
0
 /// <summary>
 /// 发送消息到队列
 /// </summary>
 /// <param name="activeMessage"></param>
 /// <param name="activeMessageOptions"></param>
 public void Send(ActiveMessage activeMessage, ActiveMessageOptions activeMessageOptions = null)
 {
     Send(new ActiveMessage[] { activeMessage }, activeMessageOptions);
 }
Exemple #16
0
 /// <summary>
 /// 发送消息到队列
 /// </summary>
 /// <param name="queue"></param>
 /// <param name="message"></param>
 /// <param name="activeMessageOptions"></param>
 public void Send(string queue, string message, ActiveMessageOptions activeMessageOptions = null)
 {
     Send(queue, new string[] { message }, activeMessageOptions);
 }