ORACLE_AQ 队列
Oracle AQ Demo,Step by Step
我准备用AQ来做一个数据仓库系统,提交分析任务队列。有以下需求:
1.利用通知异步的执行存储过程
2.设定队列大小极限
3.出列即删除
OK,let's go for it
step 1:创建用户
--create user
-- Create the user
create user PHS
identified by ""
default tablespace PHSDATA
temporary tablespace TEMP
profile DEFAULT;
-- 赋予AQ管理权限
grant execute on DBMS_AQ to PHS;
grant execute on DBMS_AQ_BQVIEW to PHS;
grant execute on Dbms_Aqadm to PHS;
-- Grant/Revoke role privileges
grant connect to PHS;
grant resource to PHS;
-- Grant/Revoke system privileges
grant create procedure to PHS;
grant create table to PHS;
grant create view to PHS;
grant unlimited tablespace to PHS;
step 2:创建一个队列载体对象,一个没有body的type
create or replace type task_c as object
(
-- Author : WANGWJ
-- Created : 2008-1-8 16:00:14
-- Purpose : infomation carrier for analyse Clone-PHS
-- Attributes 业务逻辑相关
begindate DATE,
enddate DATE,
area VARCHAR2(12),
taskid NUMBER,
phscodex VARCHAR2(20)
-- Member functions and procedures
)
--创建队列表
begin
-- Call the procedure
sys.dbms_aqadm.create_queue_table(queue_table => 'QT_CLONE',
queue_payload_type => 'task_c',--这就是我们定义的type
sort_list => 'priority,enq_time',--按优先级和入列时间排序
multiple_consumers => TRUE, --多消费者
comment => 'queue for analyse CLONE-PHS',
auto_commit => FALSE --手动控制事务--create queue
);
end;
--创建队列
begin
sys.dbms_aqadm.create_queue(
queue_name => 'q_clone',
queue_table => 'qt_clone',--刚刚建立的queue表
queue_type => sys.dbms_aqadm.normal_queue,
max_retries => 3,--dequeue失败后重试次数
retry_delay => 1,--重试前等待
retention_time => 0 --dequeue后保持时间,不保持
);
end;
step 3:启动队列
execute dbms_aqadm.start_queue('q_clone',true,true);
step 4:创建消息订阅者
SQL> execute dbms_aqadm.add_subscriber ( queue_name => 'q_clone', subscriber => sys.aq$_agent
('analyst',null,null));
PL/SQL procedure successfully completed
SQL>
step 5:入列和出列测试
SQL> --入列
SQL> declare
2 v_Message task_c;
3 v_MsgId RAW(16);
4 v_options DBMS_AQ.ENQUEUE_OPTIONS_T;
5 v_properties DBMS_AQ.MESSAGE_PROPERTIES_T;
6 v_Recipients DBMS_AQ.AQ$_RECIPIENT_LIST_T;
7 begin
8 v_Message:=task_c(begindate => SYSDATE,enddate => SYSDATE-1,area => '028',
9 taskid =>100,phscodex => 'test#$');
10 v_properties.priority := 1; --该消息的优先级别
11 v_options.visibility :=DBMS_AQ.IMMEDIATE;
12 dbms_aq.enqueue(queue_name => 'q_clone',enqueue_options => v_options,message_properties =>
v_properties,payload => v_Message,msgid => v_MsgId);
13 dbms_output.put_line('encode success,msgid is '||v_MsgId);
14
15 end;
16 /
PL/SQL procedure successfully completed
--入列成功
SQL> select t.q_name,t.msgid,t.priority from qt_clone t;
Q_NAME MSGID PRIORITY
------------------------------ -------------------------------- ----------
Q_CLONE 7466C75477954808B7E10BC50738845B 1
--改变 v_properties.priority的值为3,2,再入列两次,现在入列的先后顺序为1 3 2,我们希望的出列顺序
--为1 2 3
--出列
declare
v_Message task_c;
v_MsgId RAW(16);
v_options DBMS_AQ.DEQUEUE_OPTIONS_T;
v_properties DBMS_AQ.MESSAGE_PROPERTIES_T;
v_Recipients DBMS_AQ.AQ$_RECIPIENT_LIST_T;
begin
-- v_Recipients(0) := sys.aq$_agent('NOTE','MTQ',0);
-- v_properties.recipient_list := v_Recipients;
v_options.visibility :=DBMS_AQ.IMMEDIATE;
v_options.consumer_name := 'analyst';
dbms_aq.dequeue(queue_name => 'q_clone',dequeue_options => v_options,message_properties =>
v_properties,payload => v_Message,msgid => v_MsgId);
dbms_output.put_line('decode success,msgid is '||v_MsgId);
dbms_output.put_line('subject is '||v_Message.area);
end;
--测试结果略,可以看出出列的顺序 1 2 3
step 6:创建测试过程,并注册通知
创建测试表
-- Create table 用于在接到通知的时候插入一条消息
create table TEST_AQ
(
INFO VARCHAR2(100),
MESSAGE TASK_C
)
--创建测试过程,插入一条消息,并出列
create or replace procedure plsqlnotif
AS
v_Message task_c;
v_MsgId RAW(16);
v_options DBMS_AQ.DEQUEUE_OPTIONS_T;
v_properties DBMS_AQ.MESSAGE_PROPERTIES_T;
v_Recipients DBMS_AQ.AQ$_RECIPIENT_LIST_T;
BEGIN
v_options.visibility :=DBMS_AQ.IMMEDIATE;
v_options.consumer_name := 'analyst';
dbms_aq.dequeue(queue_name => 'q_clone',dequeue_options => v_options,message_properties =>
v_properties,payload => v_Message,msgid => v_MsgId);
dbms_output.put_line('decode success,msgid is '||v_MsgId);
dbms_output.put_line('subject is '||v_Message.area);
INSERT INTO test_aq VALUES('Get message on ',v_Message);
END;
--注册
declare
reginfolist sys.aq$_reg_info_list;
begin
reginfolist := sys.aq$_reg_info_list(
sys.aq$_reg_info('phs.q_clone:analyst',
DBMS_AQ.NAMESPACE_AQ,
'plsql://phs.plsqlnotif', null));
dbms_aq.register(reginfolist, 1);
end;
step 7:测试情况
--入列 略
--接到通知后,插入test表,并出列
SQL> execute plsqlnotif;
decode success,msgid is 59578D93BD55477994D8C9C6B672242B
subject is 028
PL/SQL procedure successfully completed
SQL> select * from test_aq;
INFO MESSAGE
-------------------------------------------------------------------------------- -------
Get message on
ORACLE_AQ 队列的更多相关文章
- 消息队列——RabbitMQ学习笔记
消息队列--RabbitMQ学习笔记 1. 写在前面 昨天简单学习了一个消息队列项目--RabbitMQ,今天趁热打铁,将学到的东西记录下来. 学习的资料主要是官网给出的6个基本的消息发送/接收模型, ...
- 消息队列 Kafka 的基本知识及 .NET Core 客户端
前言 最新项目中要用到消息队列来做消息的传输,之所以选着 Kafka 是因为要配合其他 java 项目中,所以就对 Kafka 了解了一下,也算是做个笔记吧. 本篇不谈论 Kafka 和其他的一些消息 ...
- Beanstalkd一个高性能分布式内存队列系统
高性能离不开异步,异步离不开队列,内部是Producer-Consumer模型的原理. 设计中的核心概念: job:一个需要异步处理的任务,是beanstalkd中得基本单元,需要放在一个tube中: ...
- .net 分布式架构之业务消息队列
开源QQ群: .net 开源基础服务 238543768 开源地址: http://git.oschina.net/chejiangyi/Dyd.BusinessMQ ## 业务消息队列 ##业务消 ...
- 【原创经验分享】WCF之消息队列
最近都在鼓捣这个WCF,因为看到说WCF比WebService功能要强大许多,另外也看了一些公司的招聘信息,貌似一些中.高级的程序员招聘,都有提及到WCF这一块,所以,自己也关心关心一下,虽然目前工作 ...
- 缓存、队列(Memcached、redis、RabbitMQ)
本章内容: Memcached 简介.安装.使用 Python 操作 Memcached 天生支持集群 redis 简介.安装.使用.实例 Python 操作 Redis String.Hash.Li ...
- Java消息队列--ActiveMq 实战
1.下载安装ActiveMQ ActiveMQ官网下载地址:http://activemq.apache.org/download.html ActiveMQ 提供了Windows 和Linux.Un ...
- Java消息队列--JMS概述
1.什么是JMS JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送 ...
- 消息队列性能对比——ActiveMQ、RabbitMQ与ZeroMQ(译文)
Dissecting Message Queues 概述: 我花了一些时间解剖各种库执行分布式消息.在这个分析中,我看了几个不同的方面,包括API特性,易于部署和维护,以及性能质量..消息队列已经被分 ...
随机推荐
- 一个Java开发的Python之路----------------(一)
最近开始学习Python了,主要是因为现在在给海航通过JAVA写CMDB运维管理平台,我就是作为唯一一个坐在运维屋里的开发,又当爹,又当妈,前端,后台,测试,设计,需求, 发布,统统一把抓!!在Git ...
- msp430入门编程05
msp430中C语言的运算符和表达式 msp430中C语言的程序结构06 msp430中C语言的函数及实现07 msp430中C语言操作端口I/O10 msp430中C语言的模块化头文件及实现11 m ...
- abs 暴力
Given a number x, ask positive integer y≥2y≥2, that satisfy the following conditions: 1. The absolut ...
- Model、ModelMap、ModelAndView的使用和区别
1.Model的使用 数据传递:Model是通过addAttribute方法向页面传递数据的: 数据获取:JSP页面可以通过el表达式或C标签库的方法获取数据: return:return返回的是指定 ...
- JSP的异常处理
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/exception-handling.html: 当写JSP代码的时候,有可能会留下一个编码错误,并且它会 ...
- python 交互模式 方向键乱码问题解决
python交互模式下通常用向上键来找到之前执行的命令,用左右键移动光标.这很方便.但有的时候这些键在按完后却会出现乱码. 本文只解决CentOS 6.4 下 python2.7.8 的乱码问题. 这 ...
- JavaScript你所不知道的困惑(1)
困惑一: 先看一个样例: function test(){ message = "hi"; } test(); alert(message); 会输出字符串"hi&quo ...
- 使用逆向工程生成mybatis的Mapper文件
之前有写过一篇博客: 使用MyBatis Generator自动生成MyBatis的代码链接:http://www.cnblogs.com/klslb/p/6908535.html 这个太麻烦了,而且 ...
- 用py文件调用操作系统的命名,粘包问题
帅爆太阳的男人 1,执行代码 在py代码中去调用操作系统的命令 新的模块:subprocess, import subprocess r = subprocess().Popen( "dir ...
- Python extensions for Windows
Python extensions for Windows pywin32 214 Python extensions for Windows Maintainer: Mark Hammond Hom ...