Introduction
JS7 offers a number of JS7 - Job Classes including JS7 - Shell Jobs, JS7 - JavaScript Jobs and Java Jobs.
- Java Jobs are operated in the Java Virtual Machine of the JS7 Agent
- Java Jobs are loaded on start-up of the JS7 Agent, this means there is zero latency when starting Java Jobs.
JS7 offers the JS7 - Job API to Java Jobs that serves the purpose of
- accessing arguments available to the job from various sources such as JS7 - Order Variables, job and node arguments, JS7 - Job Resources,
- allowing to specify the outcome of a job including return values that are passed to subsequent jobs from order variables.
Java jobs are available starting from the following releases:
FEATURE AVAILABILITY STARTING FROM RELEASE 2.5.4
FEATURE AVAILABILITY STARTING FROM RELEASE 2.6.1
Implementation
Implementing a basic Job
A Java Job extends the class com.sos.js7.job.Job
like this:
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
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 job-specific argument: myString JobArgument<String> myString = myArgs.getMyString(); step.getLogger().info(myString.getName() + "=" + myString.getValue()); // reading job-specific argument: myBool JobArgument<Boolean> myBool = myArgs.getMyBool(); step.getLogger().info(myBool.getName() + "=" + myBool.getValue()); // reading job-specific argument: 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 argument values. - The
getAllArguments()
method provides a map of argument objects. - 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
Example of a class of the job-specific declared argumentsimport 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.
Creating a Maven project in the development environment
The classes com.sos.js7.job.Job, com.sos.js7.job.JobArguments etc. are not available in a public Maven repository, so a local repository must be created in the Maven project.
- The directory
sos
has to be created - The following .jar files have to be copied from a JS7 Agent installation
./lib/sos
directory to thesos
directory of the Maven project. The example makes use of release 2.6.1, users should check for a current release.com.sos-berlin.js7.engine.js7-base-2.6.1.jar
com.sos-berlin.js7.engine.js7-data-for-java-2.6.1.jar
com.sos-berlin.js7.engine.js7-launcher-for-java-2.6.1.jar
sos-commons-exception-2.6.1.jar
sos-commons-util-2.6.1.jar
sos-js7-job-2.6.1.jar
The following
pom.xml
file can be used as a base in the Maven project.groupId
,artifactId
andversion
should be adjusted.The
pom.xml
file contains a profilecreate-local-repo
that must be executed once after copying the jar files. It updates the users local.m2
repository with the jars from the sos folder.> mvn initialize -Pcreate-local-repo -U
Adding other sos-berlin jar files to the repository
Using the pom.xml file
Add an execution element to the pom.xml file for each jar file that has to be added. Adjust the values for file, version and artifactId
<execution> <id>install-${sos.js7.commons.exception.artifactId}</id> <goals> <goal>install-file</goal> </goals> <phase>initialize</phase> <configuration> <file>[filePath]</file> <groupId>${sos.js7.groupId}</groupId> <artifactId>[artefactId]</artifactId> <version>${sos.js7.version}</version> <packaging>jar</packaging> <createChecksum>true</createChecksum> </configuration> </execution>
Using command line
Open a command shell and call
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
Example:
mvn install:install-file -Dfile=/sos/sos-commons-httpclient-2.5.5.jar -DgroupId=com.sos-berlin -DartifactId=sos-commons-httpclient -Dversion=2.5.5 -Dpackaging=jar
Further Resources
Features
Articles
Pages
Navigation