Example #1
0
        public MessageConsumer CreateMessageConsumer()
        {
            CN100MessageQueue messageQueue = GetConnect();
            MessageConsumer   consumer     = new MessageConsumer(messageQueue);

            return(consumer);
        }
Example #2
0
 /// <summary>
 /// 回收一个连接
 /// </summary>
 private void ReturnObjectToPool(CN100MessageQueue queue)
 {
     if (queue != null)
     {
         lock (queuePool)
         {
             queuePool.Push(queue);
         }
     }
 }
Example #3
0
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="obj"></param>
        public void Send(object obj)
        {
            CN100MessageQueue queue = GetConnect();

            if (queue.QueueConfig.isTransactional)  //事务性消息
            {
                SendByTransactional(queue, obj);
            }
            else  //非事务性消息
            {
                Send(queue, obj);
            }
        }
Example #4
0
        /// <summary>
        /// 开始执行
        /// </summary>
        private void Start(ItemConfig conf)
        {
            try
            {
                mq             = new CN100MessageQueue(string.Format(@"FormatName:DIRECT=TCP:{0}\private$\{1}", conf.host, conf.queueName));
                mq.Formatter   = MessageFormatter;
                mq.QueueConfig = conf;

                m_Host            = conf.host;
                m_QueueName       = conf.queueName;
                m_IsTransactional = conf.isTransactional;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #5
0
 /// <summary>
 /// 发送非事务性消息
 /// </summary>
 /// <param name="queue"></param>
 private void Send(CN100MessageQueue queue, object obj)
 {
     lock (queue)
     {
         try
         {
             queue.Send(obj);
         }
         catch (Exception ex)
         {
             throw ex;
         }
         finally
         {
             ReturnObjectToPool(queue);
         }
     }
 }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="queue"></param>
        public MessageConsumer(CN100MessageQueue queue)
        {
            //获取所设置的线程数,如果没有则使用默认设置

            if (CN100.MSMQ.MSMQSection.Instance != null &&
                CN100.MSMQ.MSMQSection.Instance.Connections != null &&
                CN100.MSMQ.MSMQSection.Instance.Connections.MaxThreads > 1)
            MaxThreads = CN100.MSMQ.MSMQSection.Instance.Connections.MaxThreads;


            messageQueue = queue;


            StringBuilder logsb = new StringBuilder("侦听消息开始!");
            logsb.Append("MSMQ服务器地址:" + queue.QueueConfig.host + ";");
            logsb.Append("队列名称:" + queue.QueueConfig.queueName + ";");
            logsb.Append("是否是事务性队列:" + queue.QueueConfig.isTransactional + ";");
            logsb.Append("消息接收线程数:" + MaxThreads + ";");

            Logger.MQLog.Info(logsb.ToString());
        }
Example #7
0
        /// <summary>
        /// 获取一个链接
        /// </summary>
        /// <returns></returns>
        private CN100MessageQueue GetConnect()
        {
            lock (queuePool)
            {
                CN100MessageQueue result = null;
                if (queuePool.Count > 0)
                {
                    result = queuePool.Pop();
                }
                else
                {
                    result = mq;
                }

                if (result == null)
                {
                    throw new Exception("请先执行Start方法!");
                }

                return(result);
            }
        }
Example #8
0
        /// <summary>
        /// 发送事务性消息
        /// </summary>
        /// <param name="queue"></param>
        private void SendByTransactional(CN100MessageQueue queue, object obj)
        {
            lock (queue)
            {
                MessageQueueTransaction tran = new MessageQueueTransaction();
                tran.Begin();
                try
                {
                    queue.Send(obj, tran);
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Abort();

                    throw ex;
                }
                finally
                {
                    ReturnObjectToPool(queue);
                }
            }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="queue"></param>
        public MessageConsumer(CN100MessageQueue queue)
        {
            //获取所设置的线程数,如果没有则使用默认设置

            if (CN100.MSMQ.MSMQSection.Instance != null &&
                CN100.MSMQ.MSMQSection.Instance.Connections != null &&
                CN100.MSMQ.MSMQSection.Instance.Connections.MaxThreads > 1)
            {
                MaxThreads = CN100.MSMQ.MSMQSection.Instance.Connections.MaxThreads;
            }


            messageQueue = queue;


            StringBuilder logsb = new StringBuilder("侦听消息开始!");

            logsb.Append("MSMQ服务器地址:" + queue.QueueConfig.host + ";");
            logsb.Append("队列名称:" + queue.QueueConfig.queueName + ";");
            logsb.Append("是否是事务性队列:" + queue.QueueConfig.isTransactional + ";");
            logsb.Append("消息接收线程数:" + MaxThreads + ";");

            Logger.MQLog.Info(logsb.ToString());
        }
 /// <summary>
 /// 回收一个连接
 /// </summary>
 private void ReturnObjectToPool(CN100MessageQueue queue)
 {
     if (queue != null)
     {
         lock (queuePool)
         {
             queuePool.Push(queue);
         }
     }
 }
        /// <summary>
        /// 发送事务性消息
        /// </summary>
        /// <param name="queue"></param>
        private void SendByTransactional(CN100MessageQueue queue,object obj)
        {
            lock (queue)
            {
                MessageQueueTransaction tran = new MessageQueueTransaction();
                tran.Begin();
                try
                {
                    queue.Send(obj, tran);
                    tran.Commit();

                }
                catch (Exception ex)
                {
                    tran.Abort();

                    throw ex;
                }
                finally
                {
                    ReturnObjectToPool(queue);
                }
            }
        }
 /// <summary>
 /// 发送非事务性消息
 /// </summary>
 /// <param name="queue"></param>
 private void Send(CN100MessageQueue queue, object obj)
 {
     lock (queue)
     {
         try
         {
             queue.Send(obj);
         }
         catch (Exception ex)
         {
             throw ex;
         }
         finally
         {
             ReturnObjectToPool(queue);
         }
     }
 }
        /// <summary>
        /// 开始执行
        /// </summary>
        private void Start(ItemConfig conf)
        {
            try
            {
                mq = new CN100MessageQueue(string.Format(@"FormatName:DIRECT=TCP:{0}\private$\{1}", conf.host, conf.queueName));
                mq.Formatter = MessageFormatter;
                mq.QueueConfig = conf;

                m_Host = conf.host;
                m_QueueName = conf.queueName;
                m_IsTransactional = conf.isTransactional;

            }
            catch(Exception ex)
            {
                throw ex;
            }
        }