on 07-28-2015 2:34 PM
Hello,
I wrote a script which should create one job per each row in a table. The table has two columns (key and parameter).
The script works only for the first value. Afterwards the job goes to error with the error message "Trying to use non-attached object TableValue".
May you help me to find the failure?
Source code:
import java.util.Iterator;
import com.redwood.scheduler.api.model.*;
import com.redwood.scheduler.api.model.enumeration.*;
import com.redwood.scheduler.api.scripting.variables.ScriptSessionFactory;
{
String vTableName = "TABLE1";
SchedulerSession session = ScriptSessionFactory.getSession();
Table importTable = session.getTableByName(vTableName);
if(importTable == null)
{
jcsOut.println("Table " + vTableName + " does not exist");
return;
}
// Iterator for looping through the table
Iterator iterTV = importTable.getTableValues();
while(iterTV.hasNext())
{
// Get the single table value
TableValue tv = (TableValue)iterTV.next();
// Check, if the table value belongs to a specific column
if (tv.getColumnName().equals("PARAMETER"))
{
// Get the key to identify the table row
String vkey = tv.getKey();
String strParameter = importTable.getTableValueBySearchKeySearchColumnName(vkey,"PARAMETER").getColumnValue();
// Get the job definition
JobDefinition jd = jcsSession.getJobDefinitionByName("TEST_JOBDEFINITION");
// Prepare a job based on the job definition
Job job = jd.prepare();
// Provide the other parameter values to the job
jcsOut.println("JOBNAME: " + strParameter);
job.getJobParameterByName("p_JobName").setInValueString(strParameter);
job.getJobParameterByName("jdpName").setInValueString("PRINT_PDEST");
job.getJobParameterByName("replaceValue").setInValueString("");
job.getJobParameterByName("p_ChangeJobDef").setInValueString("Y");
job.getJobParameterByName("p_TestRun").setInValueString("Y");
jcsOut.println("");
jcsSession.persist();
jcsSession.waitForJob(job);
jcsSession.refreshObjects(new SchedulerEntity[]{jd});
}
}
jcsOut.println("All requests from Table " + vTableName + " processed.");
}
Best regards
Dana
Hello Dana,
This has to do with the refreshing of the session, this removes all retrieved objects from the session, also the entries that are still in your Iterator.
It is better to setup two SchedulerSessions, one for the query and one for the submit. Or do the query, store the items in a list and do a loop on that.
Regards Gerben
PS: a small improvement:
String strParameter = importTable.getTableValueBySearchKeySearchColumnName(vkey,"PARAMETER").getColumnValue();
can be replaced with:
String strParameter = tv.getColumnValue();
Because you already have the correct TableValue selected here.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Gerben,
I thought session and jcsSession are already 2 sessions!?
I created a second session but got the same error message. Do I use the sessions in a wrong way?
import java.util.Iterator;
import com.redwood.scheduler.api.model.*;
import com.redwood.scheduler.api.model.enumeration.*;
import com.redwood.scheduler.api.scripting.variables.ScriptSessionFactory;
{
String vTableName = "TABLE1";
SchedulerSession session1 = ScriptSessionFactory.getSession();
SchedulerSession session2 = ScriptSessionFactory.getSession();
Table importTable = session1.getTableByName(vTableName);
if(importTable == null)
{
jcsOut.println("Table " + vTableName + " does not exist");
return;
}
// Iterator for looping through the table
Iterator iterTV = importTable.getTableValues();
while(iterTV.hasNext())
{
// Get the single table value
TableValue tv = (TableValue)iterTV.next();
// Check, if the table value belongs to a specific column
if (tv.getColumnName().equals("PARAMETER"))
{
// Get the key to identify the table row
String vkey = tv.getKey();
String strParameter = tv.getColumnValue();
// Get the job definition
JobDefinition jd = session2.getJobDefinitionByName("TEST_JOBDEFINITION");
// Prepare a job based on the job definition
Job job = jd.prepare();
// Provide the other parameter values to the job
jcsOut.println("JOBNAME: " + strParameter);
job.getJobParameterByName("p_JobName").setInValueString(strParameter);
job.getJobParameterByName("jdpName").setInValueString("PRINT_PDEST");
job.getJobParameterByName("replaceValue").setInValueString("");
job.getJobParameterByName("p_ChangeJobDef").setInValueString("Y");
job.getJobParameterByName("p_TestRun").setInValueString("Y");
jcsOut.println("");
session2.persist();
session2.waitForJob(job);
session2.refreshObjects(new SchedulerEntity[]{jd});
}
}
jcsOut.println("All requests from Table " + vTableName + " processed.");
}
Best regards
Dana
User | Count |
---|---|
80 | |
24 | |
12 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.