Can I define a variable that is accessible from multiple Jobs?

 

Answer

Yes, you can declare a static variable in a routine, and add the setter/getter methods for this variable in the routine. The variable is then accessible from different Jobs. This article demonstrates how to implement this process.

Create a routine

  1. In the Repository tree view, click on Code node, then on Routines node.
  2. Right-click on Routines and select Create routine to create a new routine.
  3. In this example, name the new routine MyRoutine.

  4. Declare a static variable in the routine and set its value as null, for example:

    private static String name = null;
  5. Add the setter/getter methods for this variable in the routine.

    public static synchronized void setValue(String message) {
           name=message;
       }
     
       public static synchronized String getValue() {
           return name;
      }

Below is the complete Java code of this routine:

package routines;
/*
 * user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
 * it must be before the "{talendTypes}" key.
 *
 * 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
 * long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
 * Short
 *
 * 3. {Category} define a category for the Function. it is required. its value is user-defined .
 *
 * 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
 *
 * <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
 * Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
 * added. you can have many parameters for the Function.
 *
 * 5. {example} gives a example for the Function. it is optional.
 */
public class MyRoutine {
     private static String name = null;
    /**
     * helloExample: not return value, only print "hello" + message.
     *
     *
     * {talendTypes} String
     *
     * {Category} User Defined
     *
     * {param} string("world") input: The string need to be printed.
     *
     * {example} helloExemple("world") # hello world !.
     */
    public static void helloExample(String message) {
        if (message == null) {
            message = "World"; //$NON-NLS-1$
        }
        System.out.println("Hello " + message + " !"); //$NON-NLS-1$ //$NON-NLS-2$
    }
 
    public static synchronized void setValue(String message) {
        name=message;
    }
 
    public static synchronized String getValue() {
        return name;
   }
}

 

Create demo Jobs

You will create three Jobs to demonstrate this example.

  1. Create a Job called childJob1, as below:



    In this Job, use a
    tFixedFlowInput to generate an input data flow, and pass the data flow to a tJavaRow in which the Job calls the setter method to give a new value to the variable.


     
  2. Create another Job called childJob2 , as below:



    In this Job, there is only one tJava component, calling the getter method and assigning the return value to a string variable, then printing the variable value in the console.
     
  3. Create another Job called parentJob , as below:



    In this Job, there are two tRunJob components connected to each other via an OnSubjobOKlink. Each one of the tRunJob components calls respectively a child Job, childJob1 andchildJob2 .

    Icon

    This approach of defining a variable in routine and sharing it across different Jobs does not work with 'Use an independent process to run subjob' feature on tRunJob component.

Execute the demo Job

Execute the parentJob Job. You can see the results in the console:

Starting job parentJob at 19:52 21/06/2013.
 
[statistics] connecting to socket on port 3397
[statistics] connected
Talend
[statistics] disconnected
Job parentJob ended at 19:52 21/06/2013. [exit code=0]

Summary

Through the above example, we can conclude that sharing and passing a value between different Jobs is achieved by defining a static variable and adding the setter/getter methods in a routine, and calling the setter/getter method in the Job.

Version History
Revision #:
1 of 1
Last update:
‎04-17-2017 05:23 PM
Updated by:
 
Labels (1)
Contributors