
This procedure tells you how to create a scheduler task for the HelloJob definition that instructs the Java Scheduler to run a HelloJob instance within the next minute from the current time on.
The HelloJob definition is deployed on the server and that the SAP NetWeaver Scheduler for Java is running.
For the reference documentation of the Scheduler API, see the Java doc at http://help.sap.com/javadocs/nwce/current/sc/com.sap.sc/index.html and refer to the com.sap.scheduler.api package.
Obtain a JNDI reference to the scheduler.
For more information, see Getting a JNDI Reference to the Scheduler API .
Get the JobParameterDefinition of the IN parameter UserName and use it to construct a JobParameter with value John .
Create a SchedulerTime object that represents the desired time and use it to construct a RecurringEntry with a single expiration time.
Pass the JobDefinitionId , the JobParameter , and the RecurringEntry to the static method createSchedulerTask(...) of the SchedulerTask class to obtain the SchedulerTask object. For the other parameters of the createSchedulerTask method see the JavaDoc of the Scheduler API.
You schedule the task by calling the schedule() method.
|
package com.sap.scheduler.examples.hellojob; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; import javax.ejb.Stateless; import javax.naming.InitialContext; import javax.naming.NamingException; import com.sap.scheduler.api.RecurringEntry; import com.sap.scheduler.api.Scheduler; import com.sap.scheduler.api.SchedulerTask; import com.sap.scheduler.api.SchedulerTime; import com.sap.scheduler.api.TaskValidationException; 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.getParameter("UserName"); //Specify the input for the IN parameter JobParameter[] param = { new JobParameter(def, "John") }; // Create SchedulerTime instance for one minute in the future Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MINUTE, 1); Date date = calendar.getTime(); //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 = SchedulerTask.createSchedulerTask(helloJobDef.getJobDefinitionId(), param, re, null, null, -2, "Hellojob Task", "Hellojob Task Description", null); //Schedule the task try { scheduler.schedule(task); } catch (TaskValidationException e) { return; } return; } |
You have created a scheduler task for the HelloJob . When you schedule the task, the job will run after a minute and log two messages: Hello John and The length of your name is 4 characters .
You can only schedule tasks that have execution times in the future. For this reason, you cannot omit the line calendar.add(Calendar.MINUTE, 1); from the code.