标签:
本人是借荐他人的文章目的是学习dbms_scheduler创建CHAIN类型的JOB,至于EXECUTABLE类型的JOB我己经在<DBMS_SCHEDULER执行PERL脚本加载数据>介绍了.下面转网上相关介绍:
Oracle CHAIN,也就是链,是将一串需要完成的作业连在一起,根据每一个步骤完成的不同结果来确定下面的哪一个动作需要被完成。实际上也就是if .. then ..else。比如,可能有如下的定义,step 1成功执行,执行step 2,如果step 1执行失败,执行step 3,如果step 2或step 3 执行成功,退出整个chain,如果step 2 或step 3执行失败,发送邮件并退出。CHAIN的主要步骤包括定义程序(做什么)、定义CHAIN步骤(总步骤),以及CHAIN的规则(如何做)。本文主要描述了chain的一些步骤并给出示例供大家参考。有关job,program可以参考Oracle在线文档。
CHAIN定义及实施的主要步骤
1、创建程序(定义每一步骤需要执行的子程序,调用DBMS_SCHEDULER.create_program)
2、创建CHAIN(调用DBMS_SCHEDULER.create_chain)
3、定义CHAIN步骤(也就每一步的顺序)
4、定义CHAIN规则(定义每一步的执行结果成功或失败后的处理方式)
5、激活CHAIN
6、将chain添加到job
7、CHAIN的单步调试
本人以ETL_TEST用户为例进行测试:
1.创建用户并赋予权限
create user etl_test identified by etl_test
default tablespace users
temporary tablespace temp;
grant create job to etl_test;
grant create evaluation context to etl_test;
grant create rule set to etl_test;
grant create rule to etl_test;
2.创建测试表及序列
create table tb_schduler_chain
(
id number,
job_name varchar2(30),
run_date date
);
create sequence tb_schduler_chain_seq;
3.下面为CHAIN定义及实施的主要步骤
A.创建程序,创建TEST_PRO_1,TEST_PRO_2,TEST_PRO_3三个程序,将job运行情况记录到测试表中
begin
dbms_scheduler.create_program (
program_name => ‘TEST_PRO_1‘,
program_type => ‘PLSQL_BLOCK‘,
program_action => ‘begin
insert into tb_schduler_chain (id, job_name, run_date)
values (tb_schduler_chain_seq.nextval, ‘‘JOB_NAME_1‘‘, sysdate);
commit;
end;‘,
enabled => TRUE,
comments => ‘program for first link in the chain.‘
);
dbms_scheduler.create_program (
program_name => ‘TEST_PRO_2‘,
program_type => ‘PLSQL_BLOCK‘,
program_action => ‘begin
insert into tb_schduler_chain (id, job_name, run_date)
values (tb_schduler_chain_seq.nextval, ‘‘JOB_NAME_2‘‘, sysdate);
commit;
end;‘,
enabled => TRUE,
comments => ‘program for second link in the chain.‘);
dbms_scheduler.create_program (
program_name => ‘TEST_PRO_3‘,
program_type => ‘PLSQL_BLOCK‘,
program_action => ‘begin
insert into tb_schduler_chain (id, job_name, run_date)
values (tb_schduler_chain_seq.nextval, ‘‘JOB_NAME_3‘‘, sysdate);
commit;
end;‘,
enabled => TRUE,
comments => ‘program for last link in the chain.‘);
end;
B.创建CHAIN,此步一般只需要CHAIN名称即可.
begin
dbms_scheduler.create_chain (
chain_name => ‘TEST_CHAIN‘,
rule_set_name => NULL,
evaluation_interval => NULL,
comments => ‘test chain‘
);
end;
C.定义STEP CHAIN 定义CHAIN的每一个步骤以及其对应的PROGRAM_NAME,也就是每一步需要做什么
begin
dbms_scheduler.define_chain_step (
chain_name => ‘TEST_CHAIN‘,
step_name => ‘CHAIN_STEP_1‘,
program_name => ‘TEST_PRO_1‘);
dbms_scheduler.define_chain_step (
chain_name => ‘TEST_CHAIN‘,
step_name => ‘CHAIN_STEP_2‘,
program_name => ‘TEST_PRO_2‘);
dbms_scheduler.define_chain_step (
chain_name => ‘TEST_CHAIN‘,
step_name => ‘CHAIN_STEP_3‘,
program_name => ‘TEST_PRO_3‘);
end;
D.定义规则
begin
dbms_scheduler.define_chain_rule (
chain_name => ‘TEST_CHAIN‘,
condition => ‘TRUE‘,
action => ‘START "CHAIN_STEP_1"‘,
rule_name => ‘CHAIN_RULE_1‘,
comments => ‘first link in the chain.‘);
dbms_scheduler.define_chain_rule (
chain_name => ‘TEST_CHAIN‘,
condition => ‘"CHAIN_STEP_1" COMPLETED‘,
action => ‘START "CHAIN_STEP_2"‘,
rule_name => ‘CHAIN_RULE_2‘,
comments => ‘second link in the chain.‘);
dbms_scheduler.define_chain_rule (
chain_name => ‘TEST_CHAIN‘,
condition => ‘"CHAIN_STEP_2" COMPLETED‘,
action => ‘START "CHAIN_STEP_3"‘,
rule_name => ‘CHAIN_RULE_3‘,
comments => ‘third link in the chain.‘);
dbms_scheduler.define_chain_rule (
chain_name => ‘TEST_CHAIN‘,
condition => ‘"CHAIN_STEP_3" COMPLETED‘,
action => ‘END‘,
rule_name => ‘CHAIN_RULE_4‘,
comments => ‘end of the chain.‘);
end;
F.激活CHAIN
begin
dbms_scheduler.enable (‘TEST_CHAIN‘);
end;
G.将CHAIN添加到JOB中自动运行或手工运行或通过RUN_CHAIN给定一个JOB名称运行
1.创建JOB自动运行
begin
dbms_scheduler.create_job (
job_name => ‘TEST_CHAIN_AUTO_JOB‘,
job_type => ‘CHAIN‘,
job_action => ‘TEST_CHAIN‘,
repeat_interval => ‘freq=minutely; interval=2‘,
start_date => systimestamp,
end_date => systimestamp + (1/48),
enabled => false);
end;
2.创建手工JOB并执行
begin
dbms_scheduler.create_job (
job_name => ‘TEST_CHAIN_MANU_JOB‘,
job_type => ‘CHAIN‘,
job_action => ‘TEST_CHAIN‘,
enabled => FALSE);
end;
begin
dbms_scheduler.enable(name => ‘TEST_CHAIN_MANU_JOB‘);
end;
3.以RUN_CHAIN方式执行,可以给定一个JOB名称
begin
dbms_scheduler.run_chain(chain_name => ‘TEST_CHAIN‘,start_steps =>‘CHAIN_STEP_1,CHAIN_STEP_2,CHAIN_STEP_3‘ ,job_name => ‘TEST_CHAIN_RUN_JOB‘);
end;
a.删除scheduler job
begin
dbms_scheduler.drop_job(job_name => ‘TEST_CHAIN_MANU_JOB‘);
end;
b.清除scheduler job运行日志
begin
dbms_scheduler.purge_log();
end;
c.删除chain
begin
dbms_scheduler.drop_chain(chain_name => ‘TEST_CHAIN‘);
end;
d.删除program
begin
dbms_scheduler.drop_program (program_name => ‘TEST_PRO_1‘);
end;
e.删除rule
begin
dbms_scheduler.drop_chain_rule(chain_name =>‘TEST_CHAIN‘ ,rule_name =>‘CHAIN_RULE_1‘ );
end;
f.删除step
begin
dbms_scheduler.drop_chain_step(chain_name =>‘TEST_CHAIN‘ ,step_name =>‘CHAIN_STEP_1‘ );
end;
CHAIN相关状态及视图查询
a.查看scheduler job运行状态视图:user_scheduler_job_run_details
b.查看scheduler job 视图:user_scheduler_jobs
c.查看scheduler chain 视图:user_scheduler_chains
d.查看scheduler run 视图:user_scheduler_chain_rules
e.查看scheduler step 视图:user_scheduler_chain_steps
f.查看sheduler program视图:user_scheduler_programs
标签:
原文地址:http://blog.csdn.net/warden2010/article/details/43710391