彩票走势图

dotConnect for Oracle使用教程:高级队列技术

原创|使用教程|编辑:郝浩|2013-04-25 11:06:38.000|阅读 621 次

概述: 高级队列(AQ)是内置在Oracle服务器上的一个灵活的信息交流机制,使用这个高级队列,你可以将来自一个工作站或服务器的信息发送到一个或是多个工作站上。dotConnect for Oracle采用类集合来充分的利用这项技术。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

相关链接:

    高级队列(AQ)是内置在Oracle服务器上的一个灵活的信息交流机制,使用这个高级队列,你可以将来自一个工作站或服务器的信息发送到一个或是多个工作站上。dotConnect for Oracle采用类集合来充分的利用这项技术。

首先来看一下高级队列(AQ),主要通过下面的几部分来具体的了解:

  • AQ基本
  • Oracle dotConnect中的AQ组件
  • 点到点的信息示例
  • 点到点信息的扩展示例
  • 点到多点信息的示例

AQ基本

   高级队列提供了数据库集成的消息队列功能,高级队列信息可以持久存储、不同数据或是数据库上队列之间的传播、使用Oracle网络服务的传输、HTTP、SMTP。

    由于Oracle高级队列是在数据库表上实现的,高可用性、可扩展性和可靠性完全适用于队列的数据。这项技术是由DBMS_AQ 和DBMS_AQADM2个包实现,DBMS_AQ包管理队列的信息,如入队和出队,DBMS_AQADM则是负责管理生命周期以及属性。

    支持标准的数据库功能,如恢复、重启、安全性、以及队列表的导入好和导出,还支持信息管理功能和异步通信功能。

    下面是AQ的两种功能模式:点对点模型和发布订阅模型。在第一个中,将会有应用程序发送消息到队列(称为enqueuing),以及一个用于退出队列信息的用户应用程序(称为dequeuing)。可以有多个用户应用程序,但是任何的一条信息只能够读取一次,同时用户可以浏览没有退出队列的信息。

dotConnect for Oracle使用教程:高级队列技术

  在发布订阅模型中,有一些入队和出队信息的应用程序,这些信息可以是针对特定的应用程序,或是被目的地所接收,应用程序接信息也被称为代理。

dotConnect for Oracle使用教程:高级队列技术

dotConnect for Oracle中的AQ组件

    dotConnect for Oracle中有一些类用于提供AQ功能,这些类主要是以下几种:

  • OracleQueueMessage
  • OracleQueue
  • OracleQueueAdmin

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@pclwef.cn

文章转载自:慧都控件

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP