Building Jobs Dynamically
To enable your application to respond quickly to changing conditions, you can build jobs dynamically. The example class, org.pentaho.di.sdk.samples.embedding.GeneratingJobs
, shows you how. It generates a job definition and saves it to a .kjb file.
- Always make the first call to
KettleEnvironment.init()
whenever you are working with the PDI APIs. - Create and configure a job definition object. A job definition is represented by a
JobMeta
object. Create this object using the default constructor. The job definition includes the name, the declared parameters, and the required database connections. - Populate the
JobMeta
object with job entries. The control flow of a job is defined by job entries that are connected by hops.- Create the job entry by instantiating its class directly and configure it using its
get
andset
methods. The job entries reside in sub-packages oforg.pentaho.di.job.entries
. For example, use the File Exists job entry, create an instance oforg.pentaho.di.job.entries.fileexists.JobEntryFileExists
, and usesetFilename()
to configure it. The Start job entry is implemented byorg.pentaho.di.job.entries.special.JobEntrySpecial
. - Create an instance of
org.pentaho.di.job.entry.JobEntryCopy
by passing the job entry created in the previous step to the constructor. An instance ofJobEntryCopy
encapsulates the properties of a job entry, as well as controls the placement of the job entry on the Spoon canvas and connections to hops. Once created, callsetDrawn(true)
andsetLocation(x,y)
to make sure the job entry appears correctly on the Spoon canvas. Finally, add the job entry to the job by callingaddJobEntry()
on the job definition object. It is possible to place the same job entry in several places on the canvas by creating multiple instances ofJobEntryCopy
and passing in the same job entry instance. - Once job entries have been added to the job definition, they need to be connected by hops. To create a hop, create an instance of
org.pentaho.di.job.JobHopMeta
, passing in the From and To job entries as arguments to the constructor. Configure the hop consistently. Configure it as a green or red hop by callingsetConditional()
andsetEvaluation(true/false)
. If it is an unconditional hop, callsetUnconditional()
. Add the hop to the job definition by callingaddJobHop()
.
- Create the job entry by instantiating its class directly and configure it using its
After all job entries have been added and connected by hops, the job definition object can be serialized to a .kjb file by calling getXML()
, and opened in Spoon for inspection. The sample class org.pentaho.di.sdk.samples.embedding.GeneratingJobs
generates this job.