You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »


Introduction

JS7 offers a number of JS7 - Job Types including JS7 - Shell JobsJS7 - 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:

Example for implementation of a HelloWorld job with Java
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 its getLogger() method to write output to the job's log.

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

Example for implementation of a Job with Java
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 

      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
  • No labels