9/11

# 6 Create and Manage Jobs with APIs

The following sections describe how you can manage and create jobs with ConsolidatorManager APIs:

- Section 6.1, "Managing Scheduled Jobs Using ConsolidatorManager APIs"

- Section 6.2, "Start a Standalone Job Engine In Separate JVM"

- Section 6.3, "Using the ConsolidatorManager APIs to Create Jobs"

## 6.1 Managing Scheduled Jobs Using ConsolidatorManager APIs

Application developers can define, submit, and manage jobs programmatically based on a pre-determined time and interval. For example, jobs can be scheduled to run repeatedly for a specified duration on any specified day or days of the week or month. Administrators can schedule jobs to run repeatedly for a specified number of months, weeks or specified days of the month or week.

The Job Scheduler API schedules and executes jobs using a job engine. It is a generic component which enables apply and compose functions for MGP, device manager jobs, and custom jobs.

- Using the class `oracle.lite.sync.ConsolidatorManager`, application developers can register or de-register a job class, create, drop, enable or disable a job, search, and delete a job execution log.

- Use other supporting classes, such as `Job`, `Schedule`, `ExecutionResult` and `ExecutionLog` in the `oracle.lite.sync.job` package to manage your scheduled jobs.

For more information on these classes and their methods, refer to the Oracle Database Mobile Server JavaDoc.

## 6.2 Start a Standalone Job Engine In Separate JVM

If you want to execute a Standalone Job engine in a separate JVM from any of the mobile servers in the farm, then perform the following:

1. Retrieve a connection to the database with the Consolidator Manager `openConnection` method. Pass in the Mobile Manager administrator user name, password and optionally, the JDBC URL to the back-end Oracle database.

2. Create a new Job engine with the `JobEngine` class and start it with the `startUp` method. The Standalone Job engine executes in a separate thread, which you can terminate from the main thread.

3. Define how long the thread is to sleep between execution of all jobs.

4. Terminate the Standalone Job engine when you have completed all activities.

Note:

The following example demonstrates how to start up a Standalone Job engine in its own thread. It executes all of the jobs that have been scheduled either through the API or through the Mobile Manager Job Scheduler screens, because the Job Scheduler retrieves the scheduled job information from the repository.

    JobEngine JobEngine = new JobEngine();
    JobEngine.startUp();
    if (JobEngine.runnerThreadException != NULL){
      System.out.println("runnerThreadException:");
      JobEngine.runnerThreadException.printStackTrace();
    }

    Thread.currentThread().sleep(60*1000);

    if (JobEngine.runnerThreadException != NULL){
      System.out.println("runnerThreadException:");
      JobEngine.runnerThreadException.printStackTrace();
    }
    JobEngine.shutDown();

## 6.3 Using the ConsolidatorManager APIs to Create Jobs

Within the `oracle.lite.sync.ConsolidatorManager` class, there are several APIs, which are documented fully in the Oracle Database Mobile Server JavaDoc, that enable you to create, register, and schedule your job.

While these methods are described fully in the Oracle Database Mobile Server JavaDoc, the following demonstrates the order in which you would execute the methods:

1. Create your job class by implementing the `oracle.lite.job.Job` interface. Implement the Job interface methods, as follows:

   - `init` method—This method is invoked by the Job Scheduler when the job is loaded.

   - `execute` method—This method is invoked by the Job Scheduler when the job is scheduled to execute. Put a call into your application within this method. The Job Scheduler passes in the input parameter that was provided when the job is created—either with the `createJob` method or within the Mobile Manager Job Scheduler screen. When finished, the `execute` method returns an object of class type `ExecutionResult` containing whether the job was a success or failure.

   - `destroy` method—This method is invoked after the job completes.

2. After you have created your job class, register it with the `registerJobClass` method.

3. Create the job in the Job Scheduler by executing the `createJob` method. One of the input parameters is an object of class type `Schedule`, which defines when the job is executed. There are also other management methods that correspond to the Mobile Manager GUI, such as `dropJob`, `enableJob`, and `disableJob`.

4. If you want to retrieve any logs, execute the `getJobExecutionLogs` method, which retrieves objects of `ExecutionLog` class.