Show TOC Start of Content Area

Procedure documentation Creating a Scheduler Task for the HelloJob  Locate the document in its SAP Library structure

Use

This procedure tells you how to create a scheduler task for the HelloJob definition that instructs the Java Scheduler to run a HelloJob instance at 8 AM on the April 1, 2007.

Prerequisites

The HelloJob definition is deployed on the server and that the NetWeaver Scheduler for Java is running.

For the reference documentation of the Scheduler API, see the JavaDoc at http://help.sap.com/javadocs/nwce/current/sc/index.html and refer to the com.sap.scheduler.apipackage.

Procedure

...

       1.      Get a JNDI reference to the scheduler. See Getting a JNDI Reference to the Scheduler API

       2.      Create a SchedulerTask object as the sample below shows.

You have to create a Calendar instance that you pass to a SchedulerTime object. You create a RecurringEntryobject and pass the SchedulerTime instance to it. In this way, you provide the Java Scheduler with an immutable reference to time.

In the scheduler task, you provide an input value for the UserName job parameter with direction IN that you set in the job definition.

When you create a SchedulerTask instance, you have to create its ID by invoking the SchedulerTaskID.newID() method provided by the Scheduler API.

You schedule the task by invoking the schedule()method.

package com.sap.scheduler.examples.hellojob;

import java.util.TimeZone;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import com.sap.scheduler.api.CronEntry;

import com.sap.scheduler.api.RecurringEntry;

import com.sap.scheduler.api.Scheduler;

import com.sap.scheduler.api.SchedulerTask;

import com.sap.scheduler.api.SchedulerTaskID;

import com.sap.scheduler.api.SchedulerTime;

import com.sap.scheduler.runtime.JobDefinition;

import com.sap.scheduler.runtime.JobParameter;

import com.sap.scheduler.runtime.JobParameterDefinition;

 

public class MyApplication {

   

    public void doSomething() {

        //Obtain a JNDI reference to the scheduler

        Scheduler scheduler;

        try {

            InitialContext ctx = new InitialContext();

            scheduler = (Scheduler)ctx.lookup("scheduler");

        } catch (NamingException ne) {

            return;

        }

//Get the HelloJob definition by name

//as defined in the job-definition.xml

JobDefinition helloJobDef = scheduler.getJobDefinitionByName("HelloJob");

//Get the job's IN parameter by name

JobParameterDefinition def = helloJobDef.getJobDefinitionParameter("UserName");

//Specify the input for the IN parameter       

JobParameter param = new JobParameter(def, "John");

//Create Calendar instance for SchedulerTime

   //Set date and time to 8 AM on April 1, 2007

   java.util.Calendar calendar = java.util.Calendar.getInstance();

   calendar.set(Calendar.YEAR, 2007);

   calendar.set(Calendar.MONTH, 3);

   calendar.set(Calendar.DAY_OF_MONTH, 1);

   calendar.set(Calendar.HOUR_OF_DAY, 8);

   calendar.set(Calendar.MINUTE, 0);

   java.util.Date date = calendar.getDate();

//Create SchedulerTime and pass to it the Calendar instance

SchedulerTime time = new SchedulerTime(date, TimeZone.getDefault());

//Create RecurringEntry and pass to it SchedulerTime instance

RecurringEntry re = new RecurringEntry(time);

//Create a SchedulerTask for the HelloJob

SchedulerTask task = new SchedulerTask(

   SchedulerTaskID.newID(),

   helloWorldJobDef.getJobDefinitionId(),

    new JobParameter[] {param},

    new RecurringEntry[] {re},

    new CronEntry[] {} );

//Schedule the task

scheduler.schedule(task);

    }

}

Result

You have created a scheduler task for the HelloJob. When you schedule the task, the job will log two messages: Hello John and The length of your name is 4 characters at 8 AM on the April 1, 2007.

Note

If you schedule the task after 8 AM on the April 1, 2007, the job will not run. In this case, create a scheduler task for a later date.

 

 

End of Content Area