public class QueueManage
{
///
/// 發送對象到隊列中
///
/// 隊列名稱,因為隊列名稱在一個應用中應該不改變的,所以大家最好寫在配置文件中
/// 要發出去的對象
public static void SendQueue(string QueuePath,MyBase.SmsQueue sq)
{
System.Messaging.MessageQueue mqSend=new System.Messaging.MessageQueue(QueuePath,false);
EnsureQueueExists(QueuePath);
mqSend.Send(sq);
}
///
/// 檢查隊列,如果隊列不存在,則建立
///
/// 隊列名稱
private static void EnsureQueueExists(string path)
{
if(!MessageQueue.Exists(path))
{
if(!MessageQueue.Exists(path))
{
MessageQueue.Create(path);
MessageQueue mqTemp=new MessageQueue(path);
mqTemp.SetPermissions("Everyone",System.Messaging.MessageQueueAccessRights.FullControl);
///不知道該給什麼樣的權限好,所以就給了Everone全部權限了,當然大家最好自己控制一下
}
}
}
///
/// 從隊列中取出對象列表
///
/// 隊列名稱
public static System.Collections.ArrayList GetMessage(string QueuePath)
{
MyBase.SmsQueue sq=new MyBase.SmsQueue();
System.Messaging.MessageQueue mq=new System.Messaging.MessageQueue(QueuePath,false);
mq.Formatter=new XmlMessageFormatter(new Type[] {typeof(MyBase.SmsQueue)});
System.Messaging.Message[] arrM=mq.GetAllMessages();
mq.Close();
System.Collections.ArrayList al=new System.Collections.ArrayList();
foreach(System.Messaging.Message m in arrM)
{
sq=(TimeFound.SmsGate.Base.SmsQueue)m.Body;
al.Add(sq);
}
return al;
}
}