Skip to main content
Hitachi Vantara Lumada and Pentaho Documentation

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.

  1. Always make the first call to KettleEnvironment.init() whenever you are working with the PDI APIs.
  2. 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. 
  3. Populate the JobMeta object with job entries. The control flow of a job is defined by job entries that are connected by hops.
    1. Create the job entry by instantiating its class directly and configure it using its get and set methods. The job entries reside in sub-packages of org.pentaho.di.job.entries. For example, use the File Exists job entry, create an instance of org.pentaho.di.job.entries.fileexists.JobEntryFileExists, and use setFilename() to configure it. The Start job entry is implemented by org.pentaho.di.job.entries.special.JobEntrySpecial.
    2. 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 of JobEntryCopy 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, call setDrawn(true) and setLocation(x,y) to make sure the job entry appears correctly on the Spoon canvas. Finally, add the job entry to the job by calling addJobEntry() on the job definition object. It is possible to  place the same job entry in several places on the canvas by creating multiple instances of JobEntryCopy and passing in the same job entry instance. 
    3. 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 calling setConditional() and setEvaluation(true/false). If it is an unconditional hop, call setUnconditional(). Add the hop to the job definition by calling addJobHop().

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.