Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Oracle Database 11g.pdf
Скачиваний:
78
Добавлен:
10.06.2015
Размер:
12.69 Mб
Скачать

Creating Lightweight Jobs 

589

8 return_date_after := start_date;

9FOR i IN 1..10 LOOP

10DBMS_SCHEDULER.EVALUATE_CALENDAR_STRING(

11‘FREQ=MONTHLY; INTERVAL=2; BYMONTHDAY=15’,

12start_date, return_date_after, next_run_date);

13DBMS_OUTPUT.PUT_LINE(

14‘next_run_date: ‘ || next_run_date);

15return_date_after := next_run_date;

16END LOOP;

17END;

SQL> /

next_run_date: 15-OCT-08 10.00.00.000000 AM next_run_date: 15-DEC-08 10.00.00.000000 AM next_run_date: 15-FEB-09 10.00.00.000000 AM next_run_date: 15-APR-09 10.00.00.000000 AM next_run_date: 15-JUN-09 10.00.00.000000 AM next_run_date: 15-AUG-09 10.00.00.000000 AM next_run_date: 15-OCT-09 10.00.00.000000 AM next_run_date: 15-DEC-09 10.00.00.000000 AM next_run_date: 15-FEB-10 10.00.00.000000 AM next_run_date: 15-APR-10 10.00.00.000000 AM

PL/SQL procedure successfully completed.

This example calls the procedure inside of a loop, and each time through, it uses the NEXT_ RUN_DATE returned from the prior call as the value for the RETURN_DATE_AFTER parameter. This tells Oracle to only return a date that is farther in the future than the date specified. Therefore, you will get each successive execution date.

Creating Lightweight Jobs

New to Oracle 11g, a lightweight job is defined by indicating LIGHTWEIGHT as the value for job_style when creating the job. Lightweight jobs have the following characteristics:

NN

They are not schema objects

NNBecause they are not schema objects, they have lower overhead and better create and drop time when compared to regular jobs.

NN

They store less metadata and job runtime data than regular jobs.

NNThey must reference an enabled ‘PLSQL_BLOCK’ or ‘STORED_PROCEDURE’ program to specify a job action.

590  Chapter 12  n  Using the Scheduler to Automate Tasks

Lightweight jobs inherit their privileges from the program; you cannot grant privileges to lightweight jobs. Consider using a lightweight job when you have a high-frequency short-duration job.

Here’s an example of a PL/SQL block that creates a lightweight job.

BEGIN DBMS_SCHEDULER.CREATE_JOB (

job_name => ‘example_lightweight_job’, program_name => ‘lne_prog’,

repeat_interval => ‘FREQ=SECONDLY;INTERVAL=30’, job_style => ‘LIGHTWEIGHT’,

comments => ‘Heartbeat monitor job’); END;

/

In Exercise 12.2, you’ll create a lightweight job and execute it.

E x e r c i s e 12 . 2

Creating and Executing a Lightweight Job

For this exercise, we’ll create a lightweight job and execute it. Use the table LNE_TEST created in Exercise 12.1.

1.Create a stored procedure that inserts sysdate into a row in a table.

create or replace procedure LNE_TEST_PROC as

begin

insert into LNE_TEST select sysdate from dual; end;

/

2.Create and enable a program for the stored procedure using dbms_scheduler.create_ program, and dbms_scheduler.enable.

begin dbms_scheduler.create_program( program_name => ‘LNE_PROGRAM’,

program_type => ‘STORED_PROCEDURE’, program_action => ‘LNE_TEST_PROC’, number_of_arguments => 0,

comments => ‘Insert SYSDATE into LNE_TEST table’); end;

Using Job Chains 

591

E x e r c i s e 12 . 2   ( c o n t i n u e d )

/

exec dbms_scheduler.enable(‘LNE_PROGRAM’);

3. Create a lightweight job using dbms_scheduler.create_job to execute the stored procedure every 30 seconds.

begin dbms_scheduler.create_job (

job_name => ‘LNE_LIGHTWEIGHT_JOB’, program_name => ‘LNE_PROGRAM’,

repeat_interval => ‘FREQ=SECONDLY;INTERVAL=30’, job_style => ‘LIGHTWEIGHT’,

comments => ‘Lightweight job exercise’); end;

/

4.Now run the job using dbms_scheduler.run_job.

begin dbms_scheduler.run_job(‘LNE_LIGHTWEIGHT_JOB’,TRUE);

end;

/

5.Finally, stop the job using dbms_scheduler.stop_job.

begin

dbms_scheduler.stop_job(job_name => ‘LNE_LIGHTWEIGHT_JOB’, force => TRUE);

end;

/

Using Job Chains

Chains are used to implement dependency scheduling. A chain consists of two or more Scheduler programs that are linked together to meet an objective. These multiple steps, when combined with dependency rules or conditions, create a chain or decision tree. Here’s an example:

NN

Run program A.

NN

If program A completes successfully, run program B.

 

592  Chapter 12  n  Using the Scheduler to Automate Tasks

NN

If both programs A and B complete successfully, run program C.

NN

If program A or B does not succeed, run program F.

 

NN

Run program ZZ.

Chains are useful for complex business transactions that require multiple dependent programs to complete successfully or take predefined steps when a step in the process fails. Financial reporting and a daily ETL load and report process are both examples of these kinds of business transactions.

A chain job is a type of scheduler job that references a job chain as the job action, can reference a chain instead of a program to start the process. Each step in the chain can be one of the following:

NN

A program

 

NN

Another chain

 

NN

An inline event or event schedule

When a chain job is running you can view its progress by querying the *_SCHEDULER_ RUNNING_JOBS, *_SCHEDULER_JOB_LOG, *_SCHEDULER_JOB_RUN_DETAILS, and *_SCHEDULER_ RUNNING_CHAINS views.

Creating a Chain

Create a chain by using the CREATE_CHAIN procedure, as follows:

BEGIN DBMS_SCHEDULER.CREATE_CHAIN (

chain_name => ‘lne_chain’, rule_set_name => NULL, evaluation_interval => NULL, comments => ‘Never break the chain’);

END;

/

Once you’ve created the chain, you’ll define the steps and rules.

Defining Chain Steps

Now that you’ve created a chain, you need to add steps to it. Remember that each step can point to a program, another chain, an inline event, or an event schedule. Here’s an example that adds three steps, each of which points to a specific program:

BEGIN DBMS_SCHEDULER.DEFINE_CHAIN_STEP (

chain_name => ‘lne_chain’,

Using Job Chains 

593

step_name => ‘lne_step1’, program_name => ‘start_lne’);

DBMS_SCHEDULER.DEFINE_CHAIN_STEP ( chain_name => ‘lne_chain’, step_name => ‘lne_step2’, program_name => ‘lne_run_stage1’);

DBMS_SCHEDULER.DEFINE_CHAIN_STEP ( chain_name => ‘lne_chain’, step_name => ‘lne_step3’, program_name => ‘lne_run_stage2’);

END;

/

It is not mandatory that the program exist when you define the chain step, but it must exist and be enabled before you execute the chain. If the program is an external executable, you must use the ALTER_CHAIN procedure to set the credentials for the step. If the program is a remote external executable, use ALTER_CHAIN to set the destination.

Defining a Chain That Waits for an Event

Use the DEFINE_CHAIN_EVENT_STEP procedure to define a step that waits for an event. In this example, we add a chain step to the previously defined lne_chain chain that will wait for a specific event to occur:

BEGIN DBMS_SCHEDULER.DEFINE_CHAIN_EVENT_STEP (

chain_name => ‘lne_chain’, step_name => ‘lne_step4’,

event_schedule_name => ‘lne_event_schedule’); END;

/

Adding Rules to a Chain

Chain rules define dependencies between steps and determine when steps run. A rule has a condition and an action. When a condition is evaluated true, the associated action is taken. The condition can contain a valid SQL WHERE clause or Scheduler chain condition syntax.

The Scheduler chain condition syntax takes one of the following two forms:

stepname [NOT] {SUCCEEDED|FAILED|STOPPED|COMPLETED}

stepname ERROR_CODE {comparision_operator|[NOT] IN} {integer|list_of_integers}

594  Chapter 12  n  Using the Scheduler to Automate Tasks

You can create complex conditions by using Boolean operators AND, OR, and NOT().

Step Attributes

When using the SQL WHERE clause to evaluate a condition, you can include the following step attributes: completed, state, start_date, end_date, error_code, and duration. When the state attribute is SUCCEEDED, FAILED, or STOPPED, the completed attribute is set to TRUE.

Conditions

Here are some examples of the chain condition syntax:

Credentials_confirm_step COMPLETED

Credentials_confirm_step SUCCEEDED

Credentials_confirm_step FAILED and

credentials_confirm_step ERROR_CODE != 21000

In the first example, the step completed, with one of the following conditions: STOPPED, FAILED, or SUCCEEDED. In the second example, the step must have succeeded for the condition to be met. In the third example, the step must have failed and the returned error code must not be equal to 21000.

Defining Rules

In the following example, the rule starts the chain at step 1 and on completion starts step 2:

BEGIN DBMS_SCHEDULER.DEFINE_CHAIN_RULE (

chain_name => ‘lne_chain’, condition => ‘TRUE’,

action => ‘START lne step1’, rule_name => ‘lne_rule1’, comments => ‘start the chain’);

DBMS_SCHEDULER.DEFINE_CHAIN_RULE ( chain_name => ‘lne_chain’, condition => ‘lne step1 completed’, action => ‘START lne step2’, rule_name => ‘lne_rule2’);

END;

/

Using Job Chains 

595

Starting and Ending the Chain

To start the chain, at least one rule must always evaluate to TRUE. The easiest way to do this is to simply set the condition to ‘1=1’ if you’re using SQL syntax or ‘TRUE’ if you using Scheduler chain condition syntax.

For the chain to end, at least one chain rule must have an action of ‘END’ when a condition evaluates to TRUE. If a chain has no more running steps and no END action has been determined to be TRUE, then the chain job will go into the CHAIN_STALLED state.

Enabling a Chain

Enabling a chain is straightforward:

BEGIN

DBMS_SCHEDULER.ENABLE (‘lne_chain’); END;

/

The chain must be enabled before a job can run it.

Creating Jobs for Chains

There are two ways you can run a chain: either by using the RUN_CHAIN procedure or, as in this example, by creating and scheduling a job of type CHAIN:

BEGIN DBMS_SCHEDULER.CREATE_JOB (

job_name => ‘lne_chain_job1’, job_type => ‘CHAIN’, job_action => ‘lne_chain’,

repeat_interval => ‘freq=daily;byhour=7;byminute=30;bysecond=0’, enabled => TRUE);

END;

/

The Scheduler creates a step job for each step of a chain job that is running. Each step job is uniquely identified by a job subname. To monitor the job steps, query the *_SCHEDULER_ RUNNING_JOBS, *_SCHEDULER_JOB_LOG, and *_SCHEDULER_JOB_RUN_DETAILS views.

In Exercise 12.3, you’ll create a job chain.

596  Chapter 12  n  Using the Scheduler to Automate Tasks

E x e r c i s e 12 . 3

Creating and Executing a Job Chain

For this exercise, you’ll create a job chain. Reuse components from the previous exercises when possible.

1.Create a simple chain, and set up the starting chain step as described in the section “Starting and Ending the Chain.”

BEGIN DBMS_SCHEDULER.CREATE_CHAIN (

chain_name => ‘LNE_CHAIN’, rule_set_name => NULL, evaluation_interval => NULL, comments => ‘Never break the chain’);

END;

/

2.Create chain steps for the stored procedures created earlier, and enable the job chain.

BEGIN DBMS_SCHEDULER.DEFINE_CHAIN_STEP (

chain_name => ‘lne_chain’, step_name => ‘lne_step1’,

program_name => ‘LNE_LIGHTWEIGHT_JOB’); DBMS_SCHEDULER.DEFINE_CHAIN_STEP (

chain_name => ‘lne_chain’, step_name => ‘lne_step2’, program_name => ‘LNE_JOB’);

END;

/

BEGIN DBMS_SCHEDULER.DEFINE_CHAIN_RULE (

chain_name => ‘LNE_CHAIN’, condition => ‘TRUE’,

action => ‘START LNE_STEP1’, rule_name => ‘LNE_RULE1’, comments => ‘start the chain’);

DBMS_SCHEDULER.DEFINE_CHAIN_RULE ( chain_name => ‘LNE_CHAIN’, condition => ‘LNE_STEP1 completed’,

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]