原創|使用教程|編輯:郝浩|2013-04-25 11:06:38.000|閱讀 681 次
概述: 高級隊列(AQ)是內置在Oracle服務器上的一個靈活的信息交流機制,使用這個高級隊列,你可以將來自一個工作站或服務器的信息發送到一個或是多個工作站上。dotConnect for Oracle采用類集合來充分的利用這項技術。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
高級隊列(AQ)是內置在Oracle服務器上的一個靈活的信息交流機制,使用這個高級隊列,你可以將來自一個工作站或服務器的信息發送到一個或是多個工作站上。dotConnect for Oracle采用類集合來充分的利用這項技術。
首先來看一下高級隊列(AQ),主要通過下面的幾部分來具體的了解:
高級隊列提供了數據庫集成的消息隊列功能,高級隊列信息可以持久存儲、不同數據或是數據庫上隊列之間的傳播、使用Oracle網絡服務的傳輸、HTTP、SMTP。
由于Oracle高級隊列是在數據庫表上實現的,高可用性、可擴展性和可靠性完全適用于隊列的數據。這項技術是由DBMS_AQ 和DBMS_AQADM2個包實現,DBMS_AQ包管理隊列的信息,如入隊和出隊,DBMS_AQADM則是負責管理生命周期以及屬性。
支持標準的數據庫功能,如恢復、重啟、安全性、以及隊列表的導入好和導出,還支持信息管理功能和異步通信功能。
下面是AQ的兩種功能模式:點對點模型和發布訂閱模型。在第一個中,將會有應用程序發送消息到隊列(稱為enqueuing),以及一個用于退出隊列信息的用戶應用程序(稱為dequeuing)。可以有多個用戶應用程序,但是任何的一條信息只能夠讀取一次,同時用戶可以瀏覽沒有退出隊列的信息。
在發布訂閱模型中,有一些入隊和出隊信息的應用程序,這些信息可以是針對特定的應用程序,或是被目的地所接收,應用程序接信息也被稱為代理。
dotConnect for Oracle中有一些類用于提供AQ功能,這些類主要是以下幾種:
OracleQueueMessage對象主要是代表了隊列信息,主要被用于排隊信息的參數,并被出對的方法返回。
OracleQueueMessages包含了系統生成的消息標志符和消息載荷,是char或用戶定義的對象類型。
在這里有一個點到點的消息擴展的示例,方便大家理解:
[C#]
OracleConnection oracleConnection = new OracleConnection( "User Id=system;Password=manager;Server=ora;"); oracleConnection.Open(); OracleQueueTable oracleQueueTable = new OracleQueueTable( "QUEUE_TABLE_MESSAGE", oracleConnection); // Set sort order by priority for the queue. // The messages with higher priority will reach the recipient first. oracleQueueTable.Options.SortOrder = OracleQueueSortOrder.PriorityEnqueueTime; // Specify type of the messages in the queue. //This time each message will be represented by just a string object. //We do so to simplify the unimportant details and concentrate on the advanced features of AQ oracleQueueTable.Options.PayloadTypeName = "RAW"; // The following operations are same as in the previous example. oracleQueueTable.CreateQueueTable(); OracleQueueAdmin oracleQueueAdmin = new OracleQueueAdmin("MESSAGE_QUEUE", "QUEUE_TABLE_MESSAGE", oracleConnection); oracleQueueAdmin.CreateQueue(); oracleQueueAdmin.StartQueue(); OracleQueue oracleEnqueueQueue = new OracleQueue("MESSAGE_QUEUE", oracleConnection); // Create and send the first message. OracleQueueMessage message1 = new OracleQueueMessage(); message1.StringPayload = "First message."; message1.MessageProperties.Priority = 7; oracleEnqueueQueue.Enqueue(message1); // Create and send the second message. This message is assigned a higher priority value. // The message will be consumed first, regardless of the fact that it was sent later. OracleQueueMessage message2 = new OracleQueueMessage(); message2.StringPayload = "Second message with high priority."; message2.MessageProperties.Priority = 1; oracleEnqueueQueue.Enqueue(message2); // Create an object that receives the two messages. OracleQueue oracleDequeueQueue = new OracleQueue("MESSAGE_QUEUE", oracleConnection); oracleDequeueQueue.DequeueOptions.WaitTimeout = 1; // Retrieve the messages in a loop. Once there are two messages received, quit. // (ex.Code == 25228) specifies that the error occured is ORA-25228; it is thrown when the dequeuing timeout expires // or the end of the queue is reached without dequeuing a message. int messageCount = 0; while (messageCount < 2) { try { OracleQueueMessage msg = oracleDequeueQueue.Dequeue(); messageCount++; if (msg != null && msg.StringPayload != null) { Console.WriteLine(msg.StringPayload); } } catch(OracleException ex) { if (ex.Code == 25228) { continue; } else throw ex; } } //Stop and destroy the queue and the queue table oracleQueueAdmin.StopQueue(); oracleQueueAdmin.DropQueue(); oracleQueueTable.DropQueueTable(); oracleConnection.Close();
[Visual Basic]
Dim oracleConnection As New OracleConnection(( "User Id=system;Password=manager;Server=ora;") oracleConnection.Open() Dim oracleQueueTable As New OracleQueueTable("QUEUE_TABLE_MESSAGE", oracleConnection) ' Set sort order by priority for the queue. ' The messages with higher priority will reach the recipient first. oracleQueueTable.Options.SortOrder = OracleQueueSortOrder.PriorityEnqueueTime ' Specify type of the messages in the queue. This time each message will be represented by just a string object. ' We do so to simplify the unimportant oracleQueueTable.Options.PayloadTypeName = "RAW" ' The following operations are same as in the previous example. oracleQueueTable.CreateQueueTable() Dim oracleQueueAdmin As New OracleQueueAdmin("MESSAGE_QUEUE", _ "QUEUE_TABLE_MESSAGE", oracleConnection) oracleQueueAdmin.CreateQueue() oracleQueueAdmin.StartQueue() Dim oracleEnqueueQueue As New OracleQueue("MESSAGE_QUEUE", oracleConnection) ' Create and send the first message. Dim message1 As New OracleQueueMessage message1.StringPayload = "First message." message1.MessageProperties.Priority = 1 oracleEnqueueQueue.Enqueue(message1) ' Create and send the second message. This message is assigned a higher priority value. ' The message will be consumed first, regardless of the fact that it was sent later. Dim message2 As New OracleQueueMessage message2.StringPayload = "Second message with high priority." message2.MessageProperties.Priority = 7 oracleEnqueueQueue.Enqueue(message2) ' Create an object that receives the two messages. Dim oracleDequeueQueue As New OracleQueue("MESSAGE_QUEUE", oracleConnection) oracleDequeueQueue.DequeueOptions.WaitTimeout = 1 ' Retrieve the messages in a loop. Once there are two messages received, quit. ' (ex.Code == 25228) specifies that the error occured is ORA-25228; ' it is thrown when the dequeuing timeout expires or the end of the queue is reached ' without dequeuing a message. Dim messageCount As Integer = 0 Do While (messageCount < 2) Try Dim msg As OracleQueueMessage = oracleDequeueQueue.Dequeue() messageCount += 1 if ((Not msg Is Nothing) AndAlso (Not msg.StringPayload Is Nothing)) Then Console.WriteLine(msg.StringPayload) End If Catch ex As OracleException If (ex.Code = 25228) Then Continue Do Else Throw ex End If End Try Loop oracleQueueAdmin.StopQueue() oracleQueueAdmin.DropQueue() oracleQueueTable.DropQueueTable() oracleConnection.Close()
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件