Page History
...
Display feature availability | ||
---|---|---|
|
Implementation
Implementing a basic Job
A Java Job extends the class com.sos.js7.job.Job
like this:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
import com.sos.js7.job.Job;
import com.sos.js7.job.JobArguments;
import com.sos.js7.job.OrderProcessStep;
public class HelloWorld extends Job<JobArguments> {
@Override
public void processOrder(OrderProcessStep<JobArguments> step) throws Exception {
step.getLogger().info("Hello World!");
}
} |
Explanation:
- The extension of the class
com.sos.js7.job.Job
is required. - The
processOrder()
method is required to be implemented by the job.- The method is parameterized by the
OrderProcessStep
object provided by the Job API. The
OrderProcessStep
object is used with itsgetLogger()
method to write output to the job's log.
- The method is parameterized by the
Accessing Arguments
A Java Job can access arguments from a number of sources, see JS7 - Job API.
The straightforward way how to read arguments is explained from the next chapter.
Example: Reading specific Arguments
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
import java.util.List;
import com.sos.js7.job.Job;
import com.sos.js7.job.JobArgument;
import com.sos.js7.job.OrderProcessStep;
import com.sos.js7.job.OrderProcessStepOutcome;
public class JobWithParams extends Job<MyJobArguments> {
@Override
public void processOrder(OrderProcessStep<MyJobArguments> step) throws Exception {
// reading all arguments with "getAllArgumentsAsNameValueMap"
step.getAllArgumentsAsNameValueMap().forEach((name, value) -> {
step.getLogger().info("argument: " + name + "=" + value.toString());
});
// reading all arguments with "getAllArguments"
step.getAllArguments().forEach((name, jobArgument) -> {
step.getLogger().info("argument: " + name + "=" + jobArgument.getValue().toString());
}); MyJobArguments myArgs = step.getDeclaredArguments();
// reading myString
JobArgument<String> myString = myArgs.getMyString();
step.getLogger().info(myString.getName() + "=" + myString.getValue());
// reading myBool
JobArgument<Boolean> myBool = myArgs.getMyBool();
step.getLogger().info(myBool.getName() + "=" + myBool.getValue());
// reading myList
JobArgument<List<String>> myList = myArgs.getMyList();
step.getLogger().info(myList.getName() + "=" + myList.getValue().toString());
// return values
OrderProcessStepOutcome outcome = step.getOutcome();
outcome.putVariable(myString.getName(), "hello world");
outcome.putVariable("myInteger", 42);
}
} |
Explanation:
- The
getAllArgumentsAsNameValueMap()
method provides a map of all arguments. The map allows to read a single argument from the argument name. - The OrderProcessStep object is here typed by
MyJobArguments
that is a class with job-specific declared arguments.A class with job specific declared arguments extends
com.sos.js7.job.JobArguments
Code Block language java title Example of a class of the job-specific declared arguments import java.util.List; import com.sos.js7.job.JobArgument; import com.sos.js7.job.JobArguments; public class MyJobArguments extends JobArguments { private JobArgument<String> myString = new JobArgument<String>("myString", false); private JobArgument<List<String>> myList = new JobArgument<>("myList", false); private JobArgument<Boolean> myBool = new JobArgument<>("test", false, true); public JobArgument<String> getMyString() { return myString; } public JobArgument<List<String>> getMyList() { return myList; } public JobArgument<Boolean> getMyBool() { return myBool; } }
- For further methods to access arguments see JS7 - Job API.
Explanation:
- The
getAllArguments()
method provides a list of argument objects for iteration. - For further methods to access arguments see JS7 - Job API.
Overview
Content Tools