Using Dependency, Event-based, and Time-based Scheduling

This tutorial describes how to use the Oracle Scheduler to execute tasks based on:

Events such as the arrival of a file (event-based scheduling)
Complex schedules (time-based scheduling)

Approximately 1 hour

This tutorial covers the following topics:

Place the cursor over this icon to load and view all the screenshots for this tutorial. (Caution: This action loads all screenshots simultaneously, so response time may be slow depending on your Internet connection.)

Note: Alternatively, you can place the cursor over an individual icon in the following steps to load and view only the screenshot associated with that step. You can hide an individual screenshot by clicking it.

Oracle Database includes a feature rich job scheduler. You can schedule jobs to run at a designated date and time (such as every weeknight at 11:00pm), or upon the occurrence of a designated event (such as when inventory drops below a certain level). You can define custom calendars such as a fiscal year so you can have a schedule such as the last workday of every fiscal quarter. The Scheduler also includes chains, which are named groups of steps that work together to accomplish a task. Steps in the chain can be a program, subchain or an event, and you specify rules that determine when each step runs and what the dependencies between steps are.

In this tutorial you learn how to use the Scheduler to simplify the scheduling of complex tasks required by database administrators and application developers.

Back to Topic List

Before starting this tutorial, you should first complete the following steps:

1.
2.

Back to Topic List

Before a user can use the Oracle Scheduler, the user needs to have certain privileges granted. The user also needs access to certain packages for this tutorial. In this section you grant all the necessary privileges to the HR user. Perform the following steps:

1.

Open a browser and enter the following URL:

http://<your host name>:1158/em

Enter sys as the username, specify the password oracle, connect as SYSDBA, and then click Login.

Move your mouse over this icon to see the image

Note: If you are accessing Enterprise Manager for the first time, then scroll down to the bottom of the page and click I agree.

 

2.

Click on Administration tab.

Move your mouse over this icon to see the image

 

3.

Scroll down to Users and Privileges.

Move your mouse over this icon to see the image

 

4.

Under Users and Privileges, click Users.

Move your mouse over this icon to see the image

 

5.

Enter HR in the Object Name textbox and click Go.

Move your mouse over this icon to see the image

 

6.

Click Edit.

Move your mouse over this icon to see the image

 

7.

Click System Privileges.

Move your mouse over this icon to see the image

 

8.

Click Edit List.

Move your mouse over this icon to see the image

 

9.

Add the following privileges to the HR user. Select the following privileges from the Available System Privileges box. Click Move to move the privileges to Selected System Privileges box. Click OK.

CREATE EVALUATION CONTEXT
CREATE EXTERNAL JOB
CREATE JOB
CREATE PROCEDURE
CREATE RULE
CREATE RULE SET
SELECT ANY DICTIONARY

Move your mouse over this icon to see the image

 

10.

Click on Object Privileges.

Move your mouse over this icon to see the image

 

11.

Select Package from the Select Object Type drop down list and click Add.

Move your mouse over this icon to see the image

 

12.

Click the search light icon.

Move your mouse over this icon to see the image

 

13.

For Schema, select SYS from the drop down list and enter dbms_aq in Search Package Name. Click Go.

Move your mouse over this icon to see the image

 

14.

Select the DBMS_AQ and DBMS_AQADM check boxes and click Select.

Move your mouse over this icon to see the image

 

15.

In the Select Package Objects field after SYS.DBMS_AQADM, enter ",SYS.DBMS_LOCK", then select EXECUTE from the Available Privileges and move it to the Selected Privileges using the Move link. Click OK.

Move your mouse over this icon to see the image

 

16.

Click Apply.

Move your mouse over this icon to see the image

 

17.

Click Logout.

Move your mouse over this icon to see the image

 

Back to Topic List

Programs are used to provide a level of abstration. The action is specified in the program and not the job itself. The jobs then point to the program. If, for example, the shell script name or location changes, each job does not have to be changed, only one program. In this section, you create a program and two jobs that each point to the program by completing the following tasks:

Starting the OracleJobSchedulerORCL service
Creating a Program
Creating a Job that Uses the Program
Granting Access to Programs
Creating a Job that Resides in Another Schema that uses the Program

Back to Topic List

Starting the OracleJobSchedulerORCL Service

Perform the following steps:

1.

Open the window to view the Windows services. If you are using Windows XP, you follow this path:

Start > Control Panel > Administrative Tools > Services

 

2.

Find the OracleJobSchedulerORCL service in the list of services. If the service is disabled, right-click the service and choose Properties..

Move your mouse over this icon to see the image

 

3.

The executable path for this service should point to your Oracle Home directory, and may differ from the directory shown here.

Change the Startup Type for the service to Manual.

Move your mouse over this icon to see the image

 

4.

Click Apply.

Move your mouse over this icon to see the image

 

5.

Click Start to start this service.

Move your mouse over this icon to see the image

 

6.

After the service has started, click OK.

Move your mouse over this icon to see the image

 

7.

Close the Services window and the Administrative Tools window.

Move your mouse over this icon to see the image

 

Back to List

Creating a Program

In this step you create a program that calls a script named test1.bat. This script copies the contents of one file into another file.

Perform the following steps:

1.

Login to Enterprise Manager as hr. Click Login.

Move your mouse over this icon to see the image

 

2.

Enter hr as the username, hr as your password, and then click Login.

Move your mouse over this icon to see the image

Note: If you are accessing Enterprise Manager for the first time, then scroll down to the bottom of the page and click I agree.

 

3.

Click Administration tab.

Move your mouse over this icon to see the image

 

4.

Under Database Scheduler, select Programs.

Move your mouse over this icon to see the image

 

5.

Click Create.

Move your mouse over this icon to see the image

 

6.

Enter the following information, then click Add Another Row.

Name: progexe
Schema: HR
Enabled: Yes
Description: This is a program that runs an external executable
Type: EXECUTABLE
Executable Name: C:\WINDOWS\system32\cmd.exe

Move your mouse over this icon to see the image

 

7.

Enter the following argument information, then click Add Another Row. This argument instructs the cmd.exe program to exit when the batch script is finished.

Name: arg1
Default: /q

Move your mouse over this icon to see the image

 

8.

Enter the following argument information, then click Add Another Row. This argument turns off echoing for the cmd.exe program.

Name: arg2
Default: /c

Move your mouse over this icon to see the image

 

9.

Enter the following argument information, then click OK. This is the batch script that we want to execute.

Name: script_loc
Default: C:\wkdir\test1.bat

Move your mouse over this icon to see the image

 

10.

Your program has been created. Click the Database instance breadcrumb.

Move your mouse over this icon to see the image

 

Back to List

Creating a Job that Uses the Program

Now you create a job that uses the program you just created. Perform the following steps:

1.

On the Administration page under Database Scheduler, select Jobs.

Move your mouse over this icon to see the image

 

2.

Click Create.

Move your mouse over this icon to see the image

 

3.

Enter the following information, then click Change Command Type.

Name: PROGEXE_JOB
Schema: HR
Enabled: Yes
Description: This job uses the program HR.PROGEXE
Logging Level: Log job runs only (RUNS)
Job Class:DEFAULT_JOB_CLASS
Auto Drop:FALSE
Restartable:TRUE

Move your mouse over this icon to see the image

 

4.

Select Program Name and click the search light.

Move your mouse over this icon to see the image

 

5.

Select HR.PROGEXE from the list and click Select.

Move your mouse over this icon to see the image

 

6.

Click OK.

Move your mouse over this icon to see the image

 

7.

Click OK to submit the job.

Move your mouse over this icon to see the image

 

8.

Click the Running tab.

Move your mouse over this icon to see the image

 

9.

Your job is now running. You may need to click Refresh a couple of times until the job no longer appears in the list of running jobs. Then click the History tab.

Move your mouse over this icon to see the image

 

10.

The job has run successfully. The next section requires you to be logged in as a super user. Click Logout.

Move your mouse over this icon to see the image

 

Back to List

Granting Access to Programs

In order for another user to use the program, the user who created the program or the super user needs to share it. Perform the following steps:

1.

Click Login.

Move your mouse over this icon to see the image

 

2.

Login as sys/oracle as SYSDBA and click Login.

Move your mouse over this icon to see the image

Note: The user HR can grant the user OE access to the program progexe. You are logging in as SYS because you want to grant the user OE the CREATE EXTERNAL JOB, CREATE JOB and SELECT ANY DICTIONARY privileges at the same time and only a super user can perform that task.

 

3.

Click Administration tab.

Move your mouse over this icon to see the image

 

4.

Scroll down to Users and Privileges.

Move your mouse over this icon to see the image

 

5.

Click Users.

Move your mouse over this icon to see the image

 

6.

Enter OE and click Go.

Move your mouse over this icon to see the image

 

7.

Select OE and click Edit.

Move your mouse over this icon to see the image

 

8.

Click System Privileges tab.

Move your mouse over this icon to see the image

 

9.

You need to grant additional privileges to this user. Click Edit List.

Move your mouse over this icon to see the image

 

10.

While holding down the Ctrl key, select CREATE EXTERNAL JOB, CREATE JOB and SELECT ANY DICTIONARY from the list of Available System Privileges. Release the Ctrl key and click Move. Then click OK.

Move your mouse over this icon to see the image

 

11.

Now you need to grant EXECUTE privileges on progexe program to the OE user. Click Object Privileges.

Move your mouse over this icon to see the image

 

12.

Select Programs from the Select Object Type drop down list and click Add.

Move your mouse over this icon to see the image

 

13.

Select the Search light next to the Select Package Objects box.

Move your mouse over this icon to see the image

 

14.

Select HR from the Schema drop down list box and click Go.

Move your mouse over this icon to see the image

 

15.

Select PROGEXE and click Select.

Move your mouse over this icon to see the image

 

16.

Select EXECUTE from the list of Available Privileges and click Move. Then click OK.

Move your mouse over this icon to see the image

 

17.

Select Package from the Select Object Type drop down list and click Add.

Move your mouse over this icon to see the image

 

18.

Enter SYS.DBMS_LOCK in the Select Package Objects box, select Execute from the list of privileges and click Move. Then click OK.

Move your mouse over this icon to see the image

 

19.

Click Apply.

Move your mouse over this icon to see the image

 

20.

The user OE has been updated. You now need to login as OE. Click Logout.

Move your mouse over this icon to see the image

 

Back to List

Creating a Job that Resides in Another Schema that uses the Program

Now that OE has access to the program, you can create and run a job that uses it. Perform the following steps:

1.

Click Login.

Move your mouse over this icon to see the image

 

2.

Login as oe/oe and click Login.

Move your mouse over this icon to see the image

Note: If you are accessing Enterprise Manager for the first time as this user, then scroll down to the bottom of the page and click I agree.

 

3.

Click Administration tab.

Move your mouse over this icon to see the image

 

4.

Under Database Scheduler, click Jobs.

Move your mouse over this icon to see the image

 

5.

Click Create.

Move your mouse over this icon to see the image

 

6.

Enter the following information, then click Change Command Type.

Name: PROGEXE_JOB2
Schema: OE
Enabled: Yes
Description: This job uses the program HR.PROGEXE
Logging Level: Log job runs only (RUNS)
Job Class:DEFAULT_JOB_CLASS
Auto Drop:FALSE
Restartable:TRUE

Move your mouse over this icon to see the image

 

7.

Select Program Name and click the search light.

Move your mouse over this icon to see the image

 

8.

Select HR.PROGEXE from the list and click Select.

Move your mouse over this icon to see the image

 

9.

Click OK.

Move your mouse over this icon to see the image

 

10.

Click OK.

Move your mouse over this icon to see the image

 

11.

Your job has been submitted and is running. Click the History tab.

Move your mouse over this icon to see the image

 

12.

The job has completed successfully. Select Logout.

Move your mouse over this icon to see the image

Back to List

Dependency Scheduling: Creating a Simple Chain

A chain is a grouping of programs that are linked together for a single, combined objective. An example of a chain might be "run program A and then program B, but only run program C if programs A and B complete successfully, otherwise run program D."

Each position within a chain of interdependent programs is referred to as a step. Typically, after an initial set of chain steps has started, the execution of successive steps depends on the completion of one or more previous steps. Each step can point to one of the following Scheduler objects:

A program
Another chain (a nested chain)

An event

In this section, you create a simple chain where jobs are launched based on the outcome of a previous task. Perform the following steps:

1.

Login to Enterprise Manager as hr. Click Login.

Move your mouse over this icon to see the image

 

2.

Enter hr as the username, hr as your password, and then click Login.

Move your mouse over this icon to see the image

 

3.

Click Administration tab.

Move your mouse over this icon to see the image

 

4.

Under Database Scheduler, select Chains.

Move your mouse over this icon to see the image

 

5.

Click Create.

Move your mouse over this icon to see the image

 

6.

After entering the following information for the Job Chain:

Name: Simplechain
Schema: HR
Enabled: Yes
Description: A simple chain

Under Steps, enter Step1 for the Step name in the first row. Leave the Type as Program, then click the search light next to the Object Name field.

Move your mouse over this icon to see the image

 

7. Select the program HR.PROGEXE and click Select.

Move your mouse over this icon to see the image


8.

In the second row in the Steps section, enter the following information. Then click Create in the Rules section.

Step Name: Step2
Object Name: HR.PROGEXE

Move your mouse over this icon to see the image

 

9.

Create a rule to start the chain. Enter the following information, and click Continue.

Condition: true
Action: start step1

Move your mouse over this icon to see the image

 

10.

Scroll down and Click Create in the Rules section again.

Move your mouse over this icon to see the image

 

11.

In order to view the chain while it is running, delay the start of the second step by two and a half minutes. Enter the following information, and click Continue.

Condition: step1 succeeded
Action: after 00:02:30 start step2

Move your mouse over this icon to see the image

 

12.

Now create the rule that defines the condition to end the chain. Scroll down to the Rules section and click Create.

Move your mouse over this icon to see the image

 

13.

Enter the following information, and click Continue.

Condition: step2 completed
Action: end

Move your mouse over this icon to see the image

 

14.

Scroll down to see the steps and rules in your simple chain. Then click OK.

Move your mouse over this icon to see the image

 

15.

Your simple chain is created.

Move your mouse over this icon to see the image

Back to Topic List

You are now ready to schedule your chain by creating a job that points to it. Perform the following steps:

1.

Click Create Job Using Chain.

Move your mouse over this icon to see the image

 

2.

Enter the following information and click OK.

Name: Chainjob
Description: Job using a Simple Chain

Move your mouse over this icon to see the image

 

3.

Chainjob has been created successfully. Click the Running tab to see the status of the job.

Move your mouse over this icon to see the image

 

4.

Click on Status icon to see the status of the steps running.

Move your mouse over this icon to see the image

 

5.

You see the state of each step in the chain. Click the Scheduler Jobs breadcrumb.

Move your mouse over this icon to see the image

 

6.

Click the All tab.

Move your mouse over this icon to see the image

 

7.

Click Refresh until you see that the job has successfully completed.

Move your mouse over this icon to see the image

 

8.

At the bottom of the page under Related Links, click Chains.

Move your mouse over this icon to see the image

 

Back to Topic List

In this section, you create a more complex chain that consists of multiple steps and more complex dependencies. The following diagram shows the complex chain:

Perform the following steps:

1.

Create two programs to be used by the chain. Start SQL*Plus for Windows by clicking Start, selecting All Programs, selecting your Oracle Home installation, the Application Development group of programs, and then selecting SQL*Plus.

Enter hr for the User Name and Password, then click OK.

Move your mouse over this icon to see the image

 

2.

After you are connected, run the prog1 SQL script, which is located in the wkdir directory.

@C:\wkdir\prog1

This script contains the following SQL code:

BEGIN
DBMS_SCHEDULER.CREATE_PROGRAM(
program_name=>'prog1',
program_type=>'PLSQL_BLOCK',
program_action=>'begin dbms_lock.sleep(90); end;',
enabled=>TRUE);
END;
/

Move your mouse over this icon to see the image

 

3.

Now execute the following script to create the second program.

@C:\wkdir\prog2
BEGIN
 DBMS_SCHEDULER.CREATE_PROGRAM(
 program_name=>'prog2',
 program_action=>'DECLARE
 num_tables NUMBER;
BEGIN
 SELECT COUNT(*) INTO num_tables FROM USER_TABLES;
 IF num_tables < 1000 THEN
   raise_application_error(-20100, ''Expecting at least 1000 tables'');
 ELSE
   NULL;
 END IF;
 END;',
 program_type=>'PLSQL_BLOCK',
 number_of_arguments=>0,
 comments=>'',
 enabled=>TRUE);
END;

Move your mouse over this icon to see the image

 

4.

Minimize the SQL*Plus window, but do not close it. Switch to the Enterprise Manager window. In the Scheduler Chains screen, click Create.

Move your mouse over this icon to see the image

 

5.

Entering the following information under the Create Chain section:

Name: complexchain
Schema: HR
Enabled: Yes
Description: Create a complex chain

Under the steps section, enter the following information and then click Add 5 Steps.

  Step Name Type Object Name
Row 1 Step1 Program HR.PROG1
Row 2 Step2 Program HR.PROGEXE
Row 3 Step3 Program HR.PROG2
Row 4 Step4 Program HR.PROG1
Row 5 Step5 Program HR.PROG1

Move your mouse over this icon to see the image

 

6.

In the sixth row, enter the following information.

Step Name: Step6
Type: Program
Object Name: HR.PROG1

Then scroll down to the Rules section and click Create.

Move your mouse over this icon to see the image

 

7.

Create a rule to start the chain. Enter the following information, and click Continue

Condition: true
Action: start step1

Move your mouse over this icon to see the image

 

8.

In the Rules section, click Create again.

Move your mouse over this icon to see the image

 

9.

Enter the following information, and click Continue

Condition: step1 succeeded
Action: start step2, step3

Move your mouse over this icon to see the image

 

10.

In the Rules section, click Create again.

Move your mouse over this icon to see the image

 

11.

Enter the following information, and click Continue.

Condition: step3 error_code=20100
Action: start step5

Move your mouse over this icon to see the image

 

12.

In the Rules section, click Create again.

Move your mouse over this icon to see the image

 

13.

Enter the following information, and click Continue.

Condition: step2 succeeded
Action: after 00:00:30 start step4

Move your mouse over this icon to see the image

 

14.

In the Rules section, click Create again.

Move your mouse over this icon to see the image


15.

Enter the following information, and click Continue.

Condition: step4 completed
Action: start step6

Move your mouse over this icon to see the image

 

16.

In the Rules section, click Create again.

Move your mouse over this icon to see the image

 

17.

Enter the following information, and click Continue.

Condition: (step5 completed and step6 completed) or step1 failed
Action: end

Move your mouse over this icon to see the image

 

18.

You have now created steps and rules for the complex chain. Click OK.

Move your mouse over this icon to see the image

 

19.

Your complex chain has been created successfully.

Move your mouse over this icon to see the image

 

Back to Topic List

Modifying and Scheduling a Complex Chain

In this section, you modify the chain you created in the previous section to add another step and then you schedule the chain. The following diagram shows what the chain will look like after the modification.

To modify the chain, perform the following steps:

1.

Select Complexchain and click Edit.

Move your mouse over this icon to see the image

 

2.

Enter the following information to add another step, then scroll down to the Rules section.

Step Name: Step7
Type: SUBCHAIN
Object Name: hr.simplechain

Move your mouse over this icon to see the image

 

3.

Click Create.

Move your mouse over this icon to see the image

 

4.

Enter the following information, and click Continue.

Condition: step2 succeeded
Action: start step7

Move your mouse over this icon to see the image

 

5.

Now you want to modify the rule that has the condition " step4 completed". Scroll down to the Rules section, select this rule, and click Edit.

Move your mouse over this icon to see the image

 

6.

Change the condition to step4 completed and step7 completed. Click Continue.

Move your mouse over this icon to see the image

 

7.

Scroll down to verify the new rule condition, then click Apply.

Move your mouse over this icon to see the image

 

8.

Your complex chain is modified successfully. Now you can schedule your chain. Click the Scheduler Chains breadcrumb.

Move your mouse over this icon to see the image

 

9.

Select COMPLEXCHAIN and click Create Job Using Chain.

Move your mouse over this icon to see the image

 

10.

Enter complexchainjob for the Name and enter Job using a complex chain in the description. Then click OK.

Move your mouse over this icon to see the image

 

11.

The complexchainjob job has been created successfully. Click the Running tab to see the status of the job.

Move your mouse over this icon to see the image

 

12.

Click on Status icon to see the status of the steps running.

Move your mouse over this icon to see the image

 

13.

Depending on when the page refreshes, you might see the status of the steps change. Click Reload in your browser to refresh the list. When all of the steps have executed, click the Scheduler Jobs breadcrumb.

Move your mouse over this icon to see the image

 

14.

Click the All tab.

Move your mouse over this icon to see the image

 

15.

Click Refresh.

Move your mouse over this icon to see the image

 

16.

The complexchainjob job ran successfully. Click the Database Instance breadcrumb.

Move your mouse over this icon to see the image

 

Back to Topic List

Event-based scheduling triggers jobs based on real-time events. Events are defined as any state changes or occurrences in the system such as the arrival of a file.

In this section you modify the Chainjob, created previously, to be executed when a file arrives on the file system. When file1 exists in your working directory, the job executes. In this tutorial, you create a queue where a message is enqueued when file1 arrives. When the message is enqueued the Scheduler starts the execution of the Chainjob job. In this case, you create a typed queue, which means you must first create the user-defined type, then create the queue based on that user-defined type. Queues are stored in the database using tables so you must also create a queue table. Perform the following tasks:

Create a Directory Object
Create an Object Type
Create a Typed Event Queue
Create a Job with an Event Schedule

Back to Topic List

Create a Directory Object

Create a directory to specify the location where files are placed when they arrive on the file system.

1.

Restore the SQL*Plus window, then run the following commands to add a directory.

CONNECT / AS SYSDBA
@C:\wkdir\add_dir.sql
create or replace directory file_arrival_directory as 'C:\wkdir';
grant read on directory file_arrival_directory to HR;
Move your mouse over this icon to see the image

 

Back to Topic

Create an Object Type

Create an object type that is used for identification of the arriving files.

1.

Switch back to Enterprise Manager. From the Administration tab, scroll down to the User Defined Types section and select Object Types.

Move your mouse over this icon to see the image

 

2.

Click Create.

Move your mouse over this icon to see the image

 

3.

Enter mytype for the name and under Attributes, click Add.

Move your mouse over this icon to see the image

 

4.

Enter file_name for the Name, select VARCHAR2 for the Type, and enter 80 for the Length. Then click OK.

Move your mouse over this icon to see the image

 

5.

Click OK to create the Object Type.

Move your mouse over this icon to see the image

 

6.

Your Object Type was created. Click your Database Instance breadcrumb.

Move your mouse over this icon to see the image

 

Back to Topic

Create a Typed Event Queue

Create a queue to store the file arrival events.

1.

Click the Maintenance tab.

Move your mouse over this icon to see the image

 

2.

Under Streams, Click Setup.

Move your mouse over this icon to see the image

 

3.

Click Messaging.

Move your mouse over this icon to see the image

 

4.

Click Create.

Move your mouse over this icon to see the image

 

5.

Select Normal Queue, Fixed Datatype and click Continue.

Move your mouse over this icon to see the image

 

6.

Enter event_queue for the Name and click Create New for the Queue Table.

Move your mouse over this icon to see the image

 

7.

Enter event_queue_table for the Name, under Payload Type, select Object Type and click the search light for Type.

Move your mouse over this icon to see the image

 

8.

Enter hr for Schema and click Go.

Move your mouse over this icon to see the image

 

9.

Select HR.MYTYPE and click Select.

Move your mouse over this icon to see the image

 

10.

Click OK.

Move your mouse over this icon to see the image

 

11.

Click Finish to create the messaging queue.

Move your mouse over this icon to see the image

 

12.

The queue was created. Click the Database Instance breadcrumb..

Move your mouse over this icon to see the image

 

Back to Topic

Create a Job with an Event Schedule

You first create a job that detects when a file arrives on the system. When the file arrives, the job enqueues a message in the queue you created in the previous section. Then modify Chainjob to have an event schedule. Finally you create file1 in your working directory to simulate the arrival of a file on the system.

1.

Click the Administration tab.

Move your mouse over this icon to see the image

 

2.

Click Jobs .

Move your mouse over this icon to see the image

 

3.

Click Create.

Move your mouse over this icon to see the image

 

4.

Enter file_detect for the Name and click Yes for Enabled. Copy the following script into the PL/SQL area, then click OK.

Declare
   FileDescriptor utl_file.file_type;
   my_msgid RAW(16);
   props dbms_aq.message_properties_t;
   enqopts dbms_aq.enqueue_options_t;
begin
LOOP
   BEGIN
   FileDescriptor := utl_file.fopen('FILE_ARRIVAL_DIRECTORY', 'file1', 'r');
 EXIT;
   EXCEPTION WHEN utl_file.INVALID_OPERATION THEN dbms_lock.sleep(1);
 END;
   END LOOP;
dbms_aq.enqueue(' event_queue', enqopts, props,
  MYTYPE('file1'), my_msgid);
  commit;
 end;       

Move your mouse over this icon to see the image

 

5.

Click Refresh until the job status shows 'RUNNING'.

Move your mouse over this icon to see the image

 

6.

Now change the schedule of Chainjob to be an event schedule. Select Chainjob and click Edit Job Definition.

Move your mouse over this icon to see the image

 

7.

Click Yes for enabled and then select the Schedule tab.

Move your mouse over this icon to see the image

 

8.

For Schedule Type, select Event, then click Change Queue.

Move your mouse over this icon to see the image

 

9.

Select HR.EVENT_QUEUE and click Select.

Move your mouse over this icon to see the image

 

10.

Specify a condtion that checks if an object in the queue has a value of 'file1' for the file_name attribute. To do this, enter tab.user_data.file_name=''file1'' in the Condition field, then click Apply.

Note: The word file1 has two single quotes on each end, not a single double quote character.

Move your mouse over this icon to see the image

 

11.

Chainjob has been successfully modified. Click the Scheduler Jobs breadcrumb.

Move your mouse over this icon to see the image

 

12.

Click Refresh. You now see that the Chainjob job status changes from SUCCEEDED to SCHEDULED.

Move your mouse over this icon to see the image

 

13.

The job does not execute until an event is placed in the HR.EVENT_QUEUE. To trigger the job execution, create a file named file1 in the C:\wkdir directory.

Open a command prompt window by clicking the Start button then selecting Run... In the Run window type cmd in the Open field then click OK.

Move your mouse over this icon to see the image

 

14.

Execute the following commands:

C:
cd wkdir
copy t1.txt file1

Move your mouse over this icon to see the image

15.

Close the Run window, then switch back to Enterprise Manager. Click the Running tab.

Move your mouse over this icon to see the image

 

16.

Depend upon the speed of your system, EM displays either the file_detect job or the Chainjob in this window. Click Refresh until no more jobs are listed.

Move your mouse over this icon to see the image

 

17.

The job is finished. Click the History tab.

Move your mouse over this icon to see the image

 

18.

The file_detect and Chainjob jobs completed successfully. Click the Database Instance breadcrumb.

Move your mouse over this icon to see the image

 

Back to Topic

In this section you create complex schedules by combining multiple schedules and using date filtering clauses.

1.

Restore the SQL*Plus window and run the following script to create schedules that represent each of the major holidays celebrated in the United States.

connect hr/hr
@C:\wkdir\holiday_schedule.sql
This script performs the following actions:

--Create US major holidays
 begin
   dbms_scheduler.create_schedule('Newyear', null,
   'FREQ=DAILY;BYDATE=0101', null, 'New Year');
end;
/
begin
   dbms_scheduler.create_schedule('MartinLutherKing', null,
   'FREQ=MONTHLY;BYMONTH=JAN;BYDAY=3MON',
   null, 'Martin Luther King Day');
end;
/
begin
   dbms_scheduler.create_schedule('PresidentDay', null,
   'FREQ=MONTHLY;BYMONTH=FEB;BYDAY=3MON',
   null, 'President Day');
end;
/
begin
   dbms_scheduler.create_schedule('MemorialDay', null,
   'FREQ=MONTHLY;BYMONTH=MAY;BYDAY=-1MON',
   null, 'Memorial Day');
end;
/
begin
   dbms_scheduler.create_schedule('Independence', null,
   'FREQ=DAILY;BYDATE=0704',
   null, 'Independence Day');
end;
/
begin
   dbms_scheduler.create_schedule('LaborDay', null,
   'FREQ=MONTHLY;BYMONTH=SEP;BYDAY=1MON',
   null, 'Labor Day');
end;
/
begin
   dbms_scheduler.create_schedule('ColumbusDay', null,
   'FREQ=MONTHLY;BYMONTH=OCT;BYDAY=2MON',
   null, 'Columbus Day');
end;
/
begin
   dbms_scheduler.create_schedule
   ('VeteransDay', null, 'FREQ=MONTHLY;BYMONTH=NOV;BYMONTHDAY=11',
   null, 'Veterans Day');
end;
/
       
begin
   dbms_scheduler.create_schedule
    ('Thanksgiving', null, 'FREQ=MONTHLY;BYMONTH=NOV;BYDAY=4THU',
     null, 'ThanksGiving');
end;
/
begin
   dbms_scheduler.create_schedule
   ('Xmas', null, 'FREQ=DAILY;BYDATE=1225', null, 'Christmas');
end;
/
Move your mouse over this icon to see the image

 

2.

In this step you create the following schedules:

Holidays:           Combines all the schedules for holiday dates into one schedule
Weekends:       A schedule of all weekend days
Weekdays:       A schedule that consists of only the days Monday through Friday
ClosedDays:     A schedule that combines weekend days with holidays
WorkDays:        A schedule of all weekdays, excluding holidays
LastWorkDay: The last work day of the month, excluding holidays

In the SQL*Plus window, run the following script:

@C:\wkdir\composite_schedule.sql
This script performs the following actions:
begin
   dbms_scheduler.create_schedule('Holidays', null,
   'Newyear,MartinLutherKing, PresidentDay,
   MemorialDay, Independence, LaborDay, ColumbusDay,
   VeteransDay, Thanksgiving, Xmas');
end;
/

-- Create Weekends schedule
begin
   dbms_scheduler.create_schedule 
   ('weekends',repeat_interval=> FREQ=DAILY;BYDAY=SAT,SUN');
end;
/
-- Create weekdays schedule
begin
   dbms_scheduler.create_schedule ('weekdays',
   repeat_interval=> 'FREQ=DAILY;BYDAY=MON,TUE,WED,THU,FRI');
end;
/
-- Weekend and holiday schedule
begin
   dbms_scheduler.create_schedule ('ClosedDays',
   repeat_interval=> 'FREQ=DAILY;BYDAY=SAT,SUN;INCLUDE=holidays');
end;
/
    
-- Working Days
begin
   dbms_scheduler.create_schedule
    ('WorkDays', null,'WEEKDAYS; exclude= HOLIDAYS');
end;
/
    
-- Last work day of the month
begin
   dbms_scheduler.create_schedule
    ('LastWorkDay',null,'FREQ=MONTHLY;INCLUDE=WEEKDAYS;
     EXCLUDE=HOLIDAYS; BYSETPOS=-1');
end;
/

Move your mouse over this icon to see the image

 

3.

In this step you create a schedule that defines the fiscal quarters of a year (a user-defined frequency). You then use this schedule to create a schedule for the last working day in each quarter.

In the SQL*Plus window, run the following script:

@C:\wkdir\user_defined_freq.sql
This script performs the following actions:
-- Oracle's Fiscal year
begin
  dbms_scheduler.create_schedule(schedule_name => 'FISCALYEAR',
   start_date => to_timestamp_tz('01-JUN-2005 12:00:00','DD-MON-YYYYHH24:MI:SS'),
   repeat_interval => 'freq=yearly;bydate=0301,0601,0901,1201;periods=4');
end;
 /
-- Last workday in each quarter
begin
   dbms_scheduler.create_schedule
    ('LastQtrWorkDay',null,'FREQ=FISCALYEAR;INCLUDE=WorkDays;bysetpos=-1');
 end;
 /

Move your mouse over this icon to see the image

 

4.

Now you can review the schedules you just created by using Enterprise Manager. On the Administration page, under the heading Database Scheduler, click Schedules.

Move your mouse over this icon to see the image

 

5.

You see the schedules created by the scripts. Logout of Enterprise Manager, and then you can close the SQL*Plus window.

Move your mouse over this icon to see the image

 

Back to Topic List

In this tutorial, you learned how to:

Create jobs using programs
Create, modify and schedule chains (dependency scheduling)
Execute jobs based on events (event-based scheduling)
Create advanced time-based schedules

Back to Topic List