Introduction
This article describes how order and job parameters from a job are accessed using the JobScheduler internal API. This technique is used in the JITL jobs provided with the JobScheduler and this article provides sufficient information to allow users to access order and job parameters in their own jobs.
Order and job parameters are accessed API jobs and pre-/post-processing routines in Java or another scripting language that can implement the Job_impl interface, via JobScheduler objects.
Getting the Objects
JobScheduler has two different types of parameters:
- Task parameters that are used in jobs and
- Order parameters that are used in orders.
- Both types of parameters are stored in different objects of type Variable_set. To get all parameters that are defined for a job, it is required to read both and merge them into a new object, which is not known to JobScheduler but to the program. This new object contains all parameters and, if order parameters are merged after merging the task parameters, the order parameters will overwrite task parameters with the same name.
From a technical point of view the "merger" enables the program to run with "standalone" or with "Job Chain" jobs as well.
The code shown below is part of the superclass JobSchedulerJobAdapter that is used for most of the latest JITL Jobs:
1 protected Variable_set getJobOrOrderParameters() { 2 @SuppressWarnings("unused") 3 final String conMethodName = conClassName + "::getParameters"; 4 try { 5 Variable_set objJobOrOrderParameters = spooler.create_variable_set(); 6 objJobOrOrderParameters.merge(getTaskParams()); 7 if (isJobchain() && hasOrderParameters()) { 8 objJobOrOrderParameters.merge(getOrderParams()); 9 } 10 logger.debug(String.format(Messages.getMsg(JSJ_D_0070), objJobOrOrderParameters.count())); 11 return objJobOrOrderParameters; 12 } 13 catch (Exception e) { 14 throw new JobSchedulerException(String.format(Messages.getMsg(JSJ_F_0050), e.getMessage()), e); 15 } 16 } 17 protected Variable_set] getTaskParams() { 18 return spooler_task.params(); 19 } 20 protected Variable_set getOrderParams() { 21 return spooler_task.order().params(); 22 }
Let's have a look at the Java code:
- On line 5 a Variable_set is created from scratch. It is empty.
- On line 6 the content of the task parameters is merged into this Variable_set.
- On line 7 and 8 the order parameter, if the job runs as a job chain job, will be merged as well into the Variable_set
Get variable values
To get a variable value from the object Variable_set one has to use the method var()
:
Variable_set objVariables = getJobOrOrderParameters(); String myVar = objVariables.var("myVarName"); String myVar2 = objVariables.var("nextVarname);
If the variable with the given name does not exist then the value of the result variable will be null. The values of the JobScheduler's variables are always of type "String".
Set variable values
public void setJSParam(final String pstrKey, final String pstrValue) { @SuppressWarnings("unused") final String conMethodName = conClassName + "::setJSParam"; if (isNotNull(spooler_task.params())) \{ spooler_task.params().set_var(pstrKey, pstrValue); } if (hasOrderParameters()) { [http://www.sos-berlin.com/doc/en/scheduler.doc/api/Task-java.xml spooler_task].order().params().set_var(pstrKey, pstrValue); } if (isNotNull(objJobOrOrderParams)) { objJobOrOrderParams.set_var(pstrKey, pstrValue); } }
To set a variable or to modify the current value of a variable you would have to code:
setJSParam("myVariable", "the.value");
It is important to take into account that the modification of a variable is only effective for JobScheduler if this modification is done in the JobScheduler object and not in the copy which is provided by the getJobOrOrderParameters()
method.
Further References
Job and Order Parameters
- How to set and read job and order parameters
- How to pass parameters to subsequent shell jobs in a job chain
- How to clone parameters from a persistent order