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 the Code node, then right-click the Routines node and select Create routine to create a new routine.
  2. In this example, name the new routine MyRoutine.

    create_routine.png

     

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

    private static String name = null;
  4. 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} [()] [ : ]
     *
     *  's value should be one of: string, int, list, double, object, boolean, long, char, date. '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 shown below:

    childJob1.png

     

    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.

    tjavarow.png

     

  2. Create another Job called childJob2, as shown below:

    childjob2.png

     

    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 shown below:

    parentJob1.png

     

    In this Job, there are two tRunJob components connected to each other using an OnSubjobOK link. Each of the tRunJob components calls a child Job, childJob1 and childJob2 respectively.

    parentJob2.png

     

    Note: This approach of defining a variable in a routine and sharing it across different Jobs does not work with the Use an independent process to run subjob feature of a 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, adding the setter/getter methods in a routine, and calling the setter/getter method in the Job.

Version history
Revision #:
3 of 3
Last update:
‎06-23-2017 07:09 PM
Updated by:
 
Labels (1)
Tags (1)