訊息佇列操作

來源:互聯網
上載者:User

訊息佇列主要是用來處理兩台電腦或者兩個應用程式之間的訊息傳遞。

Receive接受完之後,就刪除該Message。Peek接受完不刪除該Message。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Messaging;
using System.Net;
using System.IO;
using System.Diagnostics;

namespace Egensource.MessageQueue.SMS
{
    public class SMS
    {
        private const string smsUrl = System.Configuration.ConfigurationSettings.AppSettings["SMSUrl"].ToString();
        /// <summary>
        /// 發送訊息佇列,用來傳送簡訊
        /// </summary>
        /// <param name="mqName">訊息佇列名稱</param>
        /// <param name="smsinfo">簡訊資訊對象</param>
        public void SendMessage(string mqName, SMSInfo smsinfo)
        {
            MessageQueue mq;
            if (MessageQueue.Exists(@".\private$\" + mqName))
            {
                mq = new MessageQueue(@".\private$\" + mqName);
            }
            else
            {
                mq = MessageQueue.Create(@".\private$\" + mqName, true);
            }
            if (mq.Transactional)
            {
                MessageQueueTransaction myTransaction = new MessageQueueTransaction();
                try
                {
                    myTransaction.Begin();
                    mq.Send(string.Format(smsUrl, smsinfo.Mobile, smsinfo.Body, myTransaction));
                    myTransaction.Commit();
                }
                catch (Exception ex)
                {
                    myTransaction.Abort();
                    WriteLog(ex.ToString());
                }

            }
        }

        /// <summary>
        /// 傳送簡訊
        /// </summary>
        /// <param name="mqName"></param>
        public void SendSMS(string mqName)
        {
            MessageQueue mq;
            string strResult = string.Empty;
            if (MessageQueue.Exists(@".\private$\" + mqName))
            {
                mq = new MessageQueue(@".\private$\" + mqName);
                if (mq.Transactional)
                {
                    Message[] messages = mq.GetAllMessages();
                    foreach (Message message1 in messages)
                    {
                        MessageQueueTransaction myTransaction = new MessageQueueTransaction();
                        try
                        {
                            myTransaction.Begin();
                            System.Messaging.Message message = mq.Receive(myTransaction);
                            message.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });                           
                            myTransaction.Commit();
                        }
                        catch (Exception ex)
                        {
                            myTransaction.Abort();
                            WriteLog("發送失敗。傳回值:" + ex.ToString() + "。");
                        }
                    }
                }
            }
        }      

        #region Write System Log
        public static void WriteLog(string ex)
        {
            if (!EventLog.SourceExists("MessageQueue"))
            {
                EventLog.CreateEventSource("MessageQueue", "MessageQueueLog");
            }
            EventLog.WriteEntry("MessageQueue", ex.ToString(), EventLogEntryType.Error);

        }
        #endregion
    }
}

0 0

0

(請您對文章做出評價)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.