background image

  如

“清单 1”所示,sendNotification 以 UTL_HTTP.BEGIN_REQUEST 函数发出的 HTTP 

请求的形式向客户端发送通知消息。此

 URL 包含 ORDERS 表中已更改行的 order_id。然后,

它使用

 UTL_HTTP.GET_RESPONSE 获取客户端发出的响应信息。实际上,sendNotification 

并不需要处理客户端返回的整个响应,而是只获取一个在

 RESP 记录的 reason_phrase 字段

中存储的简短消息(描述状态代码)。
 
  创建通知处理程序

 

 
  现在,您可以创建一个通知处理程序,它将借助于上面介绍的

 sendNotification 过程向

客户端发送更改通知。来看一看

“清单 2”中的 PL/SQL 过程 orders_nf_callback。 

 
  清单

 2. 处理对 OE.ORDERS 表所做更改的通知的通知处理程序 

 
代码如下

:

 
CREATE OR REPLACE PROCEDURE orders_nf_callback (ntfnds IN SYS.CHNF

$_DESC

) IS 

tblname VARCHAR2(60); 
numtables NUMBER; 
event_type NUMBER; 
row_id VARCHAR2(20); 
numrows NUMBER; 
ord_id VARCHAR2(12); 
url VARCHAR2(256) := ''; 
BEGIN 
event_type := ntfnds.event_type; 
numtables := ntfnds.numtables; 
IF (event_type = DBMS_CHANGE_NOTIFICATION.EVENT_OBJCHANGE) THEN 
FOR i IN 1..numtables LOOP 
tblname := ntfnds.table_desc_array(i).table_name; 
IF (bitand(ntfnds.table_desc_array(i).opflags, 
DBMS_CHANGE_NOTIFICATION.ALL_ROWS) = 0) THEN 
numrows := ntfnds.table_desc_array(i).numrows; 
ELSE 
numrows :=0; 

END

 IF; 

IF (tblname = 'OE.ORDERS') THEN 
FOR j IN 1..numrows LOOP 
row_id := ntfnds.table_desc_array(i).row_desc_array(j).row_id; 
SELECT order_id INTO ord_id FROM orders WHERE rowid = row_id; 
sendNotification(url, tblname, ord_id); 

END

 LOOP; 

END

 IF; 

END

 LOOP; 

END

 IF;