Example 5: Adding a job stream to the plan after modifying its contents
The program uses Java APIs to modify an existing job stream and to apply variable substitution before submitting it to the plan.
The following program adds job stream STREAM1 to
the plan. Before doing this, the program also:
- Adds two jobs to STREAM1 after getting their attributes from two jobs extracted from job streams STREAM2 and STREAM3.
- Sets up a number of JCL promptable variables before submitting job stream STREAM1.
- Further adds/modifies specific job attributes before releasing the jobs in the plan.
After importing all the necessary classes and objects, connecting
to the scheduler, and declaring the required variables, the program:
- Fetches a job from job stream STREAM2 in the data base, gets its properties and stores them in a ZosJobInfo container called jZos1, and sets other information such as the input arrival time and the associated workstation and resources.
- Repeats these actions for job with job number 10 of job stream STREAM3, storing the job properties in a ZosJobInfo container called jZos2.
- Adds the two jobs to job stream STREAM1, and modifies their properties to define their numbers (which constitute their IDs within the job stream) and names.
- Defines first general JCL variables for all four jobs in job stream STREAM1 (using the variablesToBeSubstituted API) and then particular variables for each job (using the jobVariablesToBeSubstituted API).
- Adds job stream STREAM1 to the plan.
- Makes final changes in the job stream to add more properties, such as the extended job name and a special resource, to one of the jobs and then releases the job.
package com.ibm;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import com.ibm.tws.conn.exception.ConnEngineNotMasterException;
import com.ibm.tws.conn.exception.ConnException;
import com.ibm.tws.conn.exception.ConnLockingException;
import com.ibm.tws.conn.exception.ConnNotFoundException;
import com.ibm.tws.conn.exception.ConnSecurityException;
import com.ibm.tws.conn.exception.ConnTransportException;
import com.ibm.tws.conn.exception.ConnValidationException;
import com.ibm.tws.conn.util.Context;
import com.ibm.tws.conn.util.QueryResult;
import com.ibm.tws.objects.filter.JobStreamFilters;
import com.ibm.tws.objects.filter.QueryFilter;
import com.ibm.tws.objects.filter.WorkstationFilters;
import com.ibm.tws.objects.model.Job;
import com.ibm.tws.objects.model.JobStream;
import com.ibm.tws.objects.model.JobStreamHeader;
import com.ibm.tws.objects.model.ResourceDependency;
import com.ibm.tws.objects.model.Workstation;
import com.ibm.tws.objects.model.WorkstationHeader;
import com.ibm.tws.objects.model.ZOSJobDefinition;
import com.ibm.tws.objects.plan.JobInPlan;
import com.ibm.tws.objects.plan.JobStreamInPlan;
import com.ibm.tws.objects.plan.ResourceDependencyInPlan;
import com.ibm.tws.objects.plan.ResourceInPlanKey;
import com.ibm.tws.objects.plan.WorkstationInPlanKey;
import com.ibm.tws.objects.plan.ZOSJobDefinitionInPlan;
import com.ibm.tws.objects.plan.types.DependenciesResolutionOption;
import com.ibm.tws.objects.plan.types.JobInPlanZOSAttributes;
import com.ibm.tws.objects.plan.utils.ZosJobInfo;
import com.ibm.tws.objects.types.Identifier;
import com.ibm.tws.zconn.model.ZConnModel;
import com.ibm.tws.zconn.plan.ZConnPlan;
import com.ibm.tws.zconn.plan.dao.impl.util.ResourceInPlanHelper;
import com.ibm.tws.zconn.plan.dao.impl.util.WorkstationInPlanHelper;
@SuppressWarnings("restriction")
public class MainWitRes {
public static void main(String[] args) {
//Create a connection to the server
TWSZConn connection=new TWSZConn();
//Get Model or Plan Bean
final ZConnModel model=connection.getModelBean();
final ZConnPlan plan=connection.getPlanBean();
final Context context=new Context();
com.ibm.websphere.security.auth.WSSubject.doAs
(connection.getSubject(), new java.security.PrivilegedAction<Object>() {
/* (non-Javadoc)
* @see java.security.PrivilegedAction#run()
*/
@SuppressWarnings("unchecked")
public Object run() {
//Variable Declaration
HashMap<String, List<Integer>> dependencyToDelete=new HashMap<String, List<Integer>>();
HashMap<String, List<Integer>> dependencyToAdd=new HashMap<String, List<Integer>>();
HashMap<Integer, String[][]> jobVariablesToBeSubstituted=new HashMap<Integer, String[][]>();
List<ZosJobInfo> jobsToDelete=new ArrayList<ZosJobInfo>();
List<ZosJobInfo> jobsToAdd=new ArrayList<ZosJobInfo>();
List<Identifier> identifierList=null;
List<Integer> successorList=new ArrayList<>();
List<JobStreamHeader> jobStreamHeaderList;
List<WorkstationHeader> workstationHeaderList;
List<ZosJobInfo> jobsToModify=new ArrayList<ZosJobInfo>();
List<ResourceDependencyInPlan> resourceDependencyInPlanList;
Date startTime=new Date();
Date deadlineTime=new Date();
Long time1,time2;
int seconds,minutes,hours;
int priority=5;
int jobNum=0;
Integer succ;
String [][] variablesMap1=new String [3][2];
String[][] variablesToBeSubstituted=new String [1][2];
String [][] variablesMap2=new String [2][2];
String resourceName;
String authorityGroup=null;
String JSName="STREAM1";
String description="";
String group=null;
String owner=null;
String ownerDescription=null;
String variableTable="STREAM1";
String jobStreamName1="STREAM2";
String jobStreamName2="STREAM3";
boolean holdAll=true;
JobStream jobStream1=null;
JobStream jobStream2=null;
Job jobFM1=null;
Job jobFM2=null;
JobStreamInPlan jobStreamInPlan=null;
ZOSJobDefinition zosJobDefinition1=null;
ZOSJobDefinition zosJobDefinition2=null;
ZosJobInfo zosJobInfo1=new ZosJobInfo();
ZosJobInfo zosJobInfo2=new ZosJobInfo();
ZosJobInfo zosJobInfo3=new ZosJobInfo();
ZosJobInfo zosJobInfo4=new ZosJobInfo();
QueryFilter queryFilter;
QueryResult queryResult;
Calendar calendar1,calendar2;
Workstation workstation = null;
ZOSJobDefinitionInPlan jobDefinitionInPlan;
ResourceDependencyInPlan resourceDependencyInPlan;
ResourceInPlanKey resourceInPlanKey;
WorkstationInPlanKey workstationInPlanKey;
JobInPlanZOSAttributes jobInPlanZosAtt;
//Getting jobStream="Stream2" from the DB, first the header then the full jobStream
queryFilter=new QueryFilter();
queryFilter.setFilter(JobStreamFilters.JOB_STREAM_NAME, jobStreamName1);
try {
queryResult=model.queryTWSObject(JobStream.class, queryFilter, 1, context);
jobStreamHeaderList=(List<JobStreamHeader>) queryResult.getList();
if(jobStreamHeaderList.size()>0){
jobStream1=(JobStream) model.getTWSObject(JobStream.class,jobStreamHeaderList.get(0).getKey() , false, context);
}
} catch (ConnTransportException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnValidationException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnSecurityException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
//Getting the first job from Stream2 to add to STREAM1
if(jobStream1.getJobs().size()>0){
jobFM1=(Job) jobStream1.getJobs().get(0);
zosJobDefinition1=(ZOSJobDefinition) jobFM1.getJobDefinition();
}
//Creation of ZosJobInfo for the first job
ZosJobInfo jZos1=new ZosJobInfo();
//Setting the inputArrivalTime
calendar1=Calendar.getInstance();
time1=jobFM1.getTimeRestrictions().getStartOffset();
if(time1!=null && time1>0){
seconds=(int) (time1 / 1000) % 60 ;
minutes=(int) ((time1 / (1000*60)) % 60);
hours=(int) ((time1 / (1000*60*60)) % 24);
calendar1.set(Calendar.HOUR,hours);
calendar1.set(Calendar.MINUTE, minutes);
calendar1.set(Calendar.SECOND, seconds);
jZos1.setInputArrivalTime(calendar1);
}
//Setting the zosJobInfo with data in zosJobDefinition and job (&timeRestriction)
String workstationN=zosJobDefinition1.getFlowTargetKey().getName();
jZos1.setJobName(zosJobDefinition1.getJclName());
jZos1.setTextDescription(jobFM1.getDescription());
jZos1.setWorkstationName(workstationN);
jZos1.setDuration(jobFM1.getEstimatedDuration());
jZos1.setJobNumber(44);
jZos1.setAutoSubmit(zosJobDefinition1.getAutoSubmit());
jZos1.setTaskType(zosJobDefinition1.getTaskType());
jZos1.setTimeDependent(jobFM1.getTimeRestrictions().isTimeDependent());
jZos1.setCentralizedScript(zosJobDefinition1.getHasCentralizedScript());
//Getting the workstation from the DB -> to get the type associated, first get the header and then the full
workstation.
queryFilter.setFilter(WorkstationFilters.WORKSTATION_NAME, workstationN);
try {
queryResult=model.queryTWSObject(Workstation.class, queryFilter, 1, context);
workstationHeaderList=(List<WorkstationHeader>) queryResult.getList();
if(workstationHeaderList.size()>0){
workstation=(Workstation) model.getTWSObject(Workstation.class,workstationHeaderList.get(0).getKey() ,
false, context);
}
} catch (ConnTransportException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnValidationException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnSecurityException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
jZos1.setWorkstationType(workstation.getType());
//Setting the Resource value
for(ResourceDependency resourceD:(List<ResourceDependency>) jobFM1.getResourceDependencies()){
String rName=resourceD.getResourceKey().getName();
if(rName!=null && rName.compareToIgnoreCase("Resource1")==0){
jZos1.setR1(resourceD.getQuantity());
}
if(rName!=null && rName.compareToIgnoreCase("Resource2")==0){
jZos1.setR2(resourceD.getQuantity());
}
if(rName!=null && rName.compareToIgnoreCase("ParallelServers")==0){
jZos1.setParallelServer(resourceD.getQuantity());
}
}
//Getting jobStream="STREAM3" from the Db, first the header then the full jobStream
queryFilter.setFilter(JobStreamFilters.JOB_STREAM_NAME, jobStreamName2);
try {
queryResult=model.queryTWSObject(JobStream.class, queryFilter, 1, context);
jobStreamHeaderList=(List<JobStreamHeader>) queryResult.getList();
if(jobStreamHeaderList.size()>0){
jobStream2=(JobStream) model.getTWSObject(JobStream.class,jobStreamHeaderList.get(0).getKey() ,
false, context);
}
} catch (ConnTransportException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnValidationException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnSecurityException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
//Starting with second job to add to STREAM1
//Getting the job with number=10 from STREAM3
for(Job job:(List<Job>)jobStream2.getJobs()){
if(job.getName().compareTo("10")==0){
jobFM2=job;
zosJobDefinition2=(ZOSJobDefinition) jobFM2.getJobDefinition();
}
}
//Creation of ZosJobInfo for the second job
ZosJobInfo jZos2=new ZosJobInfo();
//Setting the inputArrivalTime
calendar2=Calendar.getInstance();
time2=jobFM2.getTimeRestrictions().getStartOffset();
if(time2!=null && time2>0){
seconds=(int) (time2 / 1000) % 60 ;
minutes=(int) ((time2 / (1000*60)) % 60);
hours=(int) ((time2 / (1000*60*60)) % 24);
calendar2.set(Calendar.HOUR,hours);
calendar2.set(Calendar.MINUTE, minutes);
calendar2.set(Calendar.SECOND, seconds);
jZos2.setInputArrivalTime(calendar2);
}
//Setting the zosJobInfo for the second job
//with data in zosJobDefinition and job (@timeRestriction)
workstationN=zosJobDefinition2.getFlowTargetKey().getName();
jZos2.setJobName(zosJobDefinition2.getJclName());
jZos2.setTextDescription(jobFM2.getDescription());
jZos2.setWorkstationName(workstationN);
jZos2.setDuration(jobFM2.getEstimatedDuration());
jZos2.setJobNumber(45);
jZos2.setAutoSubmit(zosJobDefinition2.getAutoSubmit());
jZos2.setTaskType(zosJobDefinition2.getTaskType());
jZos2.setTimeDependent(jobFM2.getTimeRestrictions().isTimeDependent());
jZos2.setCentralizedScript(zosJobDefinition2.getHasCentralizedScript());
//Getting the workstation -> for the type associated. Header then full workstation.
queryFilter.setFilter(WorkstationFilters.WORKSTATION_NAME, workstationN);
try {
queryResult=model.queryTWSObject(Workstation.class, queryFilter, 1, context);
workstationHeaderList=(List<WorkstationHeader>) queryResult.getList();
if(workstationHeaderList.size()>0){
workstation=(Workstation) model.getTWSObject(Workstation.class,workstationHeaderList.get(0).getKey() ,
false, context);
}
} catch (ConnTransportException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnValidationException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnSecurityException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
jZos2.setWorkstationType(workstation.getType());
//Setting the Resource value
for(ResourceDependency resourceD:(List<ResourceDependency>) jobFM2.getResourceDependencies()){
String rName=resourceD.getResourceKey().getName();
if(rName!=null && rName.compareToIgnoreCase("Resource1")==0){
jZos2.setR1(resourceD.getQuantity());
}
if(rName!=null && rName.compareToIgnoreCase("Resource2")==0){
jZos2.setR2(resourceD.getQuantity());
}
if(rName!=null && rName.compareToIgnoreCase("ParallelServers")==0){
jZos2.setParallelServer(resourceD.getQuantity());
}
}
//Setting predecessor and successor for the jobs.
//Otherwise we cannot add them to STREAM1
succ= new Integer(jZos1.getJobNumber());
successorList.add(succ);
succ=new Integer(jZos2.getJobNumber());
successorList.add(succ);
dependencyToAdd.put(String.valueOf(1),successorList);
//Adding the zosJobInfo to STREAM1
jobsToAdd.add(jZos1);
jobsToAdd.add(jZos2);
//Example of how to modify a job in STREAM1
zosJobInfo1.setJobNumber(5);
zosJobInfo1.setJobName("EXEC1");
jobsToModify.add(zosJobInfo1);
zosJobInfo2.setJobNumber(10);
zosJobInfo2.setJobName("EXEC2");
jobsToModify.add(zosJobInfo2);
zosJobInfo4.setJobNumber(15);
zosJobInfo4.setJobName("EXEC3");
jobsToModify.add(zosJobInfo4);
zosJobInfo3.setJobNumber(20);
zosJobInfo3.setJobName("EXEC4");
jobsToModify.add(zosJobInfo3);
//Default JCL variables values (for all jobs)
variablesToBeSubstituted [0][0] = "VAR1";
variablesToBeSubstituted [0][1] = "ValVar1ForExec2And3";
DependenciesResolutionOption resolutionOption = DependenciesResolutionOption.RESOLUTION_ALL;
//Job level JCL variables values
variablesMap1 [0][0] = "VAR1";
variablesMap1 [0][1] = "ValVar1ForExec1";
variablesMap1 [1][0] = "VAR2";
variablesMap1 [1][1] = "ValVar2ForExec1";
variablesMap1 [2][0] = "VAR3";
variablesMap1 [2][1] = "ValVar3ForExec1";
jobNum = 5;
jobVariablesToBeSubstituted.put(jobNum, variablesMap1);
variablesMap2 [0][0] = "VAR2";
variablesMap2 [0][1] = "ValVar2ForExec2";
variablesMap2 [1][0] = "VAR4";
variablesMap2 [1][1] = "ValVar4ForExec2";
jobNum = 10;
jobVariablesToBeSubstituted.put(jobNum, variablesMap2);
String [][] variablesMap3 = new String [2][2];
variablesMap3 [0][0] = "VAR2";
variablesMap3 [0][1] = "ValVar2ForExec3";
variablesMap3 [1][0] = "VAR4";
variablesMap3 [1][1] = "ValVar4ForExec3";
jobNum = 15;
jobVariablesToBeSubstituted.put(jobNum, variablesMap3);
String [][] variablesMap4 = new String [4][2];
variablesMap4 [0][0] = "VAR3";
variablesMap4 [0][1] = "ValVar3ForExec4";
variablesMap4 [1][0] = "VAR4";
variablesMap4 [1][1] = "ValVar4ForExec4";
variablesMap4 [2][0] = "VAR5";
variablesMap4 [2][1] = "ValVar5ForExec4";
variablesMap4 [3][0] = "VAR6";
variablesMap4 [3][1] = "ValVar6ForExec4";
jobNum = 20;
jobVariablesToBeSubstituted.put(jobNum, variablesMap4);
//Add STREAM1 in the plan with all the modified and new jobs (zosJobInfo)
try {
identifierList = plan.editAddJobStreamInstanceWithVariableSubstitution(
JSName, startTime, deadlineTime, priority, description, group, owner,
ownerDescription, variableTable, jobsToDelete, jobsToAdd, jobsToModify,
dependencyToDelete, dependencyToAdd, variablesToBeSubstituted, authorityGroup,
holdAll, resolutionOption, jobVariablesToBeSubstituted, context);
Identifier jsid = identifierList.get(0);
// get the jobStream (STREAM1) from the plan
jobStreamInPlan = (JobStreamInPlan) plan.getPlanObject(JobStreamInPlan.class, jsid, context);
} catch (ConnLockingException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnNotFoundException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnSecurityException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnTransportException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnValidationException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnEngineNotMasterException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
//Modify the job in JobStreamInPlan to add data missing in ZosJobInfo
//Like extendedName(job 44) and specialResource(job=45)
for(JobInPlan jobInPlan:(List<JobInPlan>) jobStreamInPlan.getJobs()){
jobDefinitionInPlan=(ZOSJobDefinitionInPlan) jobInPlan.getJobDefinition();
if(jobInPlan.getName().compareTo("44")==0){
//Setting of some extended name and HORC
jobDefinitionInPlan.setHighestOkReturnCode(0);
jobDefinitionInPlan.setExtendedName(zosJobDefinition1.getExtendedName());
try {
plan.setJobInstance(jobInPlan, context);
} catch (ConnLockingException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnNotFoundException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnSecurityException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnTransportException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnValidationException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
if(jobInPlan.getName().compareTo("45")==0){
jobDefinitionInPlan.setHighestOkReturnCode(0);
jobInPlanZosAtt=jobInPlan.getZosSpecificAttributes();
resourceDependencyInPlanList=jobInPlan.getResourceDependencies();
//Adding a special Resource to the job in a jobStreamInPlan
for(ResourceDependency rD:(List<ResourceDependency>)jobFM2.getResourceDependencies()){
resourceName=rD.getResourceKey().getName();
if(resourceName!=null && !resourceName.isEmpty() && resourceName.compareToIgnoreCase("Resource1")!=0 &&
resourceName.compareToIgnoreCase("Resource2")!=0 && resourceName.compareToIgnoreCase("ParallelServers")!=0){
//Creation and setting of a ResourceDepInPlan
resourceDependencyInPlan=new ResourceDependencyInPlan();
resourceDependencyInPlan.setActionOnComplete(rD.getActionOnComplete());
resourceDependencyInPlan.setAllocationType(rD.getAllocationType());
resourceDependencyInPlan.setQuantity(rD.getQuantity());
workstationInPlanKey=new WorkstationInPlanKey();
workstationInPlanKey.setName(zosJobDefinition2.getFlowTargetKey().getName());
resourceInPlanKey=new ResourceInPlanKey(resourceName,workstationInPlanKey);
resourceDependencyInPlan.setId(ResourceInPlanHelper.keyToId(resourceInPlanKey));
resourceDependencyInPlan.setWorkstationId(WorkstationInPlanHelper.keyToId(workstationInPlanKey));
resourceDependencyInPlan.setKey(resourceInPlanKey);
resourceDependencyInPlanList.add(resourceDependencyInPlan);
jobInPlanZosAtt.setNumberOfSpecialResources(1);
break;
}
}
try {
plan.setWholeJobInstance(jobInPlan, false, null, null, null, null, null, jobInPlan.getResourceDependencies(),
null, null, null, null, context);
} catch (ConnLockingException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnNotFoundException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnSecurityException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnTransportException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnValidationException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
try {
plan.holdJobStreamInstanceJobs((Identifier)identifierList.get(0), false, context);
} catch (ConnLockingException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnNotFoundException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnSecurityException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnTransportException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnValidationException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}); // end doAs
;
}
}