How to schedule a work to run at particular time u

2019-07-14 13:57发布

问题:

Im not clear about this, here in DBMS_SCHEDULER we have CREATE_PROGRAM CREATE_JOB CREATE_SCHEDULE etc., after reading the oracle doc still im unclear what to use, On the Oracle side, i am going to use DBMS_SCHEDULER to insert a new message into the queue at the appropriate time, i planned to create scheduler to execute it on particular time and then create program to execute my PL/SQL block which will enqueue the message in the queue Or instead of using CREATE_SCHEDULE and CREATE_PROGRAM, CREATE_JOB does both the jobs, which to use? please guide me whether i am doing correctly, if not please correct me.

Thankyou

回答1:

create_job is the basic call to schedule a call. you don't have to create a named program or schedule to do this. where creating a named program/schedule is useful, is if you have several jobs that want to use this call. you can just reference the named program schedule instead of having each job hold a copy of that.

e.g. if you had 5 jobs wanting to call your package MYPKG.ENTRY_PROG(param) and each job just used a different parameter value, i'd say you want to use create_program to define that pl/sql call and then create_job to reference that program name + set the parameter value of choice. that way, if you want to rename the API later or something, you don't have to change five seperate jobs to do this.

If your job is just a standalone job that calls a routine that won't be called by other jobs, then you don't have to use create_program/create_schedule, just use create_job directly.

one example where I used create_program was to call a test harness. my test harness package is called pkg_test_harness.queue_tests(p_set_name in varchar2) so I have a few jobs defined that enqueues various APIs to be run at 9AM, 12PM and 5PM. instead of defining each jobs call separately i just called create_program like:

    dbms_output.put('Setting up TEST_HARNESS_ENQUEUE scheduler program...');
    dbms_scheduler.create_program(program_name        => 'TEST_HARNESS_ENQUEUE',
                                  program_type        => 'STORED_PROCEDURE',                                                          
                                  program_action      => 'pkg_test_harness.queue_tests', 
                                  number_of_arguments => 1,
                                  enabled             => false,
                                  comments            => 'Program to enqueue a set of API test for the test harness to run.');

    dbms_scheduler.define_program_argument(program_name      => 'TEST_HARNESS_ENQUEUE',
                                           argument_name     => 'p_set_name',
                                           argument_position => 1,
                                           argument_type     => 'VARCHAR2',
                                           default_value     => '');

    dbms_scheduler.enable (name => 'TEST_HARNESS_ENQUEUE');

    dbms_output.put_line('done.');

and then each "job" was defined pointing to the program.

dbms_output.put('Setting up TEST_HARNESS_ENQUEUE_9AM scheduler job...');
dbms_scheduler.create_job(job_name        => 'TEST_HARNESS_ENQUEUE_9AM',
                          program_name    => 'TEST_HARNESS_ENQUEUE',
                          start_date      => systimestamp,
                          end_date        => null,
                          repeat_interval => 'freq=daily; byhour=9; byminute=0; bysecond=0;',
                          enabled         => true,
                          auto_drop       => false,
                          comments        => 'Job to enqueue a set of API test for the test harness to run.');

dbms_scheduler.set_job_argument_value(job_name          => 'TEST_HARNESS_ENQUEUE_9AM',
                                      argument_position => 1,
                                      argument_value    => 'DAILY_9AM');
dbms_output.put_line('done.');

dbms_output.put('Setting up TEST_HARNESS_ENQUEUE_12PM scheduler job...');
dbms_scheduler.create_job(job_name        => 'TEST_HARNESS_ENQUEUE_12PM',
                          program_name    => 'TEST_HARNESS_ENQUEUE',
                          start_date      => systimestamp,
                          end_date        => null,
                          repeat_interval => 'freq=daily; byhour=12; byminute=0; bysecond=0;',
                          enabled         => true,
                          auto_drop       => false,
                          comments        => 'Job to enqueue a set of API test for the test harness to run.');

i didn't create a named schedule, as these schedules are unique to the individual job.