Chains
A Scheduler chain consists of a set of related programs that run in a specified sequence. The successive
positions in the chain are referred to as “steps” in the chain, and each step can point to another
chain, a program, or an event. The chain includes the “rules” that determine what is to be done at
each step of the chain.
We’ll create a simple Scheduler chain by first creating a Scheduler chain object, then the chain
steps and the chain rules.
Creating a Chain
Since Scheduler chains use Oracle Streams Rules Engine objects, a user must have both the CREATE
JOB privilege, as well as the Rules Engine privileges, to create a chain. You can grant all the necessary
Rules Engine privileges by using a statement like this, which grants the privileges to the user nina:
SQL> BEGIN
DBMS_RULE_ADM.GRANT_SYSTEM_PRIVILEGE(DBMS_RULE_ADM.CREATE_RULE_OBJ, 'nina'),
DBMS_RULE_ADM.GRANT_SYSTEM_PRIVILEGE (
DBMS_RULE_ADM.CREATE_RULE_SET_OBJ, 'nina'),
DBMS_RULE_ADM.GRANT_SYSTEM_PRIVILEGE (
DBMS_RULE_ADM.CREATE_EVALUATION_CONTEXT_OBJ, 'nina')
END;
Now that you have the necessary privileges, let’s create a Scheduler chain called TEST_CHAIN
using the CREATE_CHAIN procedure:
SQL> BEGIN
DBMS_SCHEDULER.CREATE_CHAIN (
chain_name => 'test_chain',
rule_set_name => NULL,
evaluation_interval => NULL,
comments => NULL);
END;
Next, define the steps for the new chain using the DEFINE_CHAIN_STEP procedure. Note that a
chain step can point to a program, an event, or another chain:
SQL> BEGIN
DBMS_SCHEDULER.DEFINE_CHAIN_STEP('test_chain', 'step1', 'program1');
DBMS_SCHEDULER.DEFINE_CHAIN_STEP('test_chain', 'step2', 'program2');
DBMS_SCHEDULER.DEFINE_CHAIN_STEP('test_chain', 'step3', 'program3');
END;
Finally, to make the chain operative, you must add rules to the chain using the DEFINE_CHAIN_
RULE procedure. Chain rules determine when a step is run and specify the conditions under which a
step is run. Usually, a rule specifies that a step be run based on the fulfillment of a specific condition.
Here’s an example:
SQL> BEGIN
DBMS_SCHEDULER.DEFINE_CHAIN_RULE('test_chain', 'TRUE', 'START step1');
DBMS_SCHEDULER.DEFINE_CHAIN_RULE('test_chain', 'step1 COMPLETED',
'Start step2, step3');
DBMS_SCHEDULER.DEFINE_CHAIN_RULE('test_chain',
'step2 COMPLETED AND step3 COMPLETED', 'END');
END;
The first rule in the preceding example specifies that step1 be run, which means that the
Scheduler will start program1. The second rule specifies that step2 (program2) and step3 (program3)
be run if step1 has completed successfully ('step1 COMPLETED'). The final rule says that when step2
and step3 finish, the chain will end.
Enabling a Chain
You must enable a chain before you can use it. Here’s how to do so:
SQL> BEGIN
DBMS_SCHEDULER.ENABLE ('test_chain');
END;
Embedding Jobs in Chains
In order to run a job within a Scheduler chain, you must create a job with the JOB_TYPE attribute set
to CHAIN, and the JOB_ACTION attribute pointing to the name of the particular chain you wish to use.
Of course, this means that you must first create the chain.
Here’s the syntax for creating a job for a Scheduler chain:
SQL> BEGIN
DBMS_SCHEDULER.CREATE_JOB (
JOB_NAME => 'test_chain_job',
JOB_TYPE => 'CHAIN',
JOB_ACTION => 'test_chain',
REPEAT_INTERVAL => 'freq=daily;byhour=13;byminute=0;bysecond=0',
ENABLED => TRUE);
END;
You also have the option of using the RUN_CHAIN procedure to run a chain without creating a
job first. The procedure will create a temporary job and immediately run the chain. Here’s how you
do this:
SQL> BEGIN
DBMS_SCHEDULER.RUN_CHAIN (
CHAIN_NAME => 'my_chain1',
JOB_NAME => 'quick_chain_job',
START_STEPS => 'my_step1, my_step2');
END;
As with the other components of the Scheduler, there are procedures that enable you to drop a
chain, drop rules from a chain, disable a chain, alter a chain, and so on. For the details, please refer
to the section about the DBMS_SCHEDULER package in the Oracle manual, “PL/SQL Packages and
Types Reference.
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/1768615/viewspace-928031/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/1768615/viewspace-928031/