Calling a Talend Job from an external Java application

Overview

Talend Studio constructs a Java environment. You can execute any Java code using Java components like tJava or tJavaRow. You can customize Java code in a routine. In addition, Talend Studio allows you to export the Job script and call it from external Java applications like Eclipse or Netbean. This procedure explains how to call a Talend Job from a Java program in Eclipse.

 

Environment

This procedure was written with:

  • Talend Open Studio for Data Integration 5.5.0 r117820
  • Sun JDK build 1.7.0_02 64-bit
  • Windows 7 Professional
  • Eclipse Standard/SDK: Luna release (4.4.0)

Talend verified this procedure to be compatible with all versions of Talend Studio.

Note: Starting from version 6.0, Talend Studio requires a JDK installation to build jobs completely. For more information, please refer to JDK Required to Build Jobs/Routes in the Talend Help Center.

 

Procedure

Create a demo Job

This example begins by creating an example Job called CallJobInEclipseDemo.

In this Job, you will:

  • Define two context variables: context.nb_line and context.name.
  • Use a tFixedFlowInput component to create rows of data and append them to a file (such as: "D:/file/out.txt").

The detailed Job settings are as follows:

  1. Create and define the context variables in the Contexts tab:

    job_design.png


  2. Click the Values as table tab and set their default value:

    define_default_value.png


  3. Configure the tFixedFlowInput component in order to generate an input data:

    tFixedFlowInput.png


  4. Configure the tFileOutputDelimited component in order to output the data into a file (D:/file/out.txt in this example).
  5. Take care to select the Append checkbox so that the data gets added to an existing file, and not overwritten every time the Job is executed.

    tFileOutputDelimited.png


Note: This Job is available in the attachments list. In order to import the demo job into your studio, you have to use the Studio version 5.5.0 r117820 or higher.

 

Execute your demo Job in the Talend Studio

After designing the Job, execute the Job to ensure it works well in the Talend Studio. Open the D:file/out.txt file to check that the data is properly appended: Default name 'talend' appended with the current system timestamp.

talend;2014-08-04 09:34:13

 

Export the Job script

Refer to How to export a Job script on the Talend Help Center for instructions.

 

Integrate the Talend Job into Eclipse

Follow the steps to import the Talend Job into a Java project in Eclipse:

  1. Launch Eclipse and create a new Java project. For this example, call it P1.

    create_new_java_project.png


  2. Create a new Java class called TalendExample that will call the Talend Job.

    create_new_class.png

    create_new_class2.png


  3. Right-click the root folder of the P1 project, then select Build path.
  4. Then select the Configure Build Path option.

    configure_build_path.png


  5. In the next window, open the Libraries tab and click Add external Jars to import the Job jar and all the dependent jars. (For example, this demo Job contains the following jars, located under the <Job script unzip dir> directory.)

    calljobineclipsedemo_0_1.jar (Located in \CallJobInEclipseDemo\)
    axis.jar (Located in \lib\)
    dom4j-1.6.1.jar (Located in \lib\)
    jaxrpc.jar (Located in \lib\)
    systemRoutines.jar (Located in \lib\)
    talend_file_enhanced_20070724.jar (Located in \lib\)
    talendcsv.jar (Located in \lib\)
    userRoutines.jar (Located in \lib\)

    import_external_libs.png


  6. Import the context configuration file Default.properties:
    1. Open the Job script unzip dir\CallJobInEclipseDemo folder.
    2. Copy the di_demo folder (di_demo is the project name in this example).

      import_context_properties_file.png


    3. In Eclipse, paste it to the src folder.

      paste_context_prosperties_file.png


  7. Open the TalendExample.java class and edit its code as follows:

    package org.talend;
    import di_demo.calljobineclipsedemo_0_1.CallJobInEclipseDemo; 
    public class TalendExample { 
        public static void main(String[] args) { 
            // TODO Auto-generated method stub
            CallJobInEclipseDemo talendJob=new CallJobInEclipseDemo(); 
            talendJob.runJob(new String[]{}); 
        } 
    }
    
    java_code.png

 

Execute the Java program in Eclipse

Execute the TalendExample.java class. Open theD:file/out.txt file and see whether a new line of data is appended. If so, it means the Job was executed successfully in the Java program.

talend;2014-08-04 09:34:13
talend;2014-08-04 10:57:37

 

Pass new values to context variables

You can also pass new values to the context variables when calling a Talend Job in a Java program. This functionality is useful in real-world cases, because the values may be provided by other code snippets.

For example, edit the TalendExample.java class as follows:

package org.talend;
import di_demo.calljobineclipsedemo_0_1.CallJobInEclipseDemo;
public class TalendExample {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CallJobInEclipseDemo talendJob=new CallJobInEclipseDemo();
        int nb_line=2;
        String name="Ross";
        String [] context=new String[] {"--context_param nb_line="+nb_line,"--context_param name="+name};
        talendJob.runJob(context);
    }
}

Execute the TalendExample.java class. Open the D:file/out.txt file and see that the two lines of data are appended:

talend;2014-08-04 09:34:13
talend;2014-08-04 10:57:37
Ross;2014-08-04 11:12:04
Ross;2014-08-04 11:12:04
Version history
Revision #:
5 of 5
Last update:
‎06-12-2017 04:25 PM
Updated by:
 
Labels (1)
Tags (1)
Comments
stepro

This is very nice. Is there a sample, where the calling java application gets a return value from the talend job?

agershenson

 Hi Stepro,

 

You might get a better response if you post your question on one of the discussion forums, or for technical help constructing your job, please file a Support ticket here:  https://www.talend.com/services/technical-support/.

 

HTH,

Alyce